|
| 1 | +package g3601_3700.s3673_find_zombie_sessions; |
| 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 app_events (" |
| 24 | + + " event_id INT PRIMARY KEY," |
| 25 | + + " user_id INT NOT NULL," |
| 26 | + + " event_timestamp TIMESTAMP NOT NULL," |
| 27 | + + " event_type VARCHAR(50) NOT NULL," |
| 28 | + + " session_id VARCHAR(50) NOT NULL," |
| 29 | + + " event_value INT" |
| 30 | + + ");" |
| 31 | + + "INSERT INTO app_events (event_id, user_id, event_timestamp, " |
| 32 | + + "event_type, session_id, event_value) VALUES" |
| 33 | + + "(1, 201, '2024-03-01 10:00:00', 'app_open', 'S001', NULL)," |
| 34 | + + "(2, 201, '2024-03-01 10:05:00', 'scroll', 'S001', 500)," |
| 35 | + + "(3, 201, '2024-03-01 10:10:00', 'scroll', 'S001', 750)," |
| 36 | + + "(4, 201, '2024-03-01 10:15:00', 'scroll', 'S001', 600)," |
| 37 | + + "(5, 201, '2024-03-01 10:20:00', 'scroll', 'S001', 800)," |
| 38 | + + "(6, 201, '2024-03-01 10:25:00', 'scroll', 'S001', 550)," |
| 39 | + + "(7, 201, '2024-03-01 10:30:00', 'scroll', 'S001', 900)," |
| 40 | + + "(8, 201, '2024-03-01 10:35:00', 'app_close', 'S001', NULL)," |
| 41 | + + "(9, 202, '2024-03-01 11:00:00', 'app_open', 'S002', NULL)," |
| 42 | + + "(10, 202, '2024-03-01 11:02:00', 'click', 'S002', NULL)," |
| 43 | + + "(11, 202, '2024-03-01 11:05:00', 'scroll', 'S002', 400)," |
| 44 | + + "(12, 202, '2024-03-01 11:08:00', 'click', 'S002', NULL)," |
| 45 | + + "(13, 202, '2024-03-01 11:10:00', 'scroll', 'S002', 350)," |
| 46 | + + "(14, 202, '2024-03-01 11:15:00', 'purchase', 'S002', 50)," |
| 47 | + + "(15, 202, '2024-03-01 11:20:00', 'app_close','S002', NULL)," |
| 48 | + + "(16, 203, '2024-03-01 12:00:00', 'app_open', 'S003', NULL)," |
| 49 | + + "(17, 203, '2024-03-01 12:10:00', 'scroll', 'S003', 1000)," |
| 50 | + + "(18, 203, '2024-03-01 12:20:00', 'scroll', 'S003', 1200)," |
| 51 | + + "(19, 203, '2024-03-01 12:25:00', 'click', 'S003', NULL)," |
| 52 | + + "(20, 203, '2024-03-01 12:30:00', 'scroll', 'S003', 800)," |
| 53 | + + "(21, 203, '2024-03-01 12:40:00', 'scroll', 'S003', 900)," |
| 54 | + + "(22, 203, '2024-03-01 12:50:00', 'scroll', 'S003', 1100)," |
| 55 | + + "(23, 203, '2024-03-01 13:00:00', 'app_close','S003', NULL)," |
| 56 | + + "(24, 204, '2024-03-01 14:00:00', 'app_open', 'S004', NULL)," |
| 57 | + + "(25, 204, '2024-03-01 14:05:00', 'scroll', 'S004', 600)," |
| 58 | + + "(26, 204, '2024-03-01 14:08:00', 'scroll', 'S004', 700)," |
| 59 | + + "(27, 204, '2024-03-01 14:10:00', 'click', 'S004', NULL)," |
| 60 | + + "(28, 204, '2024-03-01 14:12:00', 'app_close','S004', NULL);") |
| 61 | +class MysqlTest { |
| 62 | + @Test |
| 63 | + void testScript(@EmbeddedDatabase DataSource dataSource) |
| 64 | + throws SQLException, FileNotFoundException { |
| 65 | + try (final Connection connection = dataSource.getConnection()) { |
| 66 | + try (final Statement statement = connection.createStatement(); |
| 67 | + final ResultSet resultSet = |
| 68 | + statement.executeQuery( |
| 69 | + new BufferedReader( |
| 70 | + new FileReader( |
| 71 | + "src/main/java/g3601_3700/" |
| 72 | + + "s3673_find_zombie_sessions/" |
| 73 | + + "script.sql")) |
| 74 | + .lines() |
| 75 | + .collect(Collectors.joining("\n")) |
| 76 | + .replaceAll("#.*?\\r?\\n", ""))) { |
| 77 | + assertThat(resultSet.next(), equalTo(true)); |
| 78 | + assertThat(resultSet.getNString(1), equalTo("S001")); |
| 79 | + assertThat(resultSet.getNString(2), equalTo("201")); |
| 80 | + assertThat(resultSet.getNString(3), equalTo("35")); |
| 81 | + assertThat(resultSet.getNString(4), equalTo("6")); |
| 82 | + assertThat(resultSet.next(), equalTo(false)); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments