Database testing : Check negative numeric fields

Check numeric fields with negative values (for both acceptance and non-acceptance).

Language: Java

Framework: JUnit using JDBC

copy
1/​/​Assumptions - Database is already set up and connected2/​/​The table we are testing is called "UserData"3/​/​The numeric field we are testing is called "Age"45import java.sql.*;67import org.junit.*;89public class DatabaseTest {1011 @Test12 public void testNumericFields() {1314 Connection conn = null;15 Statement stmt = null;16 ResultSet rs = null;1718 try {19 /​/​register JDBC driver20 Class.forName("com.mysql.jdbc.Driver");2122 /​/​Open a connection23 System.out.println("Connecting to database...");24 conn = DriverManager.getConnection("jdbc:mysql:/​/​localhost/​UserData", "root", "");2526 /​/​Execute a query27 System.out.println("Executing query...");28 stmt = conn.createStatement();2930 /​/​Test negative values for Age field31 rs = stmt.executeQuery("SELECT * FROM UserData WHERE Age < 0");3233 while(rs.next()){34 int age = rs.getInt("Age");35 Assert.assertTrue(age < 0);36 }3738 /​/​Test non-negative values for Age field39 rs = stmt.executeQuery("SELECT * FROM UserData WHERE Age >= 0");4041 while(rs.next()){42 int age = rs.getInt("Age");43 Assert.assertTrue(age >= 0);44 }4546 } catch(SQLException se) {47 /​/​Handle errors for JDBC48 se.printStackTrace();49 } catch(Exception e) {50 /​/​Handle errors for Class.forName51 e.printStackTrace();52 } finally {53 /​/​Close resources54 try {55 if(stmt!=null) stmt.close();56 } catch(SQLException se2) {57 } /​/​ do nothing58 try {59 if(conn!=null) conn.close();60 } catch(SQLException se) {61 se.printStackTrace();62 }63 }64 }65} 6667/​/​Code to connect to remote client with desired capabilities68/​* DesiredCapabilities capabilities = DesiredCapabilities.chrome();69capabilities.setCapability("version", "77.0.3865.40");70capabilities.setCapability("platform", "Windows 10");71capabilities.setCapability("name", "Database Testing");7273WebDriver driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"),capabilities); */​

Language: Python

Framework: Pytest

copy
1import pytest2from selenium import webdriver34#Assuming the database application runs on localhost and uses MySQL5db_host = "localhost"6db_user = "root"7db_password = "password"8db_name = "test_db"9table_name = "test_table"10column_name = "test_column"1112#Assuming the database has already been populated with test data13def test_negative_numeric_fields():14 #Connecting to the database15 db = MySQLdb.connect(host=db_host, user=db_user, password=db_password, db=db_name)1617 #Executing the query to get all negative numeric field values18 cursor = db.cursor()19 sql = "SELECT * FROM " + table_name + " WHERE " + column_name + " < 0"20 cursor.execute(sql)21 result = cursor.fetchall()2223 #Verifying that all numeric fields with negative values are returned24 assert len(result) > 0, "No negative numeric values returned"25 26 #Assuming that the acceptance criteria for negative numeric fields is that they must be less than zero27 #Verifying that all returned values are less than zero28 for row in result:29 assert row[column_name] < 0, "Numeric field value is not negative"30 31 #Assuming that the non-acceptance criteria for negative numeric fields is that they must not be greater than or equal to zero32 #Verifying that no returned values are greater than or equal to zero33 cursor = db.cursor()34 sql = "SELECT * FROM " + table_name + " WHERE " + column_name + " >= 0"35 cursor.execute(sql)36 result = cursor.fetchall()37 assert len(result) == 0, "Positive or zero numeric field values returned"38 39 #Closing the database connection40 db.close()4142#Assuming that the test is also running on a remote client with Chrome browser and the Selenium Grid is used43#Connecting to the remote client with desired capabilities44def test_remote_negative_numeric_fields():45 desired_capabilities = webdriver.DesiredCapabilities.CHROME.copy()46 desired_capabilities['version'] = '91.0'47 desired_capabilities['platform'] = 'WINDOWS'48 driver = webdriver.Remote(command_executor='http:/​/​localhost:4444/​wd/​hub',49 desired_capabilities=desired_capabilities)50 51 #Executing the test case as described above52 test_negative_numeric_fields()53 54 #Closing the driver session55 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