|
| 1 | +package g3501_3600.s3521_find_product_recommendation_pairs |
| 2 | + |
| 3 | +import org.hamcrest.CoreMatchers |
| 4 | +import org.hamcrest.MatcherAssert |
| 5 | +import org.junit.jupiter.api.Test |
| 6 | +import org.zapodot.junit.db.annotations.EmbeddedDatabase |
| 7 | +import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest |
| 8 | +import org.zapodot.junit.db.common.CompatibilityMode |
| 9 | +import java.io.BufferedReader |
| 10 | +import java.io.FileNotFoundException |
| 11 | +import java.io.FileReader |
| 12 | +import java.sql.ResultSet |
| 13 | +import java.sql.SQLException |
| 14 | +import java.util.stream.Collectors |
| 15 | +import javax.sql.DataSource |
| 16 | + |
| 17 | +@EmbeddedDatabaseTest( |
| 18 | + compatibilityMode = CompatibilityMode.MySQL, |
| 19 | + initialSqls = [ |
| 20 | + ( |
| 21 | + " CREATE TABLE ProductPurchases (" + |
| 22 | + " user_id INT," + |
| 23 | + " product_id INT," + |
| 24 | + " quantity INT" + |
| 25 | + ");" + |
| 26 | + "CREATE TABLE ProductInfo (" + |
| 27 | + " product_id INT," + |
| 28 | + " category VARCHAR(100)," + |
| 29 | + " price BIGINT" + |
| 30 | + ");" + |
| 31 | + "INSERT INTO ProductPurchases (user_id, product_id, quantity)" + |
| 32 | + "VALUES" + |
| 33 | + " (1 , 101 , 2)," + |
| 34 | + " (1 , 102 , 1 )," + |
| 35 | + " (1 , 103 , 3 )," + |
| 36 | + " (2 , 101 , 1 )," + |
| 37 | + " (2 , 102 , 5 )," + |
| 38 | + " (2 , 104 , 1 )," + |
| 39 | + " (3 , 101 , 2 )," + |
| 40 | + " (3 , 103 , 1 )," + |
| 41 | + " (3 , 105 , 4 )," + |
| 42 | + " (4 , 101 , 1 )," + |
| 43 | + " (4 , 102 , 1 )," + |
| 44 | + " (4 , 103 , 2 )," + |
| 45 | + " (4 , 104 , 3 )," + |
| 46 | + " (5 , 102 , 2 )," + |
| 47 | + " (5 , 104 , 1 );" + |
| 48 | + "INSERT INTO ProductInfo (product_id, category, price)" + |
| 49 | + "VALUES" + |
| 50 | + " (101 , 'Electronics' , 100)," + |
| 51 | + " (102 , 'Books' , 20)," + |
| 52 | + " (103 , 'Clothing' , 35)," + |
| 53 | + " (104 , 'Kitchen' , 50)," + |
| 54 | + " (105 , 'Sports' , 75);" |
| 55 | + ), |
| 56 | + ], |
| 57 | +) |
| 58 | +internal class MysqlTest { |
| 59 | + @Test |
| 60 | + @Throws(SQLException::class, FileNotFoundException::class) |
| 61 | + fun testScript(@EmbeddedDatabase dataSource: DataSource) { |
| 62 | + dataSource.connection.use { connection -> |
| 63 | + connection.createStatement().use { statement -> |
| 64 | + statement.executeQuery( |
| 65 | + BufferedReader( |
| 66 | + FileReader( |
| 67 | + ( |
| 68 | + "src/main/kotlin/g3501_3600/" + |
| 69 | + "s3521_find_product_recommendation_pairs/" + |
| 70 | + "script.sql" |
| 71 | + ), |
| 72 | + ), |
| 73 | + ) |
| 74 | + .lines() |
| 75 | + .collect(Collectors.joining("\n")) |
| 76 | + .replace("#.*\\r\\n".toRegex(), ""), |
| 77 | + ).use { resultSet -> |
| 78 | + checkRow(resultSet, arrayOf<String>("101", "102", "Electronics", "Books", "3")) |
| 79 | + checkRow(resultSet, arrayOf<String>("101", "103", "Electronics", "Clothing", "3")) |
| 80 | + checkRow(resultSet, arrayOf<String>("102", "104", "Books", "Clothing", "3")) |
| 81 | + MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(false)) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Throws(SQLException::class) |
| 88 | + private fun checkRow(resultSet: ResultSet, values: Array<String>) { |
| 89 | + MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(true)) |
| 90 | + MatcherAssert.assertThat<String>(resultSet.getNString(1), CoreMatchers.equalTo<String>(values[0])) |
| 91 | + MatcherAssert.assertThat<String>(resultSet.getNString(2), CoreMatchers.equalTo<String>(values[1])) |
| 92 | + MatcherAssert.assertThat<String>(resultSet.getNString(3), CoreMatchers.equalTo<String>(values[2])) |
| 93 | + } |
| 94 | +} |
0 commit comments