|
| 1 | +package g0601_0700.s0601_human_traffic_of_stadium; |
| 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 Stadium(id INTEGER, visit_date DATE PRIMARY KEY, people INTEGER); " |
| 24 | + + "INSERT INTO Stadium(id, visit_date, people)" |
| 25 | + + " VALUES (1, '2017-01-01', 10); " |
| 26 | + + "INSERT INTO Stadium(id, visit_date, people)" |
| 27 | + + " VALUES (2, '2017-01-02', 109); " |
| 28 | + + "INSERT INTO Stadium(id, visit_date, people)" |
| 29 | + + " VALUES (3, '2017-01-03', 150); " |
| 30 | + + "INSERT INTO Stadium(id, visit_date, people)" |
| 31 | + + " VALUES (4, '2017-01-04', 99); " |
| 32 | + + "INSERT INTO Stadium(id, visit_date, people)" |
| 33 | + + " VALUES (5, '2017-01-05', 145); " |
| 34 | + + "INSERT INTO Stadium(id, visit_date, people)" |
| 35 | + + " VALUES (6, '2017-01-06', 1455); " |
| 36 | + + "INSERT INTO Stadium(id, visit_date, people)" |
| 37 | + + " VALUES (7, '2017-01-07', 199); " |
| 38 | + + "INSERT INTO Stadium(id, visit_date, people)" |
| 39 | + + " VALUES (8, '2017-01-09', 188); ") |
| 40 | +class MysqlTest { |
| 41 | + @Test |
| 42 | + void testScript(@EmbeddedDatabase DataSource dataSource) |
| 43 | + throws SQLException, FileNotFoundException { |
| 44 | + try (final Connection connection = dataSource.getConnection()) { |
| 45 | + try (final Statement statement = connection.createStatement(); |
| 46 | + final ResultSet resultSet = |
| 47 | + statement.executeQuery( |
| 48 | + new BufferedReader( |
| 49 | + new FileReader( |
| 50 | + "src/main/java/g0601_0700/" |
| 51 | + + "s0601_human_traffic" |
| 52 | + + "_of_stadium" |
| 53 | + + "/script.sql")) |
| 54 | + .lines() |
| 55 | + .collect(Collectors.joining("\n")) |
| 56 | + .replaceAll("#.*?\\r?\\n", ""))) { |
| 57 | + assertThat(resultSet.next(), equalTo(true)); |
| 58 | + assertThat(resultSet.getInt(1), equalTo(5)); |
| 59 | + assertThat(resultSet.getDate(2).toString(), equalTo("2017-01-05")); |
| 60 | + assertThat(resultSet.getInt(3), equalTo(145)); |
| 61 | + assertThat(resultSet.next(), equalTo(true)); |
| 62 | + assertThat(resultSet.getInt(1), equalTo(6)); |
| 63 | + assertThat(resultSet.getDate(2).toString(), equalTo("2017-01-06")); |
| 64 | + assertThat(resultSet.getInt(3), equalTo(1455)); |
| 65 | + assertThat(resultSet.next(), equalTo(true)); |
| 66 | + assertThat(resultSet.getInt(1), equalTo(7)); |
| 67 | + assertThat(resultSet.getDate(2).toString(), equalTo("2017-01-07")); |
| 68 | + assertThat(resultSet.getInt(3), equalTo(199)); |
| 69 | + assertThat(resultSet.next(), equalTo(true)); |
| 70 | + assertThat(resultSet.getInt(1), equalTo(8)); |
| 71 | + assertThat(resultSet.getDate(2).toString(), equalTo("2017-01-09")); |
| 72 | + assertThat(resultSet.getInt(3), equalTo(188)); |
| 73 | + assertThat(resultSet.next(), equalTo(false)); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments