Check if input data is not truncated while saving. The field length shown to the user on the page and in the database schema should be the same.
Language: Java
Framework: JUnit using JDBC
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 DatabaseTesting {910 //Assuming the database is already populated with test data and the schema is defined 11 String dbURL = "jdbc:mysql://localhost:3306/mydatabase";12 String username = "root";13 String password = "root";14 Connection conn = null;15 16 @Test17 public void checkDataTruncation() throws SQLException {18 19 try {20 //Connecting to the database21 conn = DriverManager.getConnection(dbURL, username, password);22 23 //Assuming the input data is saved to a table called "mytable"24 String query = "SELECT * FROM mytable";25 26 PreparedStatement statement = conn.prepareStatement(query);27 ResultSet result = statement.executeQuery();28 29 //Iterating through each column of the table to check if the data is truncated or not30 while(result.next()) {31 String column1 = result.getString("column1");32 String column2 = result.getString("column2");33 34 //Assuming the maximum length of column1 and column2 is 20 characters35 if(column1.length() > 20) {36 System.out.println("Data in column1 is truncated");37 }38 39 if(column2.length() > 20) {40 System.out.println("Data in column2 is truncated");41 }42 }43 44 } catch (SQLException e) {45 e.printStackTrace();46 } finally {47 //Closing the connection48 if (conn != null) {49 conn.close();50 }51 }52 }53 54 // Code to connect to remote client with desired capabilities55 /*56 DesiredCapabilities capabilities = new DesiredCapabilities();57 capabilities.setBrowserName("chrome");58 capabilities.setVersion("91.0");59 capabilities.setPlatform(Platform.WINDOWS);60 61 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);62 driver.get("http://example.com");63 */64}
Language: Python
Framework: Pytest
1import pytest2import psycopg234@pytest.fixture(scope="session")5def db_connection():6 # Assuming database credentials are already set up7 conn = psycopg2.connect(database="mydb", user="myuser", password="mypassword", host="localhost", port="5432")8 yield conn9 conn.close()1011def test_data_truncation(db_connection):12 cursor = db_connection.cursor()13 input_data = "This is a test to check data truncation"14 max_length = 50 # Assuming the max length of field is 50 characters15 16 # Check if input data is truncated while saving17 assert len(input_data) <= max_length, "Input data is too long and will be truncated"18 19 # Check if field length shown to the user on the page and in the database schema should be the same20 cursor.execute("SELECT CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'mytable' AND COLUMN_NAME = 'mycolumn'")21 database_schema_length = cursor.fetchone()[0] # Assuming only one row is returned22 assert database_schema_length == max_length, "Field length shown to the user on the page and in the database schema is not the same"23 24 cursor.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