Database testing : Check data integrity

Check for data integrity. Data should be stored in single or multiple tables based on the design.

Language: Java

Framework: JUnit using JDBC

copy
1/​/​Assuming the database connection is established and database name is "sampleDB"23import java.sql.*;45import org.junit.*;67public class DataIntegrityTest {89 private static Connection conn;10 private static Statement stmt;1112 @BeforeClass13 public static void setUp() throws Exception {14 /​/​ Establishing the database connection15 Class.forName("com.mysql.jdbc.Driver");16 conn = DriverManager.getConnection("jdbc:mysql:/​/​localhost/​sampleDB", "username", "password");17 stmt=con.createStatement();18 }1920 @Test21 public void testDataIntegrity() throws SQLException {22 String[] tables = {"table1", "table2"}; /​/​assuming the data to be stored in these tables23 24 for(String table : tables) {25 26 ResultSet rs = stmt.executeQuery("SELECT COUNT(*) from "+ table); /​/​fetching count of rows in table27 int count = 0;28 if(rs.next()) {29 count = rs.getInt(1);30 }31 32 Assert.assertTrue("Data is not stored correctly in "+table+" table.", count>0); /​/​checking if any row is present in table33 }34 }3536 @AfterClass37 public static void tearDown() throws Exception {38 /​/​ Closing the database connection39 stmt.close();40 conn.close();41 }4243 /​/​code to connect to remote client with desired capabilities44 /​*45 ChromeOptions options = new ChromeOptions();46 options.addArguments("disable-infobars");47 options.addArguments("--start-maximized");48 options.setCapability(CapabilityType.PLATFORM_NAME, Platform.WINDOWS);49 options.setCapability(CapabilityType.VERSION, "10");50 options.setCapability("browserName", "chrome");51 options.setCapability("ms:edgeChromium", true);52 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), options);53 */​5455}

Language: Python

Framework: Pytest

copy
1import pytest2import mysql.connector34#Assumptions:5#1. The database to be tested is MySQL.6#2. Database connection details have been provided.789def test_data_integrity():10 # Connect to the database11 database = mysql.connector.connect(12 host="localhost",13 user="yourusername",14 password="yourpassword",15 database="yourdatabase"16 )1718 # Execute query to check for data integrity19 cursor = database.cursor()20 cursor.execute("SELECT COUNT(*) FROM your_table")21 result = cursor.fetchone()[0]2223 # Check if data integrity is maintained24 assert result > 0, "No data found in table"2526 # Close the database connection27 database.close()2829# Remote client code3031#from selenium import webdriver32#from selenium.webdriver.common.desired_capabilities import DesiredCapabilities3334#def test_data_integrity_remote():35 # Connect to the remote client with desired capabilities36 #capabilities = DesiredCapabilities.CHROME.copy()37 #capabilities['acceptInsecureCerts'] = True38 39 #driver = webdriver.Remote(40 #command_executor='http:/​/​your_ip_address:4444/​wd/​hub',41 #desired_capabilities=capabilities)4243 # Execute query to check for data integrity44 #cursor = database.cursor()45 #cursor.execute("SELECT COUNT(*) FROM your_table")46 #result = cursor.fetchone()[0]4748 # Check if data integrity is maintained49 #assert result > 0, "No data found in table"5051 # Close the database connection52 #database.close()

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