|
| 1 | +package g0201_0300.s0262_trips_and_users; |
| 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 Trips(id INTEGER PRIMARY KEY, client_id INTEGER, " |
| 24 | + + "driver_id INTEGER, city_id INTEGER, status VARCHAR, request_at DATE); " |
| 25 | + + "INSERT INTO Trips(id, client_id, driver_id, city_id, " |
| 26 | + + "status, request_at) VALUES (1, 1, 10, 1, 'completed', '2013-10-01'); " |
| 27 | + + "INSERT INTO Trips(id, client_id, driver_id, city_id, " |
| 28 | + + "status, request_at) VALUES (2, 2, 11, 1, 'cancelled_by_driver', '2013-10-01'); " |
| 29 | + + "INSERT INTO Trips(id, client_id, driver_id, city_id, " |
| 30 | + + "status, request_at) VALUES (3, 3, 12, 6, 'completed', '2013-10-01'); " |
| 31 | + + "INSERT INTO Trips(id, client_id, driver_id, city_id, " |
| 32 | + + "status, request_at) VALUES (4, 4, 13, 6, 'cancelled_by_driver', '2013-10-01'); " |
| 33 | + + "INSERT INTO Trips(id, client_id, driver_id, city_id, " |
| 34 | + + "status, request_at) VALUES (5, 1, 10, 1, 'completed', '2013-10-02'); " |
| 35 | + + "INSERT INTO Trips(id, client_id, driver_id, city_id, " |
| 36 | + + "status, request_at) VALUES (6, 2, 11, 6, 'completed', '2013-10-02'); " |
| 37 | + + "INSERT INTO Trips(id, client_id, driver_id, city_id, " |
| 38 | + + "status, request_at) VALUES (7, 3, 12, 6, 'completed', '2013-10-02'); " |
| 39 | + + "INSERT INTO Trips(id, client_id, driver_id, city_id, " |
| 40 | + + "status, request_at) VALUES (8, 2, 12, 12, 'completed', '2013-10-03'); " |
| 41 | + + "INSERT INTO Trips(id, client_id, driver_id, city_id, " |
| 42 | + + "status, request_at) VALUES (9, 3, 10, 12, 'completed', '2013-10-03'); " |
| 43 | + + "INSERT INTO Trips(id, client_id, driver_id, city_id, " |
| 44 | + + "status, request_at) VALUES (10, 4, 13, 12, 'cancelled_by_driver', '2013-10-03'); ", |
| 45 | + "CREATE TABLE Users(users_id INTEGER, banned VARCHAR, role VARCHAR); " |
| 46 | + + "INSERT INTO Users(users_id, banned, role) VALUES (1, 'No', 'client'); " |
| 47 | + + "INSERT INTO Users(users_id, banned, role) VALUES (2, 'No', 'client'); " |
| 48 | + + "INSERT INTO Users(users_id, banned, role) VALUES (3, 'No', 'client'); " |
| 49 | + + "INSERT INTO Users(users_id, banned, role) VALUES (4, 'No', 'client'); " |
| 50 | + + "INSERT INTO Users(users_id, banned, role) VALUES (10, 'No', 'driver'); " |
| 51 | + + "INSERT INTO Users(users_id, banned, role) VALUES (11, 'No', 'driver'); " |
| 52 | + + "INSERT INTO Users(users_id, banned, role) VALUES (12, 'No', 'driver'); " |
| 53 | + + "INSERT INTO Users(users_id, banned, role) VALUES (13, 'No', 'driver'); " |
| 54 | + }) |
| 55 | +class MysqlTest { |
| 56 | + @Test |
| 57 | + void testScript(@EmbeddedDatabase DataSource dataSource) |
| 58 | + throws SQLException, FileNotFoundException { |
| 59 | + try (final Connection connection = dataSource.getConnection()) { |
| 60 | + try (final Statement statement = connection.createStatement(); |
| 61 | + final ResultSet resultSet = |
| 62 | + statement.executeQuery( |
| 63 | + new BufferedReader( |
| 64 | + new FileReader( |
| 65 | + "src/main/java/g0201_0300/" |
| 66 | + + "s0262_trips_and_users/script.sql")) |
| 67 | + .lines() |
| 68 | + .collect(Collectors.joining("\n")) |
| 69 | + .replaceAll("#.*?\\r?\\n", ""))) { |
| 70 | + assertThat(resultSet.next(), equalTo(true)); |
| 71 | + assertThat(resultSet.getDate(1).toString(), equalTo("2013-10-01")); |
| 72 | + assertThat(resultSet.getFloat(2), equalTo(0.5F)); |
| 73 | + assertThat(resultSet.next(), equalTo(true)); |
| 74 | + assertThat(resultSet.getDate(1).toString(), equalTo("2013-10-02")); |
| 75 | + assertThat(resultSet.getFloat(2), equalTo(0.0F)); |
| 76 | + assertThat(resultSet.next(), equalTo(true)); |
| 77 | + assertThat(resultSet.getDate(1).toString(), equalTo("2013-10-03")); |
| 78 | + assertThat(resultSet.getFloat(2), equalTo(0.33F)); |
| 79 | + assertThat(resultSet.next(), equalTo(false)); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments