Database testing : Check numeric fields

Check numeric fields with minimum, maximum, and float values.

Language: Java

Framework: JUnit using JDBC

copy
1/​/​Assuming the database connection has already been established2import static org.junit.Assert.*; 3import org.junit.After; 4import org.junit.Before; 5import org.junit.Test; 6import java.sql.*; 78public class NumericFieldsTest {9 Connection conn;10 Statement stmt;11 ResultSet rs;12 @Before13 public void setUp() throws ClassNotFoundException, SQLException {14 /​/​Connecting to the database15 Class.forName("com.mysql.jdbc.Driver");16 String url = "jdbc:mysql:/​/​localhost:3306/​testdb"; 17 conn = DriverManager.getConnection(url, "username", "password");18 stmt = conn.createStatement();19 }2021 @Test22 public void testNumericFields() throws SQLException {23 String query = "SELECT * FROM table_name WHERE numeric_field BETWEEN ? AND ?"; /​/​assuming 'numeric_field' is the name of the numeric field we want to test24 PreparedStatement pstmt = conn.prepareStatement(query);25 pstmt.setInt(1, 1); /​/​assuming minimum value is 126 pstmt.setInt(2, 10); /​/​assuming maximum value is 1027 rs = pstmt.executeQuery();28 while (rs.next()) {29 assertTrue(rs.getInt("numeric_field") >= 1 && rs.getInt("numeric_field") <= 10); /​/​checking if all values of numeric_field are between 1 and 1030 assertFalse(rs.getFloat("numeric_field") == (float)rs.getInt("numeric_field")); /​/​checking if any value of numeric_field is a float31 }32 }3334 @After35 public void tearDown() throws SQLException {36 conn.close();37 }38 39 /​/​Remote connection using desired capabilities40 /​*public WebDriver driver;41 @Before42 public void setUp() throws MalformedURLException {43 DesiredCapabilities capabilities = new DesiredCapabilities();44 capabilities.setBrowserName("chrome");45 capabilities.setPlatform(Platform.LINUX);46 driver = new RemoteWebDriver(new URL("http:/​/​localhost:4444/​wd/​hub"), capabilities);47 }4849 @Test50 public void testNumericFields() {51 /​/​Code to navigate to the website and login using Selenium52 }5354 @After55 public void tearDown() {56 driver.quit();57 }*/​58}

Language: Python

Framework: Pytest

copy
1# Assumptions:2# - Database used is MySQL.3# - The numeric fields to be tested are stored in a table named 'test_table'.4# - The minimum value for the fields is 0, the maximum value is 1000, and the float value is 5.5.56import pytest7import mysql.connector89@pytest.fixture(scope="module")10def db_connection():11 # Connect to MySQL database.12 conn = mysql.connector.connect(user='user', password='password',13 host='localhost',14 database='test_database')1516 yield conn1718 # Close the connection.19 conn.close()2021def test_numeric_fields(db_connection):22 cursor = db_connection.cursor()2324 # Query to fetch the numeric fields from the table.25 query = "SELECT numeric_field FROM test_table"2627 cursor.execute(query)2829 # Loop through the result set and check for the values.30 for (value,) in cursor:31 assert isinstance(value, (int, float)), f"Value {value} is not a valid numeric value"32 assert value >= 0, f"Value {value} is less than the minimum value of 0"33 assert value <= 1000, f"Value {value} is greater than the maximum value of 1000" 3435 if isinstance(value, float):36 assert value == 5.5, f"Value {value} is not equal to the float value of 5.5"3738# Uncomment the following code to connect to remote client with desired capabilities39# from selenium import webdriver40# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities4142# desired_cap = {43# 'browser': 'Chrome',44# 'browser_version': '91.0',45# 'os': 'Windows',46# 'os_version': '10'47# }4849# # Connect to remote client with desired capabilities.50# driver = webdriver.Remote(51# command_executor='http:/​/​localhost:4444/​wd/​hub',52# desired_capabilities=desired_cap53# )5455# # Use local driver.56# driver = webdriver.Chrome()

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