|
| 1 | +package g0501_0600.s0511_game_play_analysis_i; |
| 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 Activity(player_id INTEGER, device_id INTEGER," |
| 24 | + + " event_date DATE, games_played INTEGER); " |
| 25 | + + "INSERT INTO Activity(player_id, device_id, event_date, games_played)" |
| 26 | + + " VALUES (1, 2, '2016-03-01', 5); " |
| 27 | + + "INSERT INTO Activity(player_id, device_id, event_date, games_played)" |
| 28 | + + " VALUES (1, 2, '2016-05-02', 6); " |
| 29 | + + "INSERT INTO Activity(player_id, device_id, event_date, games_played)" |
| 30 | + + " VALUES (2, 3, '2017-06-25', 1); " |
| 31 | + + "INSERT INTO Activity(player_id, device_id, event_date, games_played)" |
| 32 | + + " VALUES (3, 1, '2016-03-02', 0); " |
| 33 | + + "INSERT INTO Activity(player_id, device_id, event_date, games_played)" |
| 34 | + + " VALUES (3, 4, '2018-07-03', 5); ") |
| 35 | +class MysqlTest { |
| 36 | + @Test |
| 37 | + void testScript(@EmbeddedDatabase DataSource dataSource) |
| 38 | + throws SQLException, FileNotFoundException { |
| 39 | + try (final Connection connection = dataSource.getConnection()) { |
| 40 | + try (final Statement statement = connection.createStatement(); |
| 41 | + final ResultSet resultSet = |
| 42 | + statement.executeQuery( |
| 43 | + new BufferedReader( |
| 44 | + new FileReader( |
| 45 | + "src/main/java/g0501_0600/" |
| 46 | + + "s0511_game_play_analysis_i/script.sql")) |
| 47 | + .lines() |
| 48 | + .collect(Collectors.joining("\n")) |
| 49 | + .replaceAll("#.*?\\r?\\n", ""))) { |
| 50 | + assertThat(resultSet.next(), equalTo(true)); |
| 51 | + assertThat(resultSet.getInt(1), equalTo(1)); |
| 52 | + assertThat(resultSet.getDate(2).toString(), equalTo("2016-03-01")); |
| 53 | + assertThat(resultSet.next(), equalTo(true)); |
| 54 | + assertThat(resultSet.getInt(1), equalTo(2)); |
| 55 | + assertThat(resultSet.getDate(2).toString(), equalTo("2017-06-25")); |
| 56 | + assertThat(resultSet.next(), equalTo(true)); |
| 57 | + assertThat(resultSet.getInt(1), equalTo(3)); |
| 58 | + assertThat(resultSet.getDate(2).toString(), equalTo("2016-03-02")); |
| 59 | + assertThat(resultSet.next(), equalTo(false)); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments