-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/test/java/g3201_3300/s3220_odd_and_even_transactions/MysqlTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package g3201_3300.s3220_odd_and_even_transactions; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.sql.Connection; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.stream.Collectors; | ||
import javax.sql.DataSource; | ||
import org.junit.jupiter.api.Test; | ||
import org.zapodot.junit.db.annotations.EmbeddedDatabase; | ||
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest; | ||
import org.zapodot.junit.db.common.CompatibilityMode; | ||
|
||
@EmbeddedDatabaseTest( | ||
compatibilityMode = CompatibilityMode.MySQL, | ||
initialSqls = | ||
"CREATE TABLE transactions(transaction_id INTEGER PRIMARY KEY, amount INTEGER" | ||
+ ", transaction_date DATE); " | ||
+ "INSERT INTO transactions(transaction_id, amount, transaction_date)" | ||
+ " VALUES (1, 150, '2024-07-01'); " | ||
+ "INSERT INTO transactions(transaction_id, amount, transaction_date)" | ||
+ " VALUES (2, 200, '2024-07-01'); " | ||
+ "INSERT INTO transactions(transaction_id, amount, transaction_date)" | ||
+ " VALUES (3, 75, '2024-07-01'); " | ||
+ "INSERT INTO transactions(transaction_id, amount, transaction_date)" | ||
+ " VALUES (4, 300, '2024-07-02'); " | ||
+ "INSERT INTO transactions(transaction_id, amount, transaction_date)" | ||
+ " VALUES (5, 50, '2024-07-02'); " | ||
+ "INSERT INTO transactions(transaction_id, amount, transaction_date)" | ||
+ " VALUES (6, 120, '2024-07-03'); ") | ||
class MysqlTest { | ||
@Test | ||
void testScript(@EmbeddedDatabase DataSource dataSource) | ||
throws SQLException, FileNotFoundException { | ||
try (final Connection connection = dataSource.getConnection()) { | ||
try (final Statement statement = connection.createStatement(); | ||
final ResultSet resultSet = | ||
statement.executeQuery( | ||
new BufferedReader( | ||
new FileReader( | ||
"src/main/java/g3201_3300/" | ||
+ "s3220_odd_and_even_transactions/script.sql")) | ||
.lines() | ||
.collect(Collectors.joining("\n")) | ||
.replaceAll("#.*?\\r?\\n", ""))) { | ||
assertThat(resultSet.next(), equalTo(true)); | ||
assertThat(resultSet.getNString(1), equalTo("2024-07-01")); | ||
assertThat(resultSet.getNString(2), equalTo("75")); | ||
assertThat(resultSet.getNString(3), equalTo("350")); | ||
assertThat(resultSet.next(), equalTo(true)); | ||
assertThat(resultSet.getNString(1), equalTo("2024-07-02")); | ||
assertThat(resultSet.getNString(2), equalTo("0")); | ||
assertThat(resultSet.getNString(3), equalTo("350")); | ||
assertThat(resultSet.next(), equalTo(true)); | ||
assertThat(resultSet.getNString(1), equalTo("2024-07-03")); | ||
assertThat(resultSet.getNString(2), equalTo("0")); | ||
assertThat(resultSet.getNString(3), equalTo("120")); | ||
assertThat(resultSet.next(), equalTo(false)); | ||
} | ||
} | ||
} | ||
} |