|  | 
|  | 1 | +package g3701_3800.s3716_find_churn_risk_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 subscription_events (" + | 
|  | 21 | +                "    event_id INTEGER PRIMARY KEY," + | 
|  | 22 | +                "    user_id INTEGER NOT NULL," + | 
|  | 23 | +                "    event_date DATE NOT NULL," + | 
|  | 24 | +                "    event_type VARCHAR(20) NOT NULL," + | 
|  | 25 | +                "    plan_name VARCHAR(20)," + | 
|  | 26 | +                "    monthly_amount DECIMAL(10,2) NOT NULL" + | 
|  | 27 | +                ");" + | 
|  | 28 | +                "INSERT INTO subscription_events (event_id, user_id, event_date, " + | 
|  | 29 | +                "event_type, plan_name, monthly_amount) VALUES" + | 
|  | 30 | +                "(1, 501, '2024-01-01', 'start',     'premium', 29.99)," + | 
|  | 31 | +                "(2, 501, '2024-02-15', 'downgrade', 'standard', 19.99)," + | 
|  | 32 | +                "(3, 501, '2024-03-20', 'downgrade', 'basic', 9.99)," + | 
|  | 33 | +                "(4, 502, '2024-01-05', 'start',     'standard', 19.99)," + | 
|  | 34 | +                "(5, 502, '2024-02-10', 'upgrade',   'premium', 29.99)," + | 
|  | 35 | +                "(6, 502, '2024-03-15', 'downgrade', 'basic', 9.99)," + | 
|  | 36 | +                "(7, 503, '2024-01-10', 'start',     'basic', 9.99)," + | 
|  | 37 | +                "(8, 503, '2024-02-20', 'upgrade',   'standard', 19.99)," + | 
|  | 38 | +                "(9, 503, '2024-03-25', 'upgrade',   'premium', 29.99)," + | 
|  | 39 | +                "(10, 504, '2024-01-15', 'start',    'premium', 29.99)," + | 
|  | 40 | +                "(11, 504, '2024-03-01', 'downgrade','standard', 19.99)," + | 
|  | 41 | +                "(12, 504, '2024-03-30', 'cancel',   NULL, 0.00)," + | 
|  | 42 | +                "(13, 505, '2024-02-01', 'start',    'basic', 9.99)," + | 
|  | 43 | +                "(14, 505, '2024-02-28', 'upgrade',  'standard', 19.99)," + | 
|  | 44 | +                "(15, 506, '2024-01-20', 'start',    'premium', 29.99)," + | 
|  | 45 | +                "(16, 506, '2024-03-10', 'downgrade','basic', 9.99);" + | 
|  | 46 | +                "" | 
|  | 47 | +            ), | 
|  | 48 | +    ], | 
|  | 49 | +) | 
|  | 50 | +internal class MysqlTest { | 
|  | 51 | +    @Test | 
|  | 52 | +    @Throws(SQLException::class, FileNotFoundException::class) | 
|  | 53 | +    fun testScript(@EmbeddedDatabase dataSource: DataSource) { | 
|  | 54 | +        dataSource.connection.use { connection -> | 
|  | 55 | +            connection.createStatement().use { statement -> | 
|  | 56 | +                statement.executeQuery( | 
|  | 57 | +                    BufferedReader( | 
|  | 58 | +                        FileReader( | 
|  | 59 | +                            ( | 
|  | 60 | +                                "src/main/kotlin/g3701_3800/" + | 
|  | 61 | +                                    "s3716_find_churn_risk_customers/" + | 
|  | 62 | +                                    "script.sql" | 
|  | 63 | +                                ), | 
|  | 64 | +                        ), | 
|  | 65 | +                    ) | 
|  | 66 | +                        .lines() | 
|  | 67 | +                        .collect(Collectors.joining("\n")) | 
|  | 68 | +                        .replace("#.*?\\r?\\n".toRegex(), ""), | 
|  | 69 | +                ).use { resultSet -> | 
|  | 70 | +                    MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(true)) | 
|  | 71 | +                    MatcherAssert.assertThat<String>(resultSet.getString(1), CoreMatchers.equalTo<String>("501")) | 
|  | 72 | +                    MatcherAssert.assertThat<String>(resultSet.getString(2), CoreMatchers.equalTo<String>("basic")) | 
|  | 73 | +                    MatcherAssert.assertThat<String>(resultSet.getString(3), CoreMatchers.equalTo<String>("9.99")) | 
|  | 74 | +                    MatcherAssert.assertThat<String>(resultSet.getString(4), CoreMatchers.equalTo<String>("29.99")) | 
|  | 75 | +                    MatcherAssert.assertThat<String>(resultSet.getString(5), CoreMatchers.equalTo<String>("79")) | 
|  | 76 | +                    MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(true)) | 
|  | 77 | +                    MatcherAssert.assertThat<String>(resultSet.getString(1), CoreMatchers.equalTo<String>("502")) | 
|  | 78 | +                    MatcherAssert.assertThat<String>(resultSet.getString(2), CoreMatchers.equalTo<String>("basic")) | 
|  | 79 | +                    MatcherAssert.assertThat<String>(resultSet.getString(3), CoreMatchers.equalTo<String>("9.99")) | 
|  | 80 | +                    MatcherAssert.assertThat<String>(resultSet.getString(4), CoreMatchers.equalTo<String>("29.99")) | 
|  | 81 | +                    MatcherAssert.assertThat<String>(resultSet.getString(5), CoreMatchers.equalTo<String>("70")) | 
|  | 82 | +                    MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(false)) | 
|  | 83 | +                } | 
|  | 84 | +            } | 
|  | 85 | +        } | 
|  | 86 | +    } | 
|  | 87 | +} | 
0 commit comments