Tables should have a primary key column.
Language: Java
Framework: JUnit using JDBC
1import java.sql.Connection;2import java.sql.DriverManager;3import java.sql.ResultSet;4import java.sql.SQLException;5import java.sql.Statement;6import org.junit.Assert;7import org.junit.Test;89public class DatabaseTest {1011//Assuming database url, username and password to be passed in as arguments when running the test 12String dbUrl = System.getProperty("dbUrl");13String username = System.getProperty("username");14String password = System.getProperty("password");1516@Test17public void testPrimaryKey() throws SQLException {18Connection conn = null;19Statement stmt = null;20ResultSet rs = null;2122try {23//Connecting to the database24conn = DriverManager.getConnection(dbUrl, username, password);2526//Executing the SQL query to get table metadata27stmt = conn.createStatement();28rs = stmt.executeQuery("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'table1' AND COLUMN_KEY = 'PRI'");2930int columnCount = 0;31while(rs.next()) {32columnCount++;33}3435//Asserting that there is only 1 primary key column36Assert.assertEquals("Table1 should have only one primary key column", 1, columnCount);37} catch (SQLException e) {38e.printStackTrace();39} finally {40//Closing all open resources41if(rs != null) rs.close();42if(stmt != null) stmt.close();43if(conn != null) conn.close();44}45}46}4748//Code to connect to remote client with desired capabilities49//Assuming remote client url, browser name and version to be passed in as arguments when running the test50/*51DesiredCapabilities capabilities = new DesiredCapabilities();52capabilities.setBrowserName(System.getProperty("browserName"));53capabilities.setVersion(System.getProperty("browserVersion"));5455RemoteWebDriver driver = new RemoteWebDriver(new URL(System.getProperty("remoteUrl")), capabilities);56driver.get("https://www.example.com/");57*/
Language: Python
Framework: Pytest
1import pytest2import psycopg234#Assuming remote database connection details5remote_client={6 "host": "example.com",7 "port": "5432",8 "database": "test_db",9 "user": "test_user",10 "password": "test_password",11}1213#Assuming local database connection details14local_client={15 "host": "localhost",16 "port": "5432",17 "database": "test_db",18 "user": "test_user",19 "password": "test_password",20}2122def test_primary_key():23 #Connecting to the remote database24 # conn = psycopg2.connect(host=remote_client['host'], port=remote_client['port'], database=remote_client['database'], 25 # user=remote_client['user'], password=remote_client['password'])26 27 #Connecting to the local database28 conn = psycopg2.connect(host=local_client['host'], port=local_client['port'], database=local_client['database'], 29 user=local_client['user'], password=local_client['password'])30 31 cur = conn.cursor()32 33 #Assuming table name and primary key column name34 table_name = "employees"35 primary_key = "id"36 37 #Executing SQL query to check if table has primary key38 cur.execute(f"SELECT EXISTS ( SELECT FROM information_schema.columns WHERE table_name = '{table_name}' AND column_name = '{primary_key}' AND column_default IS NOT NULL )")39 has_primary_key = cur.fetchone()[0]40 41 assert has_primary_key, "Table does not have a primary key column"42 43 cur.close()44 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.
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