|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import typing as t |
| 4 | +import subprocess |
| 5 | +import sys |
4 | 6 | from unittest.mock import patch |
5 | 7 | import pytest |
6 | 8 | from pathlib import Path |
@@ -86,6 +88,121 @@ def test_table_name(init_and_plan_context: t.Callable): |
86 | 88 | ) |
87 | 89 |
|
88 | 90 |
|
| 91 | +@time_machine.travel("2023-01-08 15:00:00 UTC") |
| 92 | +def test_run_loads_materializations_once(init_and_plan_context: t.Callable, mocker: MockerFixture): |
| 93 | + """Verify janitor and run dispatch loading while loaders execute only once.""" |
| 94 | + context, plan = init_and_plan_context("examples/sushi") |
| 95 | + context.apply(plan) |
| 96 | + load_dispatch_spy = mocker.spy(context, "_load_materializations") |
| 97 | + loader_spies = [mocker.spy(loader, "load_materializations") for loader in context._loaders] |
| 98 | + assert loader_spies |
| 99 | + assert context._materializations_loaded is True |
| 100 | + |
| 101 | + # Reset the guard to exercise lazy loading through run's janitor and scheduler paths. |
| 102 | + context._materializations_loaded = False |
| 103 | + context.run(skip_janitor=False) |
| 104 | + |
| 105 | + # The janitor and run each call the context wrapper; the second call is guarded. |
| 106 | + assert load_dispatch_spy.call_count == 2 |
| 107 | + |
| 108 | + # Only the janitor's first dispatch reaches the loaders. |
| 109 | + for loader_spy in loader_spies: |
| 110 | + assert loader_spy.call_count == 1 |
| 111 | + |
| 112 | + |
| 113 | +@time_machine.travel("2023-01-08 15:00:00 UTC") |
| 114 | +def test_destroy_fails_before_invalidating_environments_on_materialization_error( |
| 115 | + init_and_plan_context: t.Callable, mocker: MockerFixture |
| 116 | +): |
| 117 | + context, plan = init_and_plan_context("examples/sushi") |
| 118 | + context.apply(plan) |
| 119 | + assert context.state_reader.get_environment("prod").expiration_ts is None |
| 120 | + |
| 121 | + for loader in context._loaders: |
| 122 | + mocker.patch.object( |
| 123 | + loader, "load_materializations", side_effect=SQLMeshError("broken materialization") |
| 124 | + ) |
| 125 | + context._materializations_loaded = False |
| 126 | + |
| 127 | + with pytest.raises(SQLMeshError, match="broken materialization"): |
| 128 | + context.destroy() |
| 129 | + |
| 130 | + assert context.state_reader.get_environment("prod").expiration_ts is None |
| 131 | + |
| 132 | + |
| 133 | +def test_janitor_loads_materialization(tmp_path: Path): |
| 134 | + config_file_path = Path("config.yaml") |
| 135 | + database = tmp_path / "janitor_test.duckdb" |
| 136 | + materialization_name = "dummy_materialization" |
| 137 | + materialization_file_path = Path(f"materializations/{materialization_name}.py") |
| 138 | + schema_name = "test" |
| 139 | + model_name = "dummy_model" |
| 140 | + model_file_path = Path(f"models/{model_name}.sql") |
| 141 | + config_yaml = f"""\ |
| 142 | +gateways: |
| 143 | + local: |
| 144 | + connection: |
| 145 | + type: duckdb |
| 146 | + database: {database} |
| 147 | +default_gateway: local |
| 148 | +model_defaults: |
| 149 | + dialect: duckdb |
| 150 | +""" |
| 151 | + materialization_code = f"""from sqlmesh.core.snapshot.evaluator import CustomMaterialization |
| 152 | +class DummyMaterialization(CustomMaterialization): |
| 153 | + NAME = "{materialization_name}" |
| 154 | + def insert(self, table_name, query_or_df, model, is_first_insert, render_kwargs, **kwargs): |
| 155 | + self._replace_query_for_model(model, table_name, query_or_df, render_kwargs) |
| 156 | +""" |
| 157 | + model_code = f"""MODEL(name {schema_name}.{model_name}, kind CUSTOM (materialization '{materialization_name}')); |
| 158 | +SELECT 1 AS col""" |
| 159 | + |
| 160 | + create_temp_file(tmp_path, config_file_path, config_yaml) |
| 161 | + create_temp_file(tmp_path, materialization_file_path, materialization_code) |
| 162 | + create_temp_file(tmp_path, model_file_path, model_code) |
| 163 | + |
| 164 | + ctx = Context(paths=[tmp_path]) |
| 165 | + ctx.plan("dev", no_prompts=True, auto_apply=True) |
| 166 | + |
| 167 | + snapshot = ctx.get_snapshot(f"{schema_name}.{model_name}") |
| 168 | + assert snapshot |
| 169 | + table_name = snapshot.table_name() |
| 170 | + assert ctx.engine_adapter.table_exists(table_name) |
| 171 | + assert ctx.state_sync.get_snapshots([snapshot.snapshot_id]) |
| 172 | + assert ctx._loaded |
| 173 | + assert ctx._materializations_loaded |
| 174 | + ctx.invalidate_environment("dev") |
| 175 | + ctx.close() |
| 176 | + |
| 177 | + # A fresh interpreter cannot reuse materialization classes loaded during setup. |
| 178 | + program = """\ |
| 179 | +import sys |
| 180 | +from sqlmesh.core.context import Context |
| 181 | +
|
| 182 | +ctx = Context(paths=[sys.argv[1]], load=False) |
| 183 | +table_name = sys.argv[2] |
| 184 | +assert not ctx._loaded |
| 185 | +assert not ctx._materializations_loaded |
| 186 | +assert ctx.engine_adapter.table_exists(table_name) |
| 187 | +
|
| 188 | +assert ctx.run_janitor(ignore_ttl=True) |
| 189 | +
|
| 190 | +assert not ctx.engine_adapter.table_exists(table_name) |
| 191 | +assert not ctx._loaded |
| 192 | +assert ctx._materializations_loaded |
| 193 | +ctx.close() |
| 194 | +""" |
| 195 | + result = subprocess.run( |
| 196 | + [sys.executable, "-c", program, str(tmp_path), table_name], |
| 197 | + cwd=tmp_path, |
| 198 | + capture_output=True, |
| 199 | + text=True, |
| 200 | + timeout=120, |
| 201 | + ) |
| 202 | + |
| 203 | + assert result.returncode == 0, result.stdout + result.stderr |
| 204 | + |
| 205 | + |
89 | 206 | def test_janitor_cleanup_order(mocker: MockerFixture, tmp_path: Path): |
90 | 207 | def setup_scenario(): |
91 | 208 | models_dir = tmp_path / "models" |
|
0 commit comments