Database testing : Check column description

Table columns should have description information available (except for audit columns like created date, created by, etc.)

Language: Java

Framework: JUnit using JDBC

copy
1/​/​Assumptions: 2/​/​1. Application is using MySQL database.3/​/​2. Database connection settings are available in a db.properties file.4/​/​3. DB connection driver is added to the classpath.5/​/​4. TEST_TABLE is the name of the table to be tested.67import java.io.FileInputStream;8import java.sql.*;910import org.junit.Test;11import static org.junit.Assert.*;1213public class DatabaseTest {14 15 public Connection getConnection() throws SQLException {16 /​/​Read db.properties file17 Properties props = new Properties();18 FileInputStream in = new FileInputStream("db.properties");19 props.load(in);20 21 /​/​Get connection settings from properties file22 String url = props.getProperty("jdbc.url");23 String user = props.getProperty("jdbc.username");24 String password = props.getProperty("jdbc.password");25 26 /​/​Create database connection27 Connection conn = DriverManager.getConnection(url, user, password);28 return conn;29 }30 31 @Test32 public void testColumnDescription() throws SQLException {33 try(Connection conn = getConnection()) {34 /​/​Get metadata for TEST_TABLE columns35 ResultSet rs = conn.getMetaData().getColumns(null, null, "TEST_TABLE", null);3637 /​/​Iterate through columns38 while (rs.next()) {39 /​/​Skip audit columns40 String columnName = rs.getString("COLUMN_NAME");41 if (columnName.toLowerCase().contains("created")) {42 continue;43 }4445 /​/​Check if column has description46 String remarks = rs.getString("REMARKS");47 assertNotNull("Column description missing for " + columnName, remarks);48 assertTrue("Column description empty for " + columnName, !remarks.isEmpty());49 }50 }51 }52 53 /​/​Uncomment below code to use remote driver with desired capabilities54 /​/​public WebDriver getRemoteDriver(DesiredCapabilities capabilities) throws MalformedURLException {55 /​/​String remoteUrl = "http:/​/​<REMOTE_HOST>:<REMOTE_PORT>/​wd/​hub";56 /​/​return new RemoteWebDriver(new URL(remoteUrl), capabilities);57 /​/​}58}

Language: Python

Framework: Pytest

copy
1# Assumptions: 2# 1. Test database is available with necessary permissions3# 2. The table to be tested is known and accessible to the user4# 3. The user has access to necessary libraries and packages56import pytest7import pyodbc89def test_check_column_description():10 # Connecting to the database11 conn = pyodbc.connect('Driver={SQL Server};'12 'Server=server_name;'13 'Database=db_name;'14 'Trusted_Connection=yes;')1516 cursor = conn.cursor()1718 # Retrieving the columns and their descriptions19 cursor.execute("SELECT COLUMN_NAME, COLUMN_DESCRIPTION FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table_name' AND COLUMNPROPERTY(object_id('table_name'), COLUMN_NAME, 'IsComputed') = 0 AND COLUMNPROPERTY(object_id('table_name'), COLUMN_NAME, 'IsIdentity') = 0")2021 rows = cursor.fetchall()2223 for row in rows:24 column_name = row[0]25 column_description = row[1]2627 if (column_description is None) and (column_name not in ['created_date', 'created_by']):28 # The column does not have a description and is not an audit column29 assert False, f"{column_name} column does not have a description"3031 # Closing the connection32 conn.close()3334# Code to connect to remote client with desired capabilities:35# Assuming that we are using the Chrome browser36from selenium import webdriver37from selenium.webdriver.common.desired_capabilities import DesiredCapabilities3839def test_remote_browser():40 capabilities = DesiredCapabilities.CHROME.copy()41 capabilities['platform'] = "WINDOWS"42 capabilities['version'] = "10"4344 # Assuming that the remote server address and port are known45 driver = webdriver.Remote(46 command_executor='http:/​/​<REMOTE_SERVER_ADDRESS>:<PORT>/​wd/​hub',47 desired_capabilities=capabilities48 )4950 # Test code here5152 driver.quit()

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