|
| 1 | +package g3501_3600.s3554_find_category_recommendation_pairs; |
| 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 ProductPurchases(user_id INTEGER, product_id INTEGER" |
| 24 | + + ", quantity INTEGER); " |
| 25 | + + "INSERT INTO ProductPurchases(user_id, product_id, quantity)" |
| 26 | + + " VALUES " |
| 27 | + + "(1, 101, 2), " |
| 28 | + + "(1, 102, 1), " |
| 29 | + + "(1, 201, 3), " |
| 30 | + + "(1, 301, 1), " |
| 31 | + + "(2, 101, 1), " |
| 32 | + + "(2, 102, 2), " |
| 33 | + + "(2, 103, 1), " |
| 34 | + + "(2, 201, 5), " |
| 35 | + + "(3, 101, 2), " |
| 36 | + + "(3, 103, 1), " |
| 37 | + + "(3, 301, 4), " |
| 38 | + + "(3, 401, 2), " |
| 39 | + + "(4, 101, 1), " |
| 40 | + + "(4, 201, 3), " |
| 41 | + + "(4, 301, 1), " |
| 42 | + + "(4, 401, 2), " |
| 43 | + + "(5, 102, 2), " |
| 44 | + + "(5, 103, 1), " |
| 45 | + + "(5, 201, 2), " |
| 46 | + + "(5, 202, 3);" |
| 47 | + + "CREATE TABLE ProductInfo(product_id INTEGER, category VARCHAR(255)" |
| 48 | + + ", price INTEGER); " |
| 49 | + + "INSERT INTO ProductInfo(product_id, category, price) VALUES " |
| 50 | + + "(101, 'Electronics', 100), " |
| 51 | + + "(102, 'Books', 20), " |
| 52 | + + "(103, 'Books', 35), " |
| 53 | + + "(201, 'Clothing', 45), " |
| 54 | + + "(202, 'Clothing', 60), " |
| 55 | + + "(301, 'Sports', 75), " |
| 56 | + + "(401, 'Kitchen', 50);") |
| 57 | +class MysqlTest { |
| 58 | + @Test |
| 59 | + void testScript(@EmbeddedDatabase DataSource dataSource) |
| 60 | + throws SQLException, FileNotFoundException { |
| 61 | + try (final Connection connection = dataSource.getConnection()) { |
| 62 | + try (final Statement statement = connection.createStatement(); |
| 63 | + final ResultSet resultSet = |
| 64 | + statement.executeQuery( |
| 65 | + new BufferedReader( |
| 66 | + new FileReader( |
| 67 | + "src/main/java/g3501_3600/" |
| 68 | + + "s3554_find_category_recommendation_pairs/" |
| 69 | + + "script.sql")) |
| 70 | + .lines() |
| 71 | + .collect(Collectors.joining("\n")) |
| 72 | + .replaceAll("#.*?\\r?\\n", ""))) { |
| 73 | + assertThat(resultSet.next(), equalTo(true)); |
| 74 | + assertThat(resultSet.getNString(1), equalTo("Books")); |
| 75 | + assertThat(resultSet.getNString(2), equalTo("Clothing")); |
| 76 | + assertThat(resultSet.getNString(3), equalTo("3")); |
| 77 | + assertThat(resultSet.next(), equalTo(true)); |
| 78 | + assertThat(resultSet.getNString(1), equalTo("Books")); |
| 79 | + assertThat(resultSet.getNString(2), equalTo("Electronics")); |
| 80 | + assertThat(resultSet.getNString(3), equalTo("3")); |
| 81 | + assertThat(resultSet.next(), equalTo(true)); |
| 82 | + assertThat(resultSet.getNString(1), equalTo("Clothing")); |
| 83 | + assertThat(resultSet.getNString(2), equalTo("Electronics")); |
| 84 | + assertThat(resultSet.getNString(3), equalTo("3")); |
| 85 | + assertThat(resultSet.next(), equalTo(true)); |
| 86 | + assertThat(resultSet.getNString(1), equalTo("Electronics")); |
| 87 | + assertThat(resultSet.getNString(2), equalTo("Sports")); |
| 88 | + assertThat(resultSet.getNString(3), equalTo("3")); |
| 89 | + assertThat(resultSet.next(), equalTo(false)); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments