Database testing : Check input field spaces

Input field leading and trailing spaces should be truncated before committing data to the database.

Language: Java

Framework: JUnit using JDBC

copy
1import org.junit.Test;2import java.sql.Connection;3import java.sql.DriverManager;4import java.sql.PreparedStatement;5import java.sql.ResultSet;6import java.sql.SQLException;78public class DatabaseTest {910 /​/​Assuming Database Name is "TestDatabase", Table Name is "TestTable", Column Name is "TestColumn"11 /​/​Assuming JDBC URL is "jdbc:mysql:/​/​localhost/​TestDatabase"12 /​/​Assuming JDBC Driver Class is "com.mysql.jdbc.Driver"13 /​/​Assuming Database Credentials are "Username" and "Password"14 15 private final String JDBC_URL = "jdbc:mysql:/​/​localhost/​TestDatabase";16 private final String JDBC_DRIVER_CLASS = "com.mysql.jdbc.Driver";17 private final String DB_USERNAME = "Username";18 private final String DB_PASSWORD = "Password";1920 @Test21 public void testInputFieldSpaces() {2223 Connection conn = null;24 PreparedStatement stmt = null;25 ResultSet rs = null;2627 try {2829 /​/​Connecting to Local Driver - Uncomment to connect to Remote Client with Desired Capabilities30 31/​/​ DesiredCapabilities capabilities = new DesiredCapabilities();32/​/​ capabilities.setCapability(CapabilityType.BROWSER_NAME, "Chrome");33/​/​ capabilities.setCapability(CapabilityType.VERSION, "92");34/​/​ capabilities.setCapability(CapabilityType.PLATFORM_NAME, "Windows 10");35/​/​36/​/​ URL url = new URL("http:/​/​192.168.1.1:4444/​wd/​hub");37/​/​ WebDriver driver = new RemoteWebDriver(url, capabilities);3839 Class.forName(JDBC_DRIVER_CLASS);40 conn = DriverManager.getConnection(JDBC_URL, DB_USERNAME, DB_PASSWORD);4142 String input = " John ";43 String expectedOutput = "John";4445 /​/​Inserting data with leading and trailing spaces46 stmt = conn.prepareStatement("INSERT INTO TestTable(TestColumn) VALUES(?)");47 stmt.setString(1, input);48 stmt.executeUpdate();4950 /​/​Executing Select Query to Verify Leading and Trailing Space are removed51 stmt = conn.prepareStatement("SELECT TestColumn FROM TestTable WHERE TestColumn = ?");52 stmt.setString(1, expectedOutput);53 rs = stmt.executeQuery();5455 /​/​Verifying data with no leading and trailing spaces is retrieved56 while (rs.next()) {57 String actualOutput = rs.getString("TestColumn");58 assert(actualOutput.equals(expectedOutput));59 }6061 } catch (ClassNotFoundException | SQLException e) {62 e.printStackTrace();63 } finally {64 try {65 if (rs != null) rs.close();66 if (stmt != null) stmt.close();67 if (conn != null) conn.close();68 } catch (SQLException e) {69 e.printStackTrace();70 }71 }72 }73}

Language: Python

Framework: Pytest

copy
1import pytest2import mysql.connector34# Assumptions: MySQL database is being used to store data. Database connection details and table name are defined in the config file.56# Connecting to Database7def get_database_connection():8 connection_config = {9 'user': 'username',10 'password': 'password',11 'host': '127.0.0.1',12 'database': 'database_name',13 'port': '3306'14 }15 connection = mysql.connector.connect(**connection_config)16 return connection1718# Check input field spaces before committing data to the database19def test_input_field_spaces():20 connection = get_database_connection()21 cursor = connection.cursor()2223 # Creating table to store input field data24 cursor.execute("CREATE TABLE IF NOT EXISTS input_fields (id INT AUTO_INCREMENT PRIMARY KEY, input_field_value VARCHAR(255))")2526 # Input field value with spaces27 input_field_value = " Test Input Value "2829 # Truncate spaces from input field value and insert into database30 cursor.execute("INSERT INTO input_fields (input_field_value) VALUES ('{}')".format(input_field_value.strip()))3132 # Retrieve data from database and assert that leading and trailing spaces are truncated33 cursor.execute("SELECT input_field_value FROM input_fields")34 result = cursor.fetchone()35 retrieved_input_field_value = result[0]36 assert retrieved_input_field_value == "Test Input Value"3738 connection.commit()39 cursor.close()40 connection.close()4142# Code to connect to remote client with desired capabilities43# from selenium import webdriver44# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities4546# # Define desired capabilities47# capabilities = DesiredCapabilities.CHROME.copy()48# capabilities['platform'] = "WINDOWS"49# capabilities['version'] = "88.0"50# # Remote driver URL51# REMOTE_URL = "http:/​/​localhost:9515"5253# # Create driver with desired capabilities54# driver = webdriver.Remote(REMOTE_URL, capabilities)

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