| 
 | 1 | +package g3401_3500.s3497_analyze_subscription_conversion  | 
 | 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 UserActivity (" +  | 
 | 22 | +                "    user_id INT," +  | 
 | 23 | +                "    activity_date date," +  | 
 | 24 | +                "    activity_type VARCHAR(100)," +  | 
 | 25 | +                "    activity_duration INT" +  | 
 | 26 | +                ");" +  | 
 | 27 | +                "INSERT INTO UserActivity (user_id, activity_date, activity_type, activity_duration)" +  | 
 | 28 | +                "VALUES" +  | 
 | 29 | +                "    (1, '2023-01-01', 'free_trial', 45)," +  | 
 | 30 | +                "    (1, '2023-01-02', 'free_trial', 30)," +  | 
 | 31 | +                "    (1, '2023-01-05', 'free_trial', 60)," +  | 
 | 32 | +                "    (1, '2023-01-10', 'paid', 75)," +  | 
 | 33 | +                "    (1, '2023-01-12', 'paid', 90)," +  | 
 | 34 | +                "    (1, '2023-01-15', 'paid', 65)," +  | 
 | 35 | +                "    (2, '2023-02-01', 'free_trial', 55)," +  | 
 | 36 | +                "    (2, '2023-02-03', 'free_trial', 25)," +  | 
 | 37 | +                "    (2, '2023-02-07', 'free_trial', 50)," +  | 
 | 38 | +                "    (2, '2023-02-10', 'cancelled', 0)," +  | 
 | 39 | +                "    (3, '2023-03-05', 'free_trial', 70)," +  | 
 | 40 | +                "    (3, '2023-03-06', 'free_trial', 60)," +  | 
 | 41 | +                "    (3, '2023-03-08', 'free_trial', 80)," +  | 
 | 42 | +                "    (3, '2023-03-12', 'paid', 50)," +  | 
 | 43 | +                "    (3, '2023-03-15', 'paid', 55)," +  | 
 | 44 | +                "    (3, '2023-03-20', 'paid', 85)," +  | 
 | 45 | +                "    (4, '2023-04-01', 'free_trial', 40)," +  | 
 | 46 | +                "    (4, '2023-04-03', 'free_trial', 35)," +  | 
 | 47 | +                "    (4, '2023-04-05', 'paid', 45)," +  | 
 | 48 | +                "    (4, '2023-04-07', 'cancelled', 0);"  | 
 | 49 | +            ),  | 
 | 50 | +    ],  | 
 | 51 | +)  | 
 | 52 | +internal class MysqlTest {  | 
 | 53 | +    @Test  | 
 | 54 | +    @Throws(SQLException::class, FileNotFoundException::class)  | 
 | 55 | +    fun testScript(@EmbeddedDatabase dataSource: DataSource) {  | 
 | 56 | +        dataSource.connection.use { connection ->  | 
 | 57 | +            connection.createStatement().use { statement ->  | 
 | 58 | +                statement.executeQuery(  | 
 | 59 | +                    BufferedReader(  | 
 | 60 | +                        FileReader(  | 
 | 61 | +                            (  | 
 | 62 | +                                "src/main/kotlin/g3401_3500/" +  | 
 | 63 | +                                    "s3497_analyze_subscription_conversion/" +  | 
 | 64 | +                                    "script.sql"  | 
 | 65 | +                                ),  | 
 | 66 | +                        ),  | 
 | 67 | +                    )  | 
 | 68 | +                        .lines()  | 
 | 69 | +                        .collect(Collectors.joining("\n"))  | 
 | 70 | +                        .replace("#.*?\\r?\\n".toRegex(), ""),  | 
 | 71 | +                ).use { resultSet ->  | 
 | 72 | +                    checkRow(resultSet, arrayOf<String>("1", "45.0", "76.67"))  | 
 | 73 | +                    checkRow(resultSet, arrayOf<String>("3", "70.0", "63.33"))  | 
 | 74 | +                    checkRow(resultSet, arrayOf<String>("4", "37.5", "45.0"))  | 
 | 75 | +                    MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(false))  | 
 | 76 | +                }  | 
 | 77 | +            }  | 
 | 78 | +        }  | 
 | 79 | +    }  | 
 | 80 | + | 
 | 81 | +    @Throws(SQLException::class)  | 
 | 82 | +    private fun checkRow(resultSet: ResultSet, values: Array<String>) {  | 
 | 83 | +        MatcherAssert.assertThat<Boolean>(resultSet.next(), CoreMatchers.equalTo<Boolean>(true))  | 
 | 84 | +        MatcherAssert.assertThat<String>(resultSet.getNString(1), CoreMatchers.equalTo<String>(values[0]))  | 
 | 85 | +        MatcherAssert.assertThat<String>(resultSet.getNString(2), CoreMatchers.equalTo<String>(values[1]))  | 
 | 86 | +        MatcherAssert.assertThat<String>(resultSet.getNString(3), CoreMatchers.equalTo<String>(values[2]))  | 
 | 87 | +    }  | 
 | 88 | +}  | 
0 commit comments