|
| 1 | +use crate::events::constants::{ |
| 2 | + COLUMN_MIGRATION_COLUMNS, MIGRATION_COLUMNS, NODE_RESULT_COLUMNS, OLD_NAME_VIEW_COLUMNS, |
| 3 | +}; |
1 | 4 | use crate::state_tables::main::create_table_sql::create_table_sql; |
2 | 5 | use crate::state_tables::main::read_history_sql::read_history_sql; |
3 | 6 | use crate::state_tables::main::values_insert_sql::values_insert_sql; |
4 | 7 | use crate::state_tables::models::HistoryRead; |
5 | 8 | use crate::state_tables::tests::helpers::column; |
6 | 9 | use crate::state_tables::tests::test_types::{ |
7 | | - CreateTableTestCase, HistoryReadTestCase, ValuesInsertTestCase, |
| 10 | + CreateTableTestCase, EventKeyTestCase, HistoryReadTestCase, ValuesInsertTestCase, |
8 | 11 | }; |
9 | 12 |
|
10 | 13 | #[test] |
@@ -110,3 +113,126 @@ fn given_a_table_without_event_ids_when_paging_history_then_the_missing_key_neve |
110 | 113 | ); |
111 | 114 | } |
112 | 115 | } |
| 116 | + |
| 117 | +#[test] |
| 118 | +fn given_event_tables_keyed_by_event_id_when_paging_history_then_each_column_and_key_appears_once() |
| 119 | +{ |
| 120 | + let test_cases = [ |
| 121 | + EventKeyTestCase { |
| 122 | + description: "migrations", |
| 123 | + columns: &MIGRATION_COLUMNS, |
| 124 | + key_columns: &["event_id", "created_at"], |
| 125 | + timestamp_column: "created_at", |
| 126 | + expected_ordering_keys: &["event_id", "created_at"], |
| 127 | + }, |
| 128 | + EventKeyTestCase { |
| 129 | + description: "column migrations", |
| 130 | + columns: &COLUMN_MIGRATION_COLUMNS, |
| 131 | + key_columns: &["event_id", "created_at"], |
| 132 | + timestamp_column: "created_at", |
| 133 | + expected_ordering_keys: &["event_id", "created_at"], |
| 134 | + }, |
| 135 | + EventKeyTestCase { |
| 136 | + description: "old-name views", |
| 137 | + columns: &OLD_NAME_VIEW_COLUMNS, |
| 138 | + key_columns: &["event_id", "created_at"], |
| 139 | + timestamp_column: "created_at", |
| 140 | + expected_ordering_keys: &["event_id", "created_at"], |
| 141 | + }, |
| 142 | + EventKeyTestCase { |
| 143 | + description: "microbatches", |
| 144 | + columns: &["event_id", "record_type", "scope_kind", "created_at"], |
| 145 | + key_columns: &["event_id", "created_at"], |
| 146 | + timestamp_column: "created_at", |
| 147 | + expected_ordering_keys: &["event_id", "created_at"], |
| 148 | + }, |
| 149 | + EventKeyTestCase { |
| 150 | + description: "janitor events", |
| 151 | + columns: &["event_id", "schema_version", "event_type", "occurred_at"], |
| 152 | + key_columns: &["event_id", "occurred_at"], |
| 153 | + timestamp_column: "occurred_at", |
| 154 | + expected_ordering_keys: &["event_id", "occurred_at"], |
| 155 | + }, |
| 156 | + EventKeyTestCase { |
| 157 | + description: "node results, which carry no event ID column", |
| 158 | + columns: &NODE_RESULT_COLUMNS, |
| 159 | + key_columns: &[ |
| 160 | + "node_type", |
| 161 | + "node_name", |
| 162 | + "target_database", |
| 163 | + "target_schema", |
| 164 | + "target_name", |
| 165 | + "run_id", |
| 166 | + "status", |
| 167 | + "ts", |
| 168 | + ], |
| 169 | + timestamp_column: "ts", |
| 170 | + expected_ordering_keys: &[ |
| 171 | + "node_type", |
| 172 | + "node_name", |
| 173 | + "target_database", |
| 174 | + "target_schema", |
| 175 | + "target_name", |
| 176 | + "run_id", |
| 177 | + "status", |
| 178 | + "ts", |
| 179 | + "event_id", |
| 180 | + ], |
| 181 | + }, |
| 182 | + ]; |
| 183 | + let adapters = [ |
| 184 | + "snowflake", |
| 185 | + "duckdb", |
| 186 | + "motherduck", |
| 187 | + "sqlite", |
| 188 | + "postgres", |
| 189 | + "bigquery", |
| 190 | + "databricks", |
| 191 | + "sqlserver", |
| 192 | + "clickhouse", |
| 193 | + ]; |
| 194 | + for test_case in test_cases { |
| 195 | + for adapter in adapters { |
| 196 | + let protocol_cursor = vec![Some("'x'".to_owned()); test_case.key_columns.len() + 1]; |
| 197 | + let sql = read_history_sql( |
| 198 | + "s.t", |
| 199 | + Some(&protocol_cursor), |
| 200 | + HistoryRead { |
| 201 | + columns: test_case.columns, |
| 202 | + key_columns: Some(test_case.key_columns), |
| 203 | + timestamp_column: test_case.timestamp_column, |
| 204 | + observed_since: None, |
| 205 | + adapter, |
| 206 | + has_event_id: true, |
| 207 | + limit: 5, |
| 208 | + }, |
| 209 | + ) |
| 210 | + .expect("supported adapter"); |
| 211 | + let head = sql.strip_prefix("SELECT ").expect("select"); |
| 212 | + let head = head.strip_prefix("TOP 5 ").unwrap_or(head); |
| 213 | + let (projection, rest) = head.split_once(" FROM ").expect("from"); |
| 214 | + let mut selected: Vec<&str> = projection.split(", ").collect(); |
| 215 | + selected.sort_unstable(); |
| 216 | + selected.dedup(); |
| 217 | + assert_eq!( |
| 218 | + selected.len(), |
| 219 | + test_case.columns.len() + usize::from(!test_case.columns.contains(&"event_id")), |
| 220 | + "{} on {adapter}: each column selected once in {sql}", |
| 221 | + test_case.description |
| 222 | + ); |
| 223 | + let ordering_keys: Vec<&str> = rest |
| 224 | + .split_once(" ORDER BY ") |
| 225 | + .expect("order") |
| 226 | + .1 |
| 227 | + .split("CASE WHEN ") |
| 228 | + .skip(1) |
| 229 | + .filter_map(|item| item.split_once(" IS NULL").map(|(key, _)| key)) |
| 230 | + .collect(); |
| 231 | + assert_eq!( |
| 232 | + ordering_keys, test_case.expected_ordering_keys, |
| 233 | + "{} on {adapter}: {sql}", |
| 234 | + test_case.description |
| 235 | + ); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments