|
| 1 | +package g3401_3500.s3465_find_products_with_valid_serial_numbers |
| 2 | + |
| 3 | +import org.hamcrest.CoreMatchers.equalTo |
| 4 | +import org.hamcrest.MatcherAssert.assertThat |
| 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.SQLException |
| 13 | +import java.util.stream.Collectors |
| 14 | +import javax.sql.DataSource |
| 15 | + |
| 16 | +@EmbeddedDatabaseTest( |
| 17 | + compatibilityMode = CompatibilityMode.MySQL, |
| 18 | + initialSqls = [ |
| 19 | + ( |
| 20 | + " CREATE TABLE products (" + |
| 21 | + " product_id INT," + |
| 22 | + " product_name VARCHAR(50)," + |
| 23 | + " description VARCHAR(100)" + |
| 24 | + ");" + |
| 25 | + "insert into products (product_id, product_name, description) values " + |
| 26 | + "(1, 'Widget A', 'This is a sample product with SN1234-5678');" + |
| 27 | + "insert into products (product_id, product_name, description) values " + |
| 28 | + "(2, 'Widget B', 'A product with serial SN9876-1234 in the description');" + |
| 29 | + "insert into products (product_id, product_name, description) values " + |
| 30 | + "(3, 'Widget C', 'Product SN1234-56789 is available now');" + |
| 31 | + "insert into products (product_id, product_name, description) values " + |
| 32 | + "(4, 'Widget D', 'No serial number here');" + |
| 33 | + "insert into products (product_id, product_name, description) values " + |
| 34 | + "(5, 'Widget E', 'Check out SN4321-8765 in this description');" |
| 35 | + ), |
| 36 | + ], |
| 37 | +) |
| 38 | +internal class MysqlTest { |
| 39 | + @Test |
| 40 | + @Throws(SQLException::class, FileNotFoundException::class) |
| 41 | + fun testScript(@EmbeddedDatabase dataSource: DataSource) { |
| 42 | + dataSource.connection.use { connection -> |
| 43 | + connection.createStatement().use { statement -> |
| 44 | + statement.executeQuery( |
| 45 | + BufferedReader( |
| 46 | + FileReader( |
| 47 | + ( |
| 48 | + "src/main/kotlin/g3401_3500/" + |
| 49 | + "s3465_find_products_with_valid_serial_numbers/" + |
| 50 | + "script.sql" |
| 51 | + ), |
| 52 | + ), |
| 53 | + ) |
| 54 | + .lines() |
| 55 | + .collect(Collectors.joining("\n")) |
| 56 | + .replace("#.*?\\r?\\n".toRegex(), ""), |
| 57 | + ).use { resultSet -> |
| 58 | + assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true)) |
| 59 | + assertThat<String>(resultSet.getNString(1), equalTo<String>("1")) |
| 60 | + assertThat<String>( |
| 61 | + resultSet.getNString(2), |
| 62 | + equalTo<String>("Widget A"), |
| 63 | + ) |
| 64 | + assertThat<String>( |
| 65 | + resultSet.getNString(3), |
| 66 | + equalTo<String>("This is a sample product with SN1234-5678"), |
| 67 | + ) |
| 68 | + assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true)) |
| 69 | + assertThat<String>(resultSet.getNString(1), equalTo<String>("2")) |
| 70 | + assertThat<String>( |
| 71 | + resultSet.getNString(2), |
| 72 | + equalTo<String>("Widget B"), |
| 73 | + ) |
| 74 | + assertThat<String>( |
| 75 | + resultSet.getNString(3), |
| 76 | + equalTo<String>("A product with serial SN9876-1234 in the description"), |
| 77 | + ) |
| 78 | + assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true)) |
| 79 | + assertThat<String>(resultSet.getNString(1), equalTo<String>("5")) |
| 80 | + assertThat<String>( |
| 81 | + resultSet.getNString(2), |
| 82 | + equalTo<String>("Widget E"), |
| 83 | + ) |
| 84 | + assertThat<String>( |
| 85 | + resultSet.getNString(3), |
| 86 | + equalTo<String>("Check out SN4321-8765 in this description"), |
| 87 | + ) |
| 88 | + assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(false)) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments