|
| 1 | +package g3401_3500.s3497_analyze_subscription_conversion; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.FileNotFoundException; |
| 8 | +import java.io.FileReader; |
| 9 | +import java.sql.Connection; |
| 10 | +import java.sql.ResultSet; |
| 11 | +import java.sql.SQLException; |
| 12 | +import java.sql.Statement; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import javax.sql.DataSource; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.zapodot.junit.db.annotations.EmbeddedDatabase; |
| 17 | +import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest; |
| 18 | +import org.zapodot.junit.db.common.CompatibilityMode; |
| 19 | + |
| 20 | +@EmbeddedDatabaseTest( |
| 21 | + compatibilityMode = CompatibilityMode.MySQL, |
| 22 | + initialSqls = |
| 23 | + " CREATE TABLE UserActivity (" |
| 24 | + + " user_id INT," |
| 25 | + + " activity_date date," |
| 26 | + + " activity_type VARCHAR(100)," |
| 27 | + + " activity_duration INT" |
| 28 | + + ");" |
| 29 | + + "INSERT INTO UserActivity (user_id, activity_date, activity_type, activity_duration)" |
| 30 | + + "VALUES" |
| 31 | + + " (1, '2023-01-01', 'free_trial', 45)," |
| 32 | + + " (1, '2023-01-02', 'free_trial', 30)," |
| 33 | + + " (1, '2023-01-05', 'free_trial', 60)," |
| 34 | + + " (1, '2023-01-10', 'paid', 75)," |
| 35 | + + " (1, '2023-01-12', 'paid', 90)," |
| 36 | + + " (1, '2023-01-15', 'paid', 65)," |
| 37 | + + " (2, '2023-02-01', 'free_trial', 55)," |
| 38 | + + " (2, '2023-02-03', 'free_trial', 25)," |
| 39 | + + " (2, '2023-02-07', 'free_trial', 50)," |
| 40 | + + " (2, '2023-02-10', 'cancelled', 0)," |
| 41 | + + " (3, '2023-03-05', 'free_trial', 70)," |
| 42 | + + " (3, '2023-03-06', 'free_trial', 60)," |
| 43 | + + " (3, '2023-03-08', 'free_trial', 80)," |
| 44 | + + " (3, '2023-03-12', 'paid', 50)," |
| 45 | + + " (3, '2023-03-15', 'paid', 55)," |
| 46 | + + " (3, '2023-03-20', 'paid', 85)," |
| 47 | + + " (4, '2023-04-01', 'free_trial', 40)," |
| 48 | + + " (4, '2023-04-03', 'free_trial', 35)," |
| 49 | + + " (4, '2023-04-05', 'paid', 45)," |
| 50 | + + " (4, '2023-04-07', 'cancelled', 0);") |
| 51 | +class MysqlTest { |
| 52 | + @Test |
| 53 | + void testScript(@EmbeddedDatabase DataSource dataSource) |
| 54 | + throws SQLException, FileNotFoundException { |
| 55 | + try (final Connection connection = dataSource.getConnection()) { |
| 56 | + try (final Statement statement = connection.createStatement(); |
| 57 | + final ResultSet resultSet = |
| 58 | + statement.executeQuery( |
| 59 | + new BufferedReader( |
| 60 | + new FileReader( |
| 61 | + "src/main/java/g3401_3500/" |
| 62 | + + "s3497_analyze_subscription_conversion/" |
| 63 | + + "script.sql")) |
| 64 | + .lines() |
| 65 | + .collect(Collectors.joining("\n")) |
| 66 | + .replaceAll("#.*?\\r?\\n", ""))) { |
| 67 | + checkRow(resultSet, new String[] {"1", "45.0", "76.67"}); |
| 68 | + checkRow(resultSet, new String[] {"3", "70.0", "63.33"}); |
| 69 | + checkRow(resultSet, new String[] {"4", "37.5", "45.0"}); |
| 70 | + assertThat(resultSet.next(), equalTo(false)); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void checkRow(ResultSet resultSet, String[] values) throws SQLException { |
| 76 | + assertThat(resultSet.next(), equalTo(true)); |
| 77 | + assertThat(resultSet.getNString(1), equalTo(values[0])); |
| 78 | + assertThat(resultSet.getNString(2), equalTo(values[1])); |
| 79 | + assertThat(resultSet.getNString(3), equalTo(values[2])); |
| 80 | + } |
| 81 | +} |
0 commit comments