|
6 | 6 | from sqlmesh.core.plan.definition import EvaluatablePlan |
7 | 7 | from sqlmesh.core.plan.stages import ( |
8 | 8 | build_plan_stages, |
| 9 | + AfterAllStage, |
9 | 10 | PhysicalLayerUpdateStage, |
| 11 | + BeforeAllStage, |
10 | 12 | BackfillStage, |
11 | 13 | EnvironmentRecordUpdateStage, |
12 | 14 | VirtualLayerUpdateStage, |
|
15 | 17 | ) |
16 | 18 | from sqlmesh.core.snapshot.definition import SnapshotChangeCategory, DeployabilityIndex, Snapshot |
17 | 19 | from sqlmesh.core.state_sync import StateReader |
18 | | -from sqlmesh.core.environment import Environment |
| 20 | +from sqlmesh.core.environment import Environment, EnvironmentStatements |
19 | 21 | from sqlmesh.utils.date import to_timestamp |
20 | 22 |
|
21 | 23 |
|
@@ -127,6 +129,104 @@ def test_build_plan_stages_basic( |
127 | 129 | assert {s.name for s in virtual_stage.promoted_snapshots} == {'"a"', '"b"'} |
128 | 130 |
|
129 | 131 |
|
| 132 | +def test_build_plan_stages_with_before_all_and_after_all( |
| 133 | + snapshot_a: Snapshot, snapshot_b: Snapshot, mocker: MockerFixture |
| 134 | +) -> None: |
| 135 | + # Mock state reader |
| 136 | + state_reader = mocker.Mock(spec=StateReader) |
| 137 | + state_reader.get_snapshots.return_value = {} |
| 138 | + state_reader.get_environment.return_value = None |
| 139 | + |
| 140 | + # Create environment |
| 141 | + environment = Environment( |
| 142 | + snapshots=[snapshot_a.table_info, snapshot_b.table_info], |
| 143 | + start_at="2023-01-01", |
| 144 | + end_at="2023-01-02", |
| 145 | + plan_id="test_plan", |
| 146 | + previous_plan_id=None, |
| 147 | + promoted_snapshot_ids=[snapshot_a.snapshot_id, snapshot_b.snapshot_id], |
| 148 | + ) |
| 149 | + |
| 150 | + # Create evaluatable plan |
| 151 | + plan = EvaluatablePlan( |
| 152 | + start="2023-01-01", |
| 153 | + end="2023-01-02", |
| 154 | + new_snapshots=[snapshot_a, snapshot_b], |
| 155 | + environment=environment, |
| 156 | + no_gaps=False, |
| 157 | + skip_backfill=False, |
| 158 | + empty_backfill=False, |
| 159 | + restatements={}, |
| 160 | + is_dev=False, |
| 161 | + allow_destructive_models=set(), |
| 162 | + forward_only=False, |
| 163 | + end_bounded=False, |
| 164 | + ensure_finalized_snapshots=False, |
| 165 | + directly_modified_snapshots=[snapshot_a.snapshot_id, snapshot_b.snapshot_id], |
| 166 | + indirectly_modified_snapshots={}, |
| 167 | + removed_snapshots=[], |
| 168 | + requires_backfill=True, |
| 169 | + models_to_backfill=None, |
| 170 | + interval_end_per_model=None, |
| 171 | + execution_time="2023-01-02", |
| 172 | + disabled_restatement_models=set(), |
| 173 | + environment_statements=[ |
| 174 | + EnvironmentStatements( |
| 175 | + before_all=["BEFORE ALL A", "BEFORE ALL B"], |
| 176 | + after_all=["AFTER ALL A", "AFTER ALL B"], |
| 177 | + python_env={}, |
| 178 | + jinja_macros=None, |
| 179 | + ) |
| 180 | + ], |
| 181 | + user_provided_flags=None, |
| 182 | + ) |
| 183 | + |
| 184 | + # Build plan stages |
| 185 | + stages = build_plan_stages(plan, state_reader, None) |
| 186 | + |
| 187 | + # Verify stages |
| 188 | + assert len(stages) == 6 |
| 189 | + |
| 190 | + # Verify BeforeAllStage |
| 191 | + before_all_stage = stages[0] |
| 192 | + assert isinstance(before_all_stage, BeforeAllStage) |
| 193 | + assert before_all_stage.statements == ["BEFORE ALL A", "BEFORE ALL B"] |
| 194 | + |
| 195 | + # Verify PhysicalLayerUpdateStage |
| 196 | + physical_stage = stages[1] |
| 197 | + assert isinstance(physical_stage, PhysicalLayerUpdateStage) |
| 198 | + assert len(physical_stage.snapshots) == 2 |
| 199 | + assert {s.snapshot_id for s in physical_stage.snapshots} == { |
| 200 | + snapshot_a.snapshot_id, |
| 201 | + snapshot_b.snapshot_id, |
| 202 | + } |
| 203 | + assert physical_stage.deployability_index == DeployabilityIndex.all_deployable() |
| 204 | + |
| 205 | + # Verify BackfillStage |
| 206 | + backfill_stage = stages[2] |
| 207 | + assert isinstance(backfill_stage, BackfillStage) |
| 208 | + assert backfill_stage.deployability_index == DeployabilityIndex.all_deployable() |
| 209 | + expected_interval = (to_timestamp("2023-01-01"), to_timestamp("2023-01-02")) |
| 210 | + assert len(backfill_stage.snapshot_to_intervals) == 2 |
| 211 | + assert backfill_stage.snapshot_to_intervals[snapshot_a] == [expected_interval] |
| 212 | + assert backfill_stage.snapshot_to_intervals[snapshot_b] == [expected_interval] |
| 213 | + |
| 214 | + # Verify EnvironmentRecordUpdateStage |
| 215 | + assert isinstance(stages[3], EnvironmentRecordUpdateStage) |
| 216 | + |
| 217 | + # Verify VirtualLayerUpdateStage |
| 218 | + virtual_stage = stages[4] |
| 219 | + assert isinstance(virtual_stage, VirtualLayerUpdateStage) |
| 220 | + assert len(virtual_stage.promoted_snapshots) == 2 |
| 221 | + assert len(virtual_stage.demoted_snapshots) == 0 |
| 222 | + assert {s.name for s in virtual_stage.promoted_snapshots} == {'"a"', '"b"'} |
| 223 | + |
| 224 | + # Verify AfterAllStage |
| 225 | + after_all_stage = stages[5] |
| 226 | + assert isinstance(after_all_stage, AfterAllStage) |
| 227 | + assert after_all_stage.statements == ["AFTER ALL A", "AFTER ALL B"] |
| 228 | + |
| 229 | + |
130 | 230 | def test_build_plan_stages_select_models( |
131 | 231 | snapshot_a: Snapshot, snapshot_b: Snapshot, mocker: MockerFixture |
132 | 232 | ) -> None: |
|
0 commit comments