|
3 | 3 | from sqlglot import parse_one |
4 | 4 |
|
5 | 5 | from sqlmesh.core.context import Context |
6 | | -from sqlmesh.core.model import FullKind, SqlModel, ViewKind |
| 6 | +from sqlmesh.core.model import FullKind, IncrementalByTimeRangeKind, SqlModel, ViewKind |
7 | 7 | from sqlmesh.core.plan import ( |
8 | 8 | AirflowPlanEvaluator, |
9 | 9 | BuiltInPlanEvaluator, |
10 | 10 | MWAAPlanEvaluator, |
11 | 11 | Plan, |
| 12 | + can_evaluate_before_promote, |
12 | 13 | ) |
13 | 14 | from sqlmesh.core.snapshot import SnapshotChangeCategory |
| 15 | +from sqlmesh.utils.date import now_timestamp |
14 | 16 | from sqlmesh.utils.errors import SQLMeshError |
15 | 17 |
|
16 | 18 |
|
@@ -162,3 +164,61 @@ def test_mwaa_evaluator_error_from_cli(sushi_plan: Plan, mocker: MockerFixture): |
162 | 164 | evaluator.evaluate(sushi_plan) |
163 | 165 |
|
164 | 166 | mwaa_client_mock.set_variable.assert_called_once_with(mocker.ANY, plan_dag_spec_json) |
| 167 | + |
| 168 | + |
| 169 | +def test_can_evaluate_before_promote(sushi_context: Context): |
| 170 | + parent_model_a = SqlModel( |
| 171 | + name="sushi.new_test_model_a", |
| 172 | + kind=IncrementalByTimeRangeKind(time_column="ds"), |
| 173 | + cron="@daily", |
| 174 | + start="2020-01-01", |
| 175 | + query=parse_one("SELECT 1::INT AS one, '2023-01-01' as ds"), |
| 176 | + ) |
| 177 | + parent_model_b = SqlModel( |
| 178 | + name="sushi.new_test_model_b", |
| 179 | + kind=IncrementalByTimeRangeKind(time_column="ds"), |
| 180 | + cron="@daily", |
| 181 | + start="2020-01-01", |
| 182 | + query=parse_one("SELECT 2::INT AS two, '2023-01-01' as ds"), |
| 183 | + ) |
| 184 | + child_model = SqlModel( |
| 185 | + name="sushi.new_test_model_child", |
| 186 | + kind=FullKind(), |
| 187 | + start="2020-01-01", |
| 188 | + query=parse_one("SELECT one, two FROM sushi.new_test_model_a, sushi.new_test_model_b"), |
| 189 | + ) |
| 190 | + |
| 191 | + sushi_context.upsert_model(parent_model_a) |
| 192 | + sushi_context.upsert_model(parent_model_b) |
| 193 | + sushi_context.upsert_model(child_model) |
| 194 | + |
| 195 | + snapshots = sushi_context.snapshots |
| 196 | + |
| 197 | + parent_snapshot_a = snapshots[parent_model_a.name] |
| 198 | + parent_snapshot_b = snapshots[parent_model_b.name] |
| 199 | + child_snapshot = snapshots[child_model.name] |
| 200 | + |
| 201 | + all_snapshots = { |
| 202 | + s.snapshot_id: s for s in [parent_snapshot_a, parent_snapshot_b, child_snapshot] |
| 203 | + } |
| 204 | + |
| 205 | + parent_snapshot_a.change_category = SnapshotChangeCategory.BREAKING |
| 206 | + parent_snapshot_b.change_category = SnapshotChangeCategory.BREAKING |
| 207 | + child_snapshot.change_category = SnapshotChangeCategory.BREAKING |
| 208 | + assert can_evaluate_before_promote(child_snapshot, all_snapshots) |
| 209 | + |
| 210 | + parent_snapshot_a.change_category = SnapshotChangeCategory.FORWARD_ONLY |
| 211 | + parent_snapshot_b.change_category = SnapshotChangeCategory.FORWARD_ONLY |
| 212 | + assert not can_evaluate_before_promote(child_snapshot, all_snapshots) |
| 213 | + |
| 214 | + parent_snapshot_a.unpaused_ts = now_timestamp() |
| 215 | + assert not can_evaluate_before_promote(child_snapshot, all_snapshots) |
| 216 | + |
| 217 | + parent_snapshot_b.unpaused_ts = now_timestamp() |
| 218 | + assert can_evaluate_before_promote(child_snapshot, all_snapshots) |
| 219 | + |
| 220 | + child_snapshot.change_category = SnapshotChangeCategory.FORWARD_ONLY |
| 221 | + assert not can_evaluate_before_promote(child_snapshot, all_snapshots) |
| 222 | + |
| 223 | + child_snapshot.unpaused_ts = now_timestamp() |
| 224 | + assert can_evaluate_before_promote(child_snapshot, all_snapshots) |
0 commit comments