|
1 | 1 | use crate::consumption::main::recent_executions::recent_executions; |
2 | | -use crate::consumption::models::{ConsumptionAppend, ConsumptionRow, NodeKey, RelationKey}; |
3 | | - |
4 | | -fn row(relation: &str, execution: &str, recorded_at: i64) -> ConsumptionRow { |
5 | | - ConsumptionRow { |
6 | | - row: ConsumptionAppend { |
7 | | - execution_id: execution.into(), |
8 | | - phase: "succeeded".into(), |
9 | | - run_id: format!("run-{execution}"), |
10 | | - node: NodeKey { |
11 | | - kind: "model".into(), |
12 | | - name: relation.into(), |
13 | | - }, |
14 | | - relation: RelationKey { |
15 | | - database: None, |
16 | | - schema: "main".into(), |
17 | | - name: relation.into(), |
18 | | - }, |
19 | | - version_hash: "version".into(), |
20 | | - input_count: 0, |
21 | | - input: None, |
22 | | - }, |
23 | | - recorded_at, |
24 | | - } |
25 | | -} |
| 2 | +use crate::consumption::tests::helpers::recorded_row; |
| 3 | +use crate::consumption::tests::test_types::RecentExecutionsTestCase; |
26 | 4 |
|
27 | 5 | #[test] |
28 | 6 | fn given_more_executions_than_the_limit_when_choosing_recent_then_the_newest_starts_win_per_relation() |
29 | 7 | { |
30 | | - let mut rows: Vec<ConsumptionRow> = Vec::new(); |
31 | | - for execution in 1..=4 { |
32 | | - // `orders-1` started first but wrote its last row after every other execution. |
33 | | - let finished: i64 = if execution == 1 { |
34 | | - 50 |
35 | | - } else { |
36 | | - execution * 10 + 5 |
37 | | - }; |
38 | | - rows.push(row( |
| 8 | + // `orders-1` started first but wrote its last row after every other execution. |
| 9 | + let mut late_finisher = Vec::new(); |
| 10 | + for (execution, started, finished) in [(1, 10, 50), (2, 20, 25), (3, 30, 35), (4, 40, 45)] { |
| 11 | + late_finisher.push(recorded_row( |
| 12 | + "orders", |
| 13 | + &format!("orders-{execution}"), |
| 14 | + started, |
| 15 | + )); |
| 16 | + late_finisher.push(recorded_row( |
39 | 17 | "orders", |
40 | 18 | &format!("orders-{execution}"), |
41 | | - execution * 10, |
| 19 | + finished, |
42 | 20 | )); |
43 | | - rows.push(row("orders", &format!("orders-{execution}"), finished)); |
44 | 21 | } |
45 | | - rows.push(row("customers", "customers-1", 1)); |
46 | | - |
47 | | - let mut kept: Vec<String> = recent_executions(&rows, 2) |
48 | | - .into_iter() |
49 | | - .map(|row| row.row.execution_id) |
50 | | - .collect(); |
51 | | - kept.dedup(); |
52 | | - |
53 | | - assert_eq!( |
54 | | - kept, |
55 | | - ["orders-3", "orders-4", "customers-1"], |
56 | | - "each relation keeps its newest two executions by earliest row, with every row of each" |
57 | | - ); |
58 | | - assert_eq!(recent_executions(&rows, 2).len(), 5); |
| 22 | + late_finisher.push(recorded_row("customers", "customers-1", 1)); |
| 23 | + let test_cases = [ |
| 24 | + RecentExecutionsTestCase { |
| 25 | + description: "an execution that started first but wrote last is the oldest; other relations keep theirs", |
| 26 | + rows: late_finisher, |
| 27 | + limit: 2, |
| 28 | + expected_executions: vec!["orders-3", "orders-4", "customers-1"], |
| 29 | + expected_rows: 5, |
| 30 | + }, |
| 31 | + RecentExecutionsTestCase { |
| 32 | + description: "equal start times keep the larger execution id, as the warehouse query does", |
| 33 | + rows: vec![ |
| 34 | + recorded_row("orders", "orders-a", 10), |
| 35 | + recorded_row("orders", "orders-b", 10), |
| 36 | + ], |
| 37 | + limit: 1, |
| 38 | + expected_executions: vec!["orders-b"], |
| 39 | + expected_rows: 1, |
| 40 | + }, |
| 41 | + ]; |
| 42 | + for test_case in test_cases { |
| 43 | + let kept = recent_executions(&test_case.rows, test_case.limit); |
| 44 | + let mut executions: Vec<&str> = kept |
| 45 | + .iter() |
| 46 | + .map(|row| row.row.execution_id.as_str()) |
| 47 | + .collect(); |
| 48 | + executions.dedup(); |
| 49 | + assert_eq!( |
| 50 | + executions, test_case.expected_executions, |
| 51 | + "{}", |
| 52 | + test_case.description |
| 53 | + ); |
| 54 | + assert_eq!( |
| 55 | + kept.len(), |
| 56 | + test_case.expected_rows, |
| 57 | + "{}", |
| 58 | + test_case.description |
| 59 | + ); |
| 60 | + } |
59 | 61 | } |
0 commit comments