|
| 1 | +package g3201_3300.s3220_odd_and_even_transactions; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.FileNotFoundException; |
| 8 | +import java.io.FileReader; |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.ResultSet; |
| 11 | +import java.sql.SQLException; |
| 12 | +import java.sql.Statement; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import javax.sql.DataSource; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.zapodot.junit.db.annotations.EmbeddedDatabase; |
| 17 | +import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest; |
| 18 | +import org.zapodot.junit.db.common.CompatibilityMode; |
| 19 | + |
| 20 | +@EmbeddedDatabaseTest( |
| 21 | + compatibilityMode = CompatibilityMode.MySQL, |
| 22 | + initialSqls = |
| 23 | + "CREATE TABLE transactions(transaction_id INTEGER PRIMARY KEY, amount INTEGER" |
| 24 | + + ", transaction_date DATE); " |
| 25 | + + "INSERT INTO transactions(transaction_id, amount, transaction_date)" |
| 26 | + + " VALUES (1, 150, '2024-07-01'); " |
| 27 | + + "INSERT INTO transactions(transaction_id, amount, transaction_date)" |
| 28 | + + " VALUES (2, 200, '2024-07-01'); " |
| 29 | + + "INSERT INTO transactions(transaction_id, amount, transaction_date)" |
| 30 | + + " VALUES (3, 75, '2024-07-01'); " |
| 31 | + + "INSERT INTO transactions(transaction_id, amount, transaction_date)" |
| 32 | + + " VALUES (4, 300, '2024-07-02'); " |
| 33 | + + "INSERT INTO transactions(transaction_id, amount, transaction_date)" |
| 34 | + + " VALUES (5, 50, '2024-07-02'); " |
| 35 | + + "INSERT INTO transactions(transaction_id, amount, transaction_date)" |
| 36 | + + " VALUES (6, 120, '2024-07-03'); ") |
| 37 | +class MysqlTest { |
| 38 | + @Test |
| 39 | + void testScript(@EmbeddedDatabase DataSource dataSource) |
| 40 | + throws SQLException, FileNotFoundException { |
| 41 | + try (final Connection connection = dataSource.getConnection()) { |
| 42 | + try (final Statement statement = connection.createStatement(); |
| 43 | + final ResultSet resultSet = |
| 44 | + statement.executeQuery( |
| 45 | + new BufferedReader( |
| 46 | + new FileReader( |
| 47 | + "src/main/java/g3201_3300/" |
| 48 | + + "s3220_odd_and_even_transactions/script.sql")) |
| 49 | + .lines() |
| 50 | + .collect(Collectors.joining("\n")) |
| 51 | + .replaceAll("#.*?\\r?\\n", ""))) { |
| 52 | + assertThat(resultSet.next(), equalTo(true)); |
| 53 | + assertThat(resultSet.getNString(1), equalTo("2024-07-01")); |
| 54 | + assertThat(resultSet.getNString(2), equalTo("75")); |
| 55 | + assertThat(resultSet.getNString(3), equalTo("350")); |
| 56 | + assertThat(resultSet.next(), equalTo(true)); |
| 57 | + assertThat(resultSet.getNString(1), equalTo("2024-07-02")); |
| 58 | + assertThat(resultSet.getNString(2), equalTo("0")); |
| 59 | + assertThat(resultSet.getNString(3), equalTo("350")); |
| 60 | + assertThat(resultSet.next(), equalTo(true)); |
| 61 | + assertThat(resultSet.getNString(1), equalTo("2024-07-03")); |
| 62 | + assertThat(resultSet.getNString(2), equalTo("0")); |
| 63 | + assertThat(resultSet.getNString(3), equalTo("120")); |
| 64 | + assertThat(resultSet.next(), equalTo(false)); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments