|
| 1 | +package g1001_1100.s1068_product_sales_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 Sales(sale_id INTEGER, product_id INTEGER," |
| 24 | + + " sale_year INTEGER, quantity INTEGER, price INTEGER); " |
| 25 | + + "INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)" |
| 26 | + + " VALUES (1, 100, 2008, 10, 5000); " |
| 27 | + + "INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)" |
| 28 | + + " VALUES (2, 100, 2009, 12, 5000); " |
| 29 | + + "INSERT INTO Sales(sale_id, product_id, sale_year, quantity, price)" |
| 30 | + + " VALUES (7, 200, 2011, 15, 9000); " |
| 31 | + + "CREATE TABLE Product(product_id INTEGER, product_name VARCHAR); " |
| 32 | + + "INSERT INTO Product(product_id, product_name)" |
| 33 | + + " VALUES (100, 'Nokia'); " |
| 34 | + + "INSERT INTO Product(product_id, product_name)" |
| 35 | + + " VALUES (200, 'Apple'); " |
| 36 | + + "INSERT INTO Product(product_id, product_name)" |
| 37 | + + " VALUES (300, 'Samsung'); ") |
| 38 | +class MysqlTest { |
| 39 | + @Test |
| 40 | + void testScript(@EmbeddedDatabase DataSource dataSource) |
| 41 | + throws SQLException, FileNotFoundException { |
| 42 | + try (final Connection connection = dataSource.getConnection()) { |
| 43 | + try (final Statement statement = connection.createStatement(); |
| 44 | + final ResultSet resultSet = |
| 45 | + statement.executeQuery( |
| 46 | + new BufferedReader( |
| 47 | + new FileReader( |
| 48 | + "src/main/java/g1001_1100/" |
| 49 | + + "s1068_product_sales_analysis_i/script.sql")) |
| 50 | + .lines() |
| 51 | + .collect(Collectors.joining("\n")) |
| 52 | + .replaceAll("#.*?\\r?\\n", ""))) { |
| 53 | + assertThat(resultSet.next(), equalTo(true)); |
| 54 | + assertThat(resultSet.getNString(1), equalTo("Nokia")); |
| 55 | + assertThat(resultSet.getInt(2), equalTo(2008)); |
| 56 | + assertThat(resultSet.getInt(3), equalTo(5000)); |
| 57 | + assertThat(resultSet.next(), equalTo(true)); |
| 58 | + assertThat(resultSet.getNString(1), equalTo("Nokia")); |
| 59 | + assertThat(resultSet.getInt(2), equalTo(2009)); |
| 60 | + assertThat(resultSet.getInt(3), equalTo(5000)); |
| 61 | + assertThat(resultSet.next(), equalTo(true)); |
| 62 | + assertThat(resultSet.getNString(1), equalTo("Apple")); |
| 63 | + assertThat(resultSet.getInt(2), equalTo(2011)); |
| 64 | + assertThat(resultSet.getInt(3), equalTo(9000)); |
| 65 | + assertThat(resultSet.next(), equalTo(false)); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments