Check if values for table audit columns (like created date, created by, updated, updated by, is deleted, deleted data, deleted by, etc.) are populated properly.
Language: Java
Framework: JUnit using JDBC
1Assuming a MySQL database with a table named 'users' with the following columns: id, name, email, created_date, created_by, updated_date, updated_by, is_deleted, deleted_date, deleted_by.23```4import org.junit.Assert;5import org.junit.Test;6import java.sql.Connection;7import java.sql.DriverManager;8import java.sql.ResultSet;9import java.sql.Statement;1011public class DBTest {12 @Test13 public void testAuditColumns() {14 String url = "jdbc:mysql://localhost:3306/mydb";15 String username = "root";16 String password = "password";17 Connection conn = null;18 Statement stmt = null;19 ResultSet result = null;20 21 try {22 Class.forName("com.mysql.cj.jdbc.Driver");23 conn = DriverManager.getConnection(url, username, password);24 stmt = conn.createStatement();25 result = stmt.executeQuery("SELECT * FROM users WHERE id = 1");26 27 // Assuming the 'created_date' and 'created_by' columns are populated when a user is created28 Assert.assertNotNull(result.getDate("created_date"));29 Assert.assertEquals("admin", result.getString("created_by"));30 31 // Assuming the 'updated_date' and 'updated_by' columns are populated when a user record is updated32 Assert.assertNotNull(result.getDate("updated_date"));33 Assert.assertEquals("admin", result.getString("updated_by"));34 35 // Assuming the 'is_deleted' column is populated with a boolean value when a user record is deleted36 Assert.assertTrue(result.getBoolean("is_deleted"));37 38 // Assuming the 'deleted_date' and 'deleted_by' columns are populated when a user record is deleted39 Assert.assertNotNull(result.getDate("deleted_date"));40 Assert.assertEquals("admin", result.getString("deleted_by"));41 42 } catch (Exception e) {43 e.printStackTrace();44 } finally {45 try {46 if (result != null) {47 result.close();48 }49 if (stmt != null) {50 stmt.close();51 }52 if (conn != null) {53 conn.close();54 }55 } catch (Exception e) {56 e.printStackTrace();57 }58 }59 }60 61 // Code for connecting to remote client with desired capabilities62 // Assumes a remote MySQL database with the same 'users' table63 // Assumes that the IP address of the remote client is '192.168.1.100' and the port number is '3306'64 /*65 public void testAuditColumnsOnRemoteDatabase() {66 String url = "jdbc:mysql://192.168.1.100:3306/mydb";67 DesiredCapabilities dc = new DesiredCapabilities();68 dc.setCapability("browserName", "chrome");69 WebDriver driver = new RemoteWebDriver(new URL("http://192.168.1.100:4444/wd/hub"), dc);70 // Rest of the code to test audit columns71 // ...72 driver.quit();73 }74 */75}76```
Language: Python
Framework: Pytest
1#Assuming the Database is named "TestDB" and table name is "test_table"23import pytest4import mysql.connector56#Connect to Local MySQL database7conn = mysql.connector.connect(8 host="localhost",9 user="root",10 password="password",11 database="TestDB"12)1314#Connect to Remote MySQL database with desired capabilities15#uncomment the below code and update the details as per the remote database connection16"""17from selenium import webdriver1819desired_cap = {20 'browserName': 'chrome',21 'version': '91.0',22 'enableVNC': True,23 'enableVideo': False24}2526driver = webdriver.Remote(27 command_executor='http://localhost:4444/wd/hub',28 desired_capabilities=desired_cap29)30"""3132def test_audit_columns():33 #Assuming column names as 'created_date', 'created_by', 'updated', 'updated_by', 'is_deleted', 'deleted_date', 'deleted_by'.34 35 cursor = conn.cursor()36 cursor.execute("SELECT created_date, created_by, updated, updated_by, is_deleted, deleted_date, deleted_by FROM test_table")37 result = cursor.fetchone()38 39 assert result[0]!=None, "created_date column not populated properly"40 assert result[1]!=None, "created_by column not populated properly"41 assert result[2]!=None, "updated column not populated properly"42 assert result[3]!=None, "updated_by column not populated properly"43 assert result[4]!=None, "is_deleted column not populated properly"44 assert result[5]!=None, "deleted_date column not populated properly"45 assert result[6]!=None, "deleted_by column not populated properly"
Disclaimer: Following code snippets and related information have been sourced from GitHub and/or generated using AI code generation tools. LambdaTest takes no responsibility in the accuracy of the code and is not liable for any damages.
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.
Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.
Start Free Testing