|
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,27 +88,77 @@ def test_table_name(init_and_plan_context: t.Callable): |
86 | 88 | ) |
87 | 89 |
|
88 | 90 |
|
89 | | -def test_janitor_loads_materialization(mocker: MockerFixture, tmp_path: Path): |
90 | | - models_dir = tmp_path / "models" |
91 | | - models_dir.mkdir(exist_ok=True) |
92 | | - |
93 | | - test_model = models_dir / "model.sql" |
94 | | - |
95 | | - with open(test_model, "w") as f: |
96 | | - f.write( |
97 | | - "MODEL(name test.model, kind CUSTOM (materialization 'custom_full')); SELECT 1 AS col" |
98 | | - ) |
99 | | - |
100 | | - config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb")) |
101 | | - ctx = Context(paths=[tmp_path], config=config) |
| 91 | +def test_janitor_loads_materialization(tmp_path: Path): |
| 92 | + config_file_path = Path("config.yaml") |
| 93 | + database = tmp_path / "janitor_test.duckdb" |
| 94 | + materialization_name = "dummy_materialization" |
| 95 | + materialization_file_path = Path(f"materializations/{materialization_name}.py") |
| 96 | + schema_name = "test" |
| 97 | + model_name = "dummy_model" |
| 98 | + model_file_path = Path(f"models/{model_name}.sql") |
| 99 | + config_yaml = f"""\ |
| 100 | +gateways: |
| 101 | + local: |
| 102 | + connection: |
| 103 | + type: duckdb |
| 104 | + database: {database} |
| 105 | +default_gateway: local |
| 106 | +model_defaults: |
| 107 | + dialect: duckdb |
| 108 | +""" |
| 109 | + materialization_code = f"""from sqlmesh.core.snapshot.evaluator import CustomMaterialization |
| 110 | +class DummyMaterialization(CustomMaterialization): |
| 111 | + NAME = "{materialization_name}" |
| 112 | + def insert(self, table_name, query_or_df, model, is_first_insert, render_kwargs, **kwargs): |
| 113 | + self._replace_query_for_model(model, table_name, query_or_df, render_kwargs) |
| 114 | +""" |
| 115 | + model_code = f"""MODEL(name {schema_name}.{model_name}, kind CUSTOM (materialization '{materialization_name}')); |
| 116 | +SELECT 1 AS col""" |
| 117 | + |
| 118 | + create_temp_file(tmp_path, config_file_path, config_yaml) |
| 119 | + create_temp_file(tmp_path, materialization_file_path, materialization_code) |
| 120 | + create_temp_file(tmp_path, model_file_path, model_code) |
| 121 | + |
| 122 | + ctx = Context(paths=[tmp_path]) |
102 | 123 | ctx.plan("dev", no_prompts=True, auto_apply=True) |
103 | 124 |
|
104 | | - load_materializations_spy = mocker.spy(Context, "_load_materializations") |
105 | | - |
| 125 | + snapshot = ctx.get_snapshot(f"{schema_name}.{model_name}") |
| 126 | + assert snapshot |
| 127 | + table_name = snapshot.table_name() |
| 128 | + assert ctx.engine_adapter.table_exists(table_name) |
| 129 | + assert ctx.state_sync.get_snapshots([snapshot.snapshot_id]) |
| 130 | + assert ctx._loaded |
| 131 | + assert ctx._materializations_loaded |
106 | 132 | ctx.invalidate_environment("dev") |
107 | | - ctx.run_janitor(ignore_ttl=True) |
| 133 | + ctx.close() |
| 134 | + |
| 135 | + # A fresh interpreter cannot reuse materialization classes loaded during setup. |
| 136 | + program = """\ |
| 137 | +import sys |
| 138 | +from sqlmesh.core.context import Context |
| 139 | +
|
| 140 | +ctx = Context(paths=[sys.argv[1]], load=False) |
| 141 | +table_name = sys.argv[2] |
| 142 | +assert not ctx._loaded |
| 143 | +assert not ctx._materializations_loaded |
| 144 | +assert ctx.engine_adapter.table_exists(table_name) |
| 145 | +
|
| 146 | +assert ctx.run_janitor(ignore_ttl=True) |
| 147 | +
|
| 148 | +assert not ctx.engine_adapter.table_exists(table_name) |
| 149 | +assert not ctx._loaded |
| 150 | +assert ctx._materializations_loaded |
| 151 | +ctx.close() |
| 152 | +""" |
| 153 | + result = subprocess.run( |
| 154 | + [sys.executable, "-c", program, str(tmp_path), table_name], |
| 155 | + cwd=tmp_path, |
| 156 | + capture_output=True, |
| 157 | + text=True, |
| 158 | + timeout=120, |
| 159 | + ) |
108 | 160 |
|
109 | | - load_materializations_spy.assert_called_once() |
| 161 | + assert result.returncode == 0, result.stdout + result.stderr |
110 | 162 |
|
111 | 163 |
|
112 | 164 | def test_janitor_cleanup_order(mocker: MockerFixture, tmp_path: Path): |
|
0 commit comments