Skip to content

Commit 040af46

Browse files
update tests for generating expected results
1 parent 3511bf3 commit 040af46

File tree

10 files changed

+75
-109
lines changed

10 files changed

+75
-109
lines changed

src/main/java/com/powertester/database/DBConnection.java

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import static org.junit.jupiter.api.Assertions.fail;
77

8+
import java.io.BufferedReader;
9+
import java.io.FileReader;
810
import java.sql.CallableStatement;
911
import java.sql.Connection;
1012
import java.sql.PreparedStatement;
@@ -18,7 +20,6 @@
1820
import java.util.Map;
1921
import lombok.extern.slf4j.Slf4j;
2022
import com.powertester.config.TestConfig;
21-
import com.powertester.utils.SqlUtils;
2223

2324
@Slf4j
2425
public class DBConnection {
@@ -81,13 +82,6 @@ public Connection getConnection() throws SQLException {
8182
return connection;
8283
}
8384

84-
// Execute multiple update queries
85-
public void update(List<String> sqlStatements) {
86-
for (String sql : sqlStatements) {
87-
update(sql);
88-
}
89-
}
90-
9185
// Execute update query
9286
public void update(String sql) {
9387
try (Connection connection = getConnection();
@@ -98,7 +92,39 @@ public void update(String sql) {
9892
}
9993
}
10094

101-
95+
public void updateFromFile(String filePath) {
96+
List<String> statements = extractSqlStatements(filePath);
97+
update(statements);
98+
}
99+
100+
private static List<String> extractSqlStatements(String sqlFilePath) {
101+
List<String> statements = new ArrayList<>();
102+
try (BufferedReader reader = new BufferedReader(new FileReader(sqlFilePath))) {
103+
StringBuilder queryBuilder = new StringBuilder();
104+
String line;
105+
while ((line = reader.readLine()) != null) {
106+
line = line.trim();
107+
if (line.isEmpty() || line.startsWith("--")) continue;
108+
queryBuilder.append(line);
109+
if (line.endsWith(";")) {
110+
statements.add(queryBuilder.toString().replace(";", ""));
111+
queryBuilder.setLength(0);
112+
} else {
113+
queryBuilder.append(" ");
114+
}
115+
}
116+
} catch (Exception e) {
117+
throw new SqlFileReadException("Failed to read SQL file: " + sqlFilePath, e);
118+
}
119+
return statements;
120+
}
121+
122+
// Execute multiple update queries
123+
private void update(List<String> sqlStatements) {
124+
for (String sql : sqlStatements) {
125+
update(sql);
126+
}
127+
}
102128

103129
// Preferred option 1: Execute a prepared statement and return the resultSet data as a list of map
104130
// of column name and value
@@ -122,7 +148,7 @@ public List<Map<String, String>> query(String sql, String... parameters) {
122148

123149
// Create another method for executePreparedStatement which accepts file paths that contains SQLs to execute
124150
public List<Map<String, String>> queryFromFile(String filePath) {
125-
String sql = SqlUtils.readSqlFromFile(filePath);
151+
String sql = extractSqlStatements(filePath).get(0);
126152
return query(sql);
127153
}
128154

@@ -183,3 +209,12 @@ public void closeConnectionPool() {
183209
dataSource.close();
184210
}
185211
}
212+
213+
/**
214+
* Custom exception for SQL file reading errors.
215+
*/
216+
class SqlFileReadException extends RuntimeException {
217+
public SqlFileReadException(String message, Throwable cause) {
218+
super(message, cause);
219+
}
220+
}

src/main/java/com/powertester/utils/CsvUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class CsvUtils {
1717
private CsvUtils() {}
1818

1919
// Read CSV into List<Map<String, String>>
20-
public static List<Map<String, String>> readCsvToMapList(String filePath) throws IOException {
20+
public static List<Map<String, String>> convertCsvToListOfMap(String filePath) throws IOException {
2121
List<Map<String, String>> rows = new ArrayList<>();
2222

2323
CSVFormat csvFormat = CSVFormat.Builder.create()
@@ -39,7 +39,7 @@ public static List<Map<String, String>> readCsvToMapList(String filePath) throws
3939
}
4040

4141
// Write List<Map<String, String>> to CSV
42-
public static void writeMapListToCsv(String filePath, List<Map<String, String>> data) throws IOException {
42+
public static void saveDataToCsvFile(String filePath, List<Map<String, String>> data) throws IOException {
4343
if (data.isEmpty()) return;
4444

4545
CSVFormat csvFormat = CSVFormat.Builder.create()

src/main/java/com/powertester/utils/SqlUtils.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/test/java/com/powertester/database/DBConnectionPassingTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
import com.powertester.extensions.TableCompareExtension;
1515
import com.powertester.utils.CsvUtils;
16-
import com.powertester.utils.SqlUtils;
1716

1817
@Slf4j
1918
class DBConnectionPassingTest {
@@ -23,12 +22,11 @@ class DBConnectionPassingTest {
2322
static void createTables() {
2423
// Read and execute each SQL statement from input.sql
2524
String sqlFilePath = "src/test/resources/data/tc03-inactive-customers/input.sql";
26-
List<String> statements = SqlUtils.readSqlStatements(sqlFilePath);
27-
db.update(statements);
25+
db.updateFromFile(sqlFilePath);
2826
}
2927

3028
@Test
31-
void testCompareEmpAndCustomerTables() {
29+
void compareOutputOfTwoSQLStatements() {
3230
// Arrange: input (could be done at a test, class or at project level)
3331

3432
// Act: (run the application to process input data). If the app is real time like APIs, this can be done at the test level.
@@ -46,15 +44,15 @@ void testCompareEmpAndCustomerTables() {
4644
}
4745

4846
@Test
49-
void testUsingExpectedCustomerCSVAndActualSQLOutput() throws java.io.IOException {
47+
void compareOutputOfSQLStatementWithAExpectedCSVFile() throws java.io.IOException {
5048
// Arrange: input (could be done at a test, class or at project level)
5149

5250
// Act: (run the application to process input data). If the app is real time like APIs, this can be done at the test level.
5351
// But if the app works as a batch and takes significant time to process data, it might also make sense to do this at the project level.
5452

5553
// Assert: Get input and output data to compare
5654
String expectedCSVFilePath = "src/test/resources/data/tc03-inactive-customers/expected.csv";
57-
List<Map<String, String>> expectedCustomers = CsvUtils.readCsvToMapList(expectedCSVFilePath);
55+
List<Map<String, String>> expectedCustomers = CsvUtils.convertCsvToListOfMap(expectedCSVFilePath);
5856

5957
String outputSQLFilePath = "src/test/resources/data/tc03-inactive-customers/output.sql";
6058
List<Map<String, String>> actualCustomers = db.queryFromFile(outputSQLFilePath);

src/test/java/com/powertester/utils/CreateExpectedCSVFileTest.java

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,23 @@ class CreateExpectedCSVFileTest {
2121

2222
@BeforeAll
2323
static void createTables() {
24-
// Create tables with more fields
25-
db.update(
26-
"CREATE TABLE student (id INT PRIMARY KEY, first_name VARCHAR(255), last_name VARCHAR(255), age INT, gender VARCHAR(10))");
27-
28-
// Clean tables before each test to avoid PK violation in repeated tests
29-
db.update("DELETE FROM student");
30-
31-
// Insert 3 records directly
32-
db.update(
33-
"INSERT INTO student (id, first_name, last_name, age, gender) VALUES (1, 'John', 'Doe', 30, 'Male')");
34-
db.update(
35-
"INSERT INTO student (id, first_name, last_name, age, gender) VALUES (2, 'Jane', 'Smith', 25, 'Female')");
36-
db.update(
37-
"INSERT INTO student (id, first_name, last_name, age, gender) VALUES (3, 'Alex', 'Brown', 28, 'Male')");
24+
// Read and execute each SQL statement from input.sql
25+
String sqlFilePath = "src/test/resources/data/create-expected-csv-file-test/input.sql";
26+
db.updateFromFile(sqlFilePath);
3827
}
3928

4029
@Test
4130
void generateExpectedCSVFileFromActualSQLOutput() throws java.io.IOException {
4231
// Arrange: Create expected results CSV based on actual SQL output. Later adjust the values as per requirements.
43-
List<Map<String, String>> actualRows = db.query("SELECT * FROM student");
44-
45-
// Generate expected csv file.
46-
String expectedCSVFilePath = "src/test/resources/data/tc02-premium-customers/expected.csv";
47-
CsvUtils.writeMapListToCsv(expectedCSVFilePath, actualRows);
48-
49-
// Get the generated CSV file
50-
List<Map<String, String>> expectedRows = CsvUtils.readCsvToMapList(expectedCSVFilePath);
51-
TableCompareExtension.captureRows(expectedRows, actualRows);
52-
53-
// Completeness check: Assert that both input and output are of same size.
54-
assertEquals(expectedRows.size(), actualRows.size());
55-
}
56-
57-
@Test
58-
void generateExpectedCSVFileFromActualSQLOutputFile() throws java.io.IOException {
59-
// Arrange: Create expected results CSV based on actual SQL output. Later adjust the values as per requirements.
60-
String outputSQLFilePath = "src/test/resources/data/tc01-new-customers/output.sql";
32+
String outputSQLFilePath = "src/test/resources/data/create-expected-csv-file-test/output.sql";
6133
List<Map<String, String>> actualRows = db.queryFromFile(outputSQLFilePath);
6234

6335
// Generate expected csv file.
64-
String expectedCSVFilePath = "src/test/resources/data/tc01-new-customers/expected.csv";
65-
CsvUtils.writeMapListToCsv(expectedCSVFilePath, actualRows);
36+
String expectedCSVFilePath = "src/test/resources/data/create-expected-csv-file-test/expected.csv";
37+
CsvUtils.saveDataToCsvFile(expectedCSVFilePath, actualRows);
6638

6739
// Get the generated CSV file
68-
List<Map<String, String>> expectedRows = CsvUtils.readCsvToMapList(expectedCSVFilePath);
40+
List<Map<String, String>> expectedRows = CsvUtils.convertCsvToListOfMap(expectedCSVFilePath);
6941
TableCompareExtension.captureRows(expectedRows, actualRows);
7042

7143
// Completeness check: Assert that both input and output are of same size.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ID,FIRST_NAME,LAST_NAME,AGE,GENDER
2+
1,John,Doe,30,Male
3+
2,Jane,Smith,25,Female
4+
3,Alex,Brown,28,Male
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Create tables with more fields
2+
CREATE TABLE student (id INT PRIMARY KEY, first_name VARCHAR(255), last_name VARCHAR(255), age INT, gender VARCHAR(10));
3+
4+
-- Clean tables before each test to avoid PK violation in repeated tests
5+
DELETE FROM student;
6+
7+
-- Insert 3 records directly
8+
INSERT INTO student (id, first_name, last_name, age, gender) VALUES (1, 'John', 'Doe', 30, 'Male');
9+
INSERT INTO student (id, first_name, last_name, age, gender) VALUES (2, 'Jane', 'Smith', 25, 'Female');
10+
INSERT INTO student (id, first_name, last_name, age, gender) VALUES (3, 'Alex', 'Brown', 28, 'Male');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT * FROM student;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
SELECT * FROM student
1+
SELECT * FROM student;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
SELECT * FROM customer
1+
SELECT * FROM customer;

0 commit comments

Comments
 (0)