Database testing : Check logical names

Database logical names should be given according to the database name (again this is not standard but helpful for DB maintenance).

Language: Java

Framework: JUnit using JDBC

copy
1/​/​Assuming that the database is already set up and running.23import org.junit.Test;4import java.sql.Connection;5import java.sql.DriverManager;6import java.sql.ResultSet;7import java.sql.SQLException;8import java.sql.Statement;910public class TestDatabase {1112 @Test13 public void testLogicalNames() throws ClassNotFoundException, SQLException {1415 /​/​Assuming that the database name is 'mydb'.16 String dbUrl = "jdbc:mysql:/​/​localhost:3306/​mydb";1718 /​/​Assuming that the database credentials are 'root' as username and 'password' as password.19 String username = "root";20 String password = "password";2122 /​/​Connecting to the database.23 Class.forName("com.mysql.jdbc.Driver");24 Connection con = DriverManager.getConnection(dbUrl, username, password);2526 /​/​Creating a statement.27 Statement stmt = con.createStatement();28 29 /​/​Assuming that the logic for logical names is to append '_ln' to the database name.30 String expectedLogicalName = "mydb_ln";3132 /​/​Executing the SQL query to get the logical name of the database.33 String query = "SELECT DATABASE() AS db";34 ResultSet rs = stmt.executeQuery(query);3536 /​/​Checking if the logical name is as expected.37 if (rs.next()) {38 String actualLogicalName = rs.getString("db");39 if (!actualLogicalName.equals(expectedLogicalName)) {40 throw new AssertionError("Logical name not as expected. Expected: " + expectedLogicalName + ", Actual: " + actualLogicalName);41 }42 }4344 /​/​Closing the connection and the statement.45 rs.close();46 stmt.close();47 con.close();48 }49 50 /​*51 /​/​Code to connect to remote client with desired capabilities.52 public void connectToRemoteClient() {53 DesiredCapabilities desiredCapabilities = new DesiredCapabilities();54 desiredCapabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");55 desiredCapabilities.setCapability(CapabilityType.VERSION, "91.0");56 URL remoteUrl = new URL("http:/​/​localhost:4444/​wd/​hub");57 RemoteWebDriver driver = new RemoteWebDriver(remoteUrl, desiredCapabilities);58 /​/​rest of the code to interact with the application59 }60 */​6162}

Language: Python

Framework: Pytest

copy
1# Assumptions: 2# - The database credentials are stored in a separate file (e.g. config.py)3# - The database name is known and provided in the configuration file4# - The logical names are stored in a separate table (e.g. logical_names) with columns 'database_name' and 'logical_name'5# - Pytest is installed and configured properly67import pytest8import psycopg29import config1011def test_logical_names():12 # Connect to the local database13 conn = psycopg2.connect(14 host=config.host,15 database=config.database,16 user=config.user,17 password=config.password18 )19 20 # Create a cursor to execute SQL queries21 cur = conn.cursor()22 23 # Get the logical names of the database from the logical_names table24 cur.execute("SELECT logical_name FROM logical_names WHERE database_name = %s", (config.database,))25 logical_names = [name[0] for name in cur.fetchall()]26 27 # Check that the number of logical names matches the number of tables in the database28 cur.execute("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public'")29 num_tables = cur.fetchone()[0]30 assert len(logical_names) == num_tables31 32 # Close the cursor and the connection33 cur.close()34 conn.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