Database testing : Check index names

Index names should be given as per the standards e.g. IND_<Tablename><ColumnName>

Language: Java

Framework: JUnit using JDBC

copy
1/​/​Assumptions: 2/​/​1. Database connection details have been already set up. 3/​/​2. The schema and table names to be tested have been provided.45import java.sql.*;67import org.junit.Test;8import org.junit.BeforeClass;910public class DatabaseIndexNamesTest {1112 /​/​GetInstance method to establish database connection13 public static Connection getConnection() throws SQLException {14 String url = "jdbc:mysql:/​/​localhost:3306/​your_schema_here";15 String user = "your_username_here";16 String password = "your_password_here";17 18 Connection connection = DriverManager.getConnection(url, user, password);19 return connection;20 }2122 @BeforeClass23 public static void setUp() throws Exception {24 /​/​Assuming the JDBC driver has already been added to the classpath25 Class.forName("com.mysql.jdbc.Driver"); 26 }2728 @Test29 public void testIndexNames() throws SQLException {30 Connection con = null;31 Statement stmt = null;32 ResultSet rs = null;33 try {34 con = getConnection();35 stmt = con.createStatement();3637 /​/​Get all indexes' names for the given schema and table38 String query = "SHOW INDEXES FROM your_table_here WHERE Non_unique=1 AND Key_name NOT LIKE 'PRIMARY'";39 40 rs = stmt.executeQuery(query);41 while (rs.next()) {42 String indexName = rs.getString("Key_name");43 44 /​/​Check if index names are as per standards or not45 boolean isValidIndex = indexName.matches("IND_[A-Za-z0-9]*_[A-Za-z0-9]*");46 47 org.junit.Assert.assertTrue("Index name " + indexName + " is not as per standard", isValidIndex);48 }49 } finally {50 /​/​Close all instances51 if (rs != null) rs.close(); 52 if (stmt != null) stmt.close();53 if (con != null) con.close(); 54 }55 }56}5758/​/​To use Remote WebDriver with DesiredCapabilities:59/​*60import org.openqa.selenium.remote.DesiredCapabilities;61import org.openqa.selenium.remote.RemoteWebDriver;62import java.net.URL;6364/​/​Assuming the RemoteWebDriver server is already up and running65public class RemoteWebDriverExample {66 public static void main(String[] args) throws Exception {67 DesiredCapabilities capability = DesiredCapabilities.firefox();68 capability.setBrowserName("firefox");69 capability.setCapability("version", "latest");70 capability.setCapability("platform", "WINDOWS");71 RemoteWebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capability);72 driver.get("https:/​/​www.google.com");73 driver.quit();74 }75}76*/​

Language: Python

Framework: Pytest

copy
1import pytest2import mysql.connector34#Assuming that there is a MySQL database server running on a local machine with valid credentials5#Assuming that the database name is "testdb" and the table name is "testtable"67def test_index_names():8 cnx = mysql.connector.connect(user='user', password='password', host='localhost', database='testdb')9 cursor = cnx.cursor()10 11 cursor.execute('SHOW INDEX FROM testtable')12 indexes = cursor.fetchall()13 14 for index in indexes:15 assert index[2] == 'IND_testtable' + index[4] # Assuming that index column name is given as column name with trailing "_index"1617 cursor.close()18 cnx.close()19 20#Code to connect to remote client with desired capabilities21'''22from selenium import webdriver23from selenium.webdriver.common.desired_capabilities import DesiredCapabilities2425#Assuming that the remote server is accessible via url "http:/​/​192.168.0.100:4444/​wd/​hub"26#Assuming that the remote server has browser options for Chrome and Firefox with desired capabilities (e.g. version, platform)2728def test_website_login():29 chrome_cap = DesiredCapabilities.CHROME.copy()30 chrome_cap['version'] = 'latest'31 chrome_cap['platform'] = 'WINDOWS'3233 firefox_cap = DesiredCapabilities.FIREFOX.copy()34 firefox_cap['version'] = 'latest'35 firefox_cap['platform'] = 'WINDOWS'36 37 chrome_driver = webdriver.Remote(38 command_executor='http:/​/​192.168.0.100:4444/​wd/​hub',39 desired_capabilities=chrome_cap)40 41 firefox_driver = webdriver.Remote(42 command_executor='http:/​/​192.168.0.100:4444/​wd/​hub',43 desired_capabilities=firefox_cap)4445 #Assume the login page has an element named "username", "password" and a button named "login"46 chrome_driver.get('http:/​/​www.example.com')47 chrome_driver.find_element_by_name('username').send_keys('user')48 chrome_driver.find_element_by_name('password').send_keys('password')49 chrome_driver.find_element_by_name('login').click()50 51 firefox_driver.get('http:/​/​www.example.com')52 firefox_driver.find_element_by_name('username').send_keys('user')53 firefox_driver.find_element_by_name('password').send_keys('password')54 firefox_driver.find_element_by_name('login').click()5556 chrome_driver.quit()57 firefox_driver.quit()58'''59

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