Database testing : Check database logs

For every database add/update operation logs should be added.

Language: Java

Framework: JUnit using JDBC

copy
1import org.junit.*;2import java.sql.*;34public class DatabaseLogsTest {5 6 /​/​Assumption: Connection details to the database7 private final String DB_URL = "jdbc:postgresql:/​/​localhost:5432/​mydb";8 private final String USER = "myuser";9 private final String PASS = "mypass";10 11 /​/​Assumption: SQL query to retrieve all the logs12 private final String LOGS_QUERY = "SELECT * FROM logs";13 14 private Connection conn = null;15 16 /​/​Assumption: Establishing the connection to the database before running any tests17 @Before18 public void setUp() {19 try {20 conn = DriverManager.getConnection(DB_URL, USER, PASS);21 } catch(SQLException e) {22 e.printStackTrace();23 }24 }25 26 /​/​Assumption: Closing the connection to the database after running all tests27 @After28 public void tearDown() {29 try {30 if (conn != null) {31 conn.close();32 }33 } catch(SQLException e) {34 e.printStackTrace();35 }36 }37 38 /​/​Test that checks if logs are added for every database add operation39 @Test40 public void testAddOperationLogs() {41 /​/​Assumption: Performing a sample add operation on the database42 try {43 Statement stmt = conn.createStatement();44 String addQuery = "INSERT INTO users (name, email) VALUES ('John', 'john@example.com')";45 int rowsAdded = stmt.executeUpdate(addQuery);46 Assert.assertEquals("Logs not added for add operation", 1, rowsAdded);47 } catch(SQLException e) {48 e.printStackTrace();49 }50 }51 52 /​/​Test that checks if logs are added for every database update operation53 @Test54 public void testUpdateOperationLogs() {55 /​/​Assumption: Performing a sample update operation on the database56 try {57 Statement stmt = conn.createStatement();58 String updateQuery = "UPDATE users SET name='Jane' WHERE email='john@example.com'";59 int rowsUpdated = stmt.executeUpdate(updateQuery);60 Assert.assertEquals("Logs not added for update operation", 1, rowsUpdated);61 } catch(SQLException e) {62 e.printStackTrace();63 }64 }65 66 /​/​Test that checks if logs are added for every database add/​update operation67 @Test68 public void testAllOperationLogs() {69 /​/​Assumption: Performing a sample add operation on the database70 try {71 Statement stmt = conn.createStatement();72 String addQuery = "INSERT INTO users (name, email) VALUES ('John', 'john@example.com')";73 int rowsAdded = stmt.executeUpdate(addQuery);74 Assert.assertEquals("Logs not added for add operation", 1, rowsAdded);75 76 /​/​Assumption: Performing a sample update operation on the database77 String updateQuery = "UPDATE users SET name='Jane' WHERE email='john@example.com'";78 int rowsUpdated = stmt.executeUpdate(updateQuery);79 Assert.assertEquals("Logs not added for update operation", 1, rowsUpdated);80 } catch(SQLException e) {81 e.printStackTrace();82 }83 84 /​/​Assumption: Retrieving all logs for verification85 try {86 Statement stmt = conn.createStatement();87 ResultSet rs = stmt.executeQuery(LOGS_QUERY);88 int count = 0;89 while(rs.next()) {90 count++;91 }92 Assert.assertNotEquals("Logs not added for any operation", 0, count);93 } catch(SQLException e) {94 e.printStackTrace();95 }96 }97 98 /​/​Assumption: Code to connect to remote client with desired capabilities99 /​/​RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), DesiredCapabilities.chrome());100 101}

Language: Python

Framework: Pytest

copy
1# Assuming we have a database connection object called 'db_conn' already established.23import pytest45def test_database_logs():6 # Assuming there is a logs table in the database with columns 'operation' and 'timestamp'.7 cursor = db_conn.cursor()8 9 # Assuming we have a list of all the tables in the database.10 tables = ['table1', 'table2', 'table3']11 12 for table in tables:13 # Assuming the following query returns all add/​update queries for a specific table.14 query = f"SELECT * FROM {table}_logs WHERE operation IN ('ADD', 'UPDATE')"15 cursor.execute(query)16 logs = cursor.fetchall()17 assert len(logs) > 0, f"No logs found for {table}"18 19 cursor.close()20 21 # To connect to a remote client with desired capabilities, add the following code after the db_conn object has been established.22 # from selenium import webdriver23 # from selenium.webdriver.common.desired_capabilities import DesiredCapabilities24 # 25 # capabilities = DesiredCapabilities.CHROME.copy()26 # capabilities['platform'] = "WINDOWS"27 # capabilities['version'] = "10"28 # driver = webdriver.Remote(29 # command_executor='http:/​/​127.0.0.1:4444/​wd/​hub',30 # desired_capabilities=capabilities)31 # 32 # Then, replace any references to "chrome_driver" or "Firefox" with "driver" in the test code.

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.

Accelerate Your Automation Test Cycles With LambdaTest

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.

Try LambdaTest

Power Your Software Testing with AI and cloud

Test Intelligently and ship faster. Deliver unparalleled digital experiences for real world enterprises.

Start Free Testing