|
| 1 | +package g0601_0700.s0607_sales_person; |
| 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 SalesPerson(sales_id INTEGER PRIMARY KEY, name VARCHAR," |
| 24 | + + " salary INTEGER, commission_rate INTEGER, hire_date DATE); " |
| 25 | + + "INSERT INTO SalesPerson(sales_id, name, salary, commission_rate, hire_date)" |
| 26 | + + " VALUES (1, 'John', 100000, 6, '2006-04-01'); " |
| 27 | + + "INSERT INTO SalesPerson(sales_id, name, salary, commission_rate, hire_date)" |
| 28 | + + " VALUES (2, 'Amy', 12000, 5, '2006-05-01'); " |
| 29 | + + "INSERT INTO SalesPerson(sales_id, name, salary, commission_rate, hire_date)" |
| 30 | + + " VALUES (3, 'Mark', 65000, 12, '2006-12-25'); " |
| 31 | + + "INSERT INTO SalesPerson(sales_id, name, salary, commission_rate, hire_date)" |
| 32 | + + " VALUES (4, 'Pam', 25000, 25, '2006-01-01'); " |
| 33 | + + "INSERT INTO SalesPerson(sales_id, name, salary, commission_rate, hire_date)" |
| 34 | + + " VALUES (5, 'Alex', 5000, 10, '2006-02-03'); " |
| 35 | + + "CREATE TABLE Company(com_id INTEGER PRIMARY KEY, name VARCHAR, city VARCHAR); " |
| 36 | + + "INSERT INTO Company(com_id, name, city) VALUES (1, 'RED', 'Boston'); " |
| 37 | + + "INSERT INTO Company(com_id, name, city) VALUES (2, 'ORANGE', 'New York'); " |
| 38 | + + "INSERT INTO Company(com_id, name, city) VALUES (3, 'YELLOW', 'Boston'); " |
| 39 | + + "INSERT INTO Company(com_id, name, city) VALUES (4, 'GREEN', 'Austin'); " |
| 40 | + + "CREATE TABLE Orders(order_id INTEGER PRIMARY KEY, order_date DATE," |
| 41 | + + " com_id INTEGER, sales_id INTEGER, amount INTEGER); " |
| 42 | + + "INSERT INTO Orders(order_id, order_date, com_id, sales_id, amount)" |
| 43 | + + " VALUES (1, '2014-01-01', 3, 4, 10000); " |
| 44 | + + "INSERT INTO Orders(order_id, order_date, com_id, sales_id, amount)" |
| 45 | + + " VALUES (2, '2014-01-02', 4, 5, 5000); " |
| 46 | + + "INSERT INTO Orders(order_id, order_date, com_id, sales_id, amount)" |
| 47 | + + " VALUES (3, '2014-01-03', 1, 1, 50000); " |
| 48 | + + "INSERT INTO Orders(order_id, order_date, com_id, sales_id, amount)" |
| 49 | + + " VALUES (4, '2014-01-04', 1, 4, 25000); ") |
| 50 | +class MysqlTest { |
| 51 | + @Test |
| 52 | + void testScript(@EmbeddedDatabase DataSource dataSource) |
| 53 | + throws SQLException, FileNotFoundException { |
| 54 | + try (final Connection connection = dataSource.getConnection()) { |
| 55 | + try (final Statement statement = connection.createStatement(); |
| 56 | + final ResultSet resultSet = |
| 57 | + statement.executeQuery( |
| 58 | + new BufferedReader( |
| 59 | + new FileReader( |
| 60 | + "src/main/java/g0601_0700/" |
| 61 | + + "s0607_sales_person" |
| 62 | + + "/script.sql")) |
| 63 | + .lines() |
| 64 | + .collect(Collectors.joining("\n")) |
| 65 | + .replaceAll("#.*?\\r?\\n", ""))) { |
| 66 | + assertThat(resultSet.next(), equalTo(true)); |
| 67 | + assertThat(resultSet.getNString(1), equalTo("Amy")); |
| 68 | + assertThat(resultSet.next(), equalTo(true)); |
| 69 | + assertThat(resultSet.getNString(1), equalTo("Mark")); |
| 70 | + assertThat(resultSet.next(), equalTo(true)); |
| 71 | + assertThat(resultSet.getNString(1), equalTo("Alex")); |
| 72 | + assertThat(resultSet.next(), equalTo(false)); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments