|
| 1 | +package g3701_3800.s3705_find_golden_hour_customers |
| 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.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 restaurant_orders (" + |
| 21 | + " order_id INTEGER," + |
| 22 | + " customer_id INTEGER NOT NULL," + |
| 23 | + " order_timestamp DATETIME NOT NULL," + |
| 24 | + " order_amount DECIMAL(10,2) NOT NULL," + |
| 25 | + " payment_method VARCHAR(20) NOT NULL," + |
| 26 | + " order_rating INTEGER" + |
| 27 | + ");" + |
| 28 | + "INSERT INTO restaurant_orders (order_id, customer_id, " + |
| 29 | + "order_timestamp, order_amount, payment_method, order_rating) VALUES" + |
| 30 | + "(1, 101, '2024-03-01 12:30:00', 25.50, 'card', 5)," + |
| 31 | + "(2, 101, '2024-03-02 19:15:00', 32.00, 'app', 4)," + |
| 32 | + "(3, 101, '2024-03-03 13:45:00', 28.75, 'card', 5)," + |
| 33 | + "(4, 101, '2024-03-04 20:30:00', 41.00, 'app', NULL)," + |
| 34 | + "(5, 102, '2024-03-01 11:30:00', 18.50, 'cash', 4)," + |
| 35 | + "(6, 102, '2024-03-02 12:00:00', 22.00, 'card', 3)," + |
| 36 | + "(7, 102, '2024-03-03 15:30:00', 19.75, 'cash', NULL)," + |
| 37 | + "(8, 103, '2024-03-01 19:00:00', 55.00, 'app', 5)," + |
| 38 | + "(9, 103, '2024-03-02 20:45:00', 48.50, 'app', 4)," + |
| 39 | + "(10, 103, '2024-03-03 18:30:00', 62.00, 'card', 5)," + |
| 40 | + "(11, 104, '2024-03-01 10:00:00', 15.00, 'cash', 3)," + |
| 41 | + "(12, 104, '2024-03-02 09:30:00', 18.00, 'cash', 2)," + |
| 42 | + "(13, 104, '2024-03-03 16:00:00', 20.00, 'card', 3)," + |
| 43 | + "(14, 105, '2024-03-01 12:15:00', 30.00, 'app', 4)," + |
| 44 | + "(15, 105, '2024-03-02 13:00:00', 35.50, 'app', 5)," + |
| 45 | + "(16, 105, '2024-03-03 11:45:00', 28.00, 'card', 4);" |
| 46 | + ), |
| 47 | + ], |
| 48 | +) |
| 49 | +internal class MysqlTest { |
| 50 | + @Test |
| 51 | + @Throws(SQLException::class, FileNotFoundException::class) |
| 52 | + fun testScript(@EmbeddedDatabase dataSource: DataSource) { |
| 53 | + dataSource.connection.use { connection -> |
| 54 | + connection.createStatement().use { statement -> |
| 55 | + statement.executeQuery( |
| 56 | + BufferedReader( |
| 57 | + FileReader( |
| 58 | + ( |
| 59 | + "src/main/kotlin/g3701_3800/" + |
| 60 | + "s3705_find_golden_hour_customers/" + |
| 61 | + "script.sql" |
| 62 | + ), |
| 63 | + ), |
| 64 | + ) |
| 65 | + .lines() |
| 66 | + .collect(Collectors.joining("\n")) |
| 67 | + .replace("#.*?\\r?\\n".toRegex(), ""), |
| 68 | + ).use { resultSet -> |
| 69 | + MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(true)) |
| 70 | + MatcherAssert.assertThat<String>(resultSet.getString(1), CoreMatchers.equalTo<String>("103")) |
| 71 | + MatcherAssert.assertThat<String>(resultSet.getString(2), CoreMatchers.equalTo<String>("3")) |
| 72 | + MatcherAssert.assertThat<String>(resultSet.getString(3), CoreMatchers.equalTo<String>("100")) |
| 73 | + MatcherAssert.assertThat<String>(resultSet.getString(4), CoreMatchers.equalTo<String>("4.67")) |
| 74 | + MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(true)) |
| 75 | + MatcherAssert.assertThat<String>(resultSet.getString(1), CoreMatchers.equalTo<String>("101")) |
| 76 | + MatcherAssert.assertThat<String>(resultSet.getString(2), CoreMatchers.equalTo<String>("4")) |
| 77 | + MatcherAssert.assertThat<String>(resultSet.getString(3), CoreMatchers.equalTo<String>("100")) |
| 78 | + MatcherAssert.assertThat<String>(resultSet.getString(4), CoreMatchers.equalTo<String>("4.67")) |
| 79 | + MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(true)) |
| 80 | + MatcherAssert.assertThat<String>(resultSet.getString(1), CoreMatchers.equalTo<String>("105")) |
| 81 | + MatcherAssert.assertThat<String>(resultSet.getString(2), CoreMatchers.equalTo<String>("3")) |
| 82 | + MatcherAssert.assertThat<String>(resultSet.getString(3), CoreMatchers.equalTo<String>("100")) |
| 83 | + MatcherAssert.assertThat<String>(resultSet.getString(4), CoreMatchers.equalTo<String>("4.33")) |
| 84 | + MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(false)) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments