|
| 1 | +package g3601_3700.s3657_find_loyal_customers |
| 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 customer_transactions (" + |
| 21 | + " transaction_id INT PRIMARY KEY," + |
| 22 | + " customer_id INT NOT NULL," + |
| 23 | + " transaction_date DATE NOT NULL," + |
| 24 | + " amount DECIMAL(10,2) NOT NULL," + |
| 25 | + " transaction_type ENUM('purchase', 'refund') NOT NULL" + |
| 26 | + ");" + |
| 27 | + "INSERT INTO customer_transactions" + |
| 28 | + "(transaction_id, customer_id, transaction_date, amount, transaction_type)" + |
| 29 | + "VALUES" + |
| 30 | + "(1, 101, '2024-01-05', 150.00, 'purchase')," + |
| 31 | + "(2, 101, '2024-01-15', 200.00, 'purchase')," + |
| 32 | + "(3, 101, '2024-02-10', 180.00, 'purchase')," + |
| 33 | + "(4, 101, '2024-02-20', 250.00, 'purchase')," + |
| 34 | + "(5, 102, '2024-01-10', 100.00, 'purchase')," + |
| 35 | + "(6, 102, '2024-01-12', 120.00, 'purchase')," + |
| 36 | + "(7, 102, '2024-01-15', 80.00, 'refund')," + |
| 37 | + "(8, 102, '2024-01-18', 90.00, 'refund')," + |
| 38 | + "(9, 102, '2024-02-15', 130.00, 'purchase')," + |
| 39 | + "(10, 103, '2024-01-01', 500.00, 'purchase')," + |
| 40 | + "(11, 103, '2024-01-02', 450.00, 'purchase')," + |
| 41 | + "(12, 103, '2024-01-03', 400.00, 'purchase')," + |
| 42 | + "(13, 104, '2024-01-01', 200.00, 'purchase')," + |
| 43 | + "(14, 104, '2024-02-01', 250.00, 'purchase')," + |
| 44 | + "(15, 104, '2024-02-15', 300.00, 'purchase')," + |
| 45 | + "(16, 104, '2024-03-01', 350.00, 'purchase')," + |
| 46 | + "(17, 104, '2024-03-10', 280.00, 'purchase')," + |
| 47 | + "(18, 104, '2024-03-15', 100.00, 'refund');" |
| 48 | + ), |
| 49 | + ], |
| 50 | +) |
| 51 | +internal class MysqlTest { |
| 52 | + @Test |
| 53 | + @Throws(SQLException::class, FileNotFoundException::class) |
| 54 | + fun testScript(@EmbeddedDatabase dataSource: DataSource) { |
| 55 | + dataSource.connection.use { connection -> |
| 56 | + connection.createStatement().use { statement -> |
| 57 | + statement.executeQuery( |
| 58 | + BufferedReader( |
| 59 | + FileReader( |
| 60 | + ( |
| 61 | + "src/main/kotlin/g3601_3700/" + |
| 62 | + "s3657_find_loyal_customers/" + |
| 63 | + "script.sql" |
| 64 | + ), |
| 65 | + ), |
| 66 | + ) |
| 67 | + .lines() |
| 68 | + .collect(Collectors.joining("\n")) |
| 69 | + .replace("#.*?\\r?\\n".toRegex(), ""), |
| 70 | + ).use { resultSet -> |
| 71 | + assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true)) |
| 72 | + assertThat<String>(resultSet.getNString(1), equalTo<String>("101")) |
| 73 | + assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(true)) |
| 74 | + assertThat<String>(resultSet.getNString(1), equalTo<String>("104")) |
| 75 | + assertThat<Boolean>(resultSet.next(), equalTo<Boolean>(false)) |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments