-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathtest_operations.py
More file actions
376 lines (302 loc) · 13.7 KB
/
test_operations.py
File metadata and controls
376 lines (302 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import typing as t
from pathlib import Path
import pytest
from sqlmesh_dbt.operations import create
from sqlmesh_dbt.console import DbtCliConsole
from sqlmesh.utils import yaml
from sqlmesh.utils.errors import SQLMeshError
import time_machine
from sqlmesh.core.plan import PlanBuilder
from sqlmesh.core.config.common import VirtualEnvironmentMode
from tests.dbt.conftest import EmptyProjectCreator
import logging
pytestmark = pytest.mark.slow
class PlanCapturingConsole(DbtCliConsole):
def plan(
self,
plan_builder: PlanBuilder,
auto_apply: bool,
default_catalog: t.Optional[str],
no_diff: bool = False,
no_prompts: bool = False,
) -> None:
self.plan_builder = plan_builder
self.auto_apply = auto_apply
self.default_catalog = default_catalog
self.no_diff = no_diff
self.no_prompts = no_prompts
# normal console starts applying the plan here; we dont because we just want to capture the parameters
# and check they were set correctly
def test_create_sets_and_persists_default_start_date(jaffle_shop_duckdb: Path):
with time_machine.travel("2020-01-02 00:00:00 UTC"):
from sqlmesh.utils.date import yesterday_ds, to_ds
assert yesterday_ds() == "2020-01-01"
operations = create()
assert operations.context.config.model_defaults.start
assert to_ds(operations.context.config.model_defaults.start) == "2020-01-01"
assert all(
to_ds(model.start) if model.start else None == "2020-01-01"
for model in operations.context.models.values()
if not model.kind.is_seed
)
# check that the date set on the first invocation persists to future invocations
from sqlmesh.utils.date import yesterday_ds, to_ds
assert yesterday_ds() != "2020-01-01"
operations = create()
assert operations.context.config.model_defaults.start
assert to_ds(operations.context.config.model_defaults.start) == "2020-01-01"
assert all(
to_ds(model.start) if model.start else None == "2020-01-01"
for model in operations.context.models.values()
if not model.kind.is_seed
)
def test_create_uses_configured_start_date_if_supplied(jaffle_shop_duckdb: Path):
sqlmesh_yaml = jaffle_shop_duckdb / "sqlmesh.yml"
with sqlmesh_yaml.open("w") as f:
yaml.dump({"model_defaults": {"start": "2023-12-12"}}, f)
operations = create()
assert operations.context.config.model_defaults.start == "2023-12-12"
assert all(
model.start == "2023-12-12"
for model in operations.context.models.values()
if not model.kind.is_seed
)
def test_create_can_specify_profile_and_target(jaffle_shop_duckdb: Path):
with pytest.raises(SQLMeshError, match=r"Profile 'foo' not found"):
create(profile="foo")
with pytest.raises(
SQLMeshError, match=r"Target 'prod' not specified in profiles for 'jaffle_shop'"
):
create(profile="jaffle_shop", target="prod")
dbt_project = create(profile="jaffle_shop", target="dev").project
assert dbt_project.context.profile_name == "jaffle_shop"
assert dbt_project.context.target_name == "dev"
def test_default_options(jaffle_shop_duckdb: Path):
operations = create()
config = operations.context.config
dbt_project = operations.project
assert config.plan.always_recreate_environment is True
assert config.virtual_environment_mode == VirtualEnvironmentMode.DEV_ONLY
assert config.model_defaults.start is not None
assert config.model_defaults.dialect == dbt_project.context.target.dialect
def test_create_can_set_project_variables(jaffle_shop_duckdb: Path):
(jaffle_shop_duckdb / "models" / "test_model.sql").write_text("""
select '{{ var('foo') }}' as a
""")
dbt_project = create(vars={"foo": "bar"})
assert dbt_project.context.config.variables["foo"] == "bar"
test_model = dbt_project.context.models['"jaffle_shop"."main"."test_model"']
query = test_model.render_query()
assert query is not None
assert query.sql() == "SELECT 'bar' AS \"a\""
def test_run_option_mapping(jaffle_shop_duckdb: Path):
operations = create(project_dir=jaffle_shop_duckdb)
console = PlanCapturingConsole()
operations.context.console = console
plan = operations.run()
standalone_audit_name = "relationships_orders_customer_id__customer_id__ref_customers_"
assert plan.environment.name == "prod"
assert console.no_prompts is True
assert console.no_diff is True
assert console.auto_apply is True
assert plan.end_bounded is False
assert plan.ignore_cron is True
assert plan.skip_backfill is False
assert plan.selected_models_to_backfill is None
assert {s.name for s in plan.snapshots} == {k for k in operations.context.snapshots}
plan = operations.run(select=["stg_orders+"])
assert plan.environment.name == "prod"
assert console.no_prompts is True
assert console.no_diff is True
assert console.auto_apply is True
assert plan.end_bounded is False
assert plan.ignore_cron is True
assert plan.skip_backfill is False
assert plan.selected_models_to_backfill == {
'"jaffle_shop"."main"."customers"',
'"jaffle_shop"."main"."orders"',
'"jaffle_shop"."main"."stg_orders"',
}
assert {s.name for s in plan.snapshots} == (
plan.selected_models_to_backfill | {standalone_audit_name}
)
plan = operations.run(select=["stg_orders+"], exclude=["customers"])
assert plan.environment.name == "prod"
assert console.no_prompts is True
assert console.no_diff is True
assert console.auto_apply is True
assert plan.end_bounded is False
assert plan.ignore_cron is True
assert plan.skip_backfill is False
assert plan.selected_models_to_backfill == {
'"jaffle_shop"."main"."orders"',
'"jaffle_shop"."main"."stg_orders"',
}
assert {s.name for s in plan.snapshots} == (
plan.selected_models_to_backfill | {standalone_audit_name}
)
plan = operations.run(exclude=["customers"])
assert plan.environment.name == "prod"
assert console.no_prompts is True
assert console.no_diff is True
assert console.auto_apply is True
assert plan.end_bounded is False
assert plan.ignore_cron is True
assert plan.skip_backfill is False
assert plan.selected_models_to_backfill == {k for k in operations.context.snapshots} - {
'"jaffle_shop"."main"."customers"'
} - {standalone_audit_name}
assert {s.name for s in plan.snapshots} == (
plan.selected_models_to_backfill | {standalone_audit_name}
)
plan = operations.run(empty=True)
assert plan.environment.name == "prod"
assert console.no_prompts is True
assert console.no_diff is True
assert console.auto_apply is True
assert plan.end_bounded is False
assert plan.ignore_cron is True
assert plan.skip_backfill is True
assert plan.selected_models_to_backfill is None
assert {s.name for s in plan.snapshots} == {k for k in operations.context.snapshots}
def test_run_option_mapping_dev(jaffle_shop_duckdb: Path):
# create prod so that dev has something to compare against
operations = create(project_dir=jaffle_shop_duckdb)
operations.run()
(jaffle_shop_duckdb / "models" / "new_model.sql").write_text("select 1")
operations = create(project_dir=jaffle_shop_duckdb)
console = PlanCapturingConsole()
operations.context.console = console
plan = operations.run(environment="dev")
assert plan.environment.name == "dev"
assert console.no_prompts is True
assert console.no_diff is True
assert console.auto_apply is True
assert plan.include_unmodified is False
assert plan.context_diff.create_from == "prod"
assert plan.context_diff.is_new_environment is True
assert console.plan_builder._enable_preview is True
assert plan.end_bounded is True
assert plan.ignore_cron is False
assert plan.skip_backfill is False
assert plan.selected_models_to_backfill == {'"jaffle_shop"."main"."new_model"'}
plan = operations.run(environment="dev", empty=True)
assert plan.environment.name == "dev"
assert console.no_prompts is True
assert console.no_diff is True
assert console.auto_apply is True
assert plan.include_unmodified is False
assert plan.context_diff.create_from == "prod"
assert plan.context_diff.is_new_environment is True
assert console.plan_builder._enable_preview is True
assert plan.end_bounded is True
assert plan.ignore_cron is False
assert plan.skip_backfill is True
assert plan.selected_models_to_backfill == {'"jaffle_shop"."main"."new_model"'}
plan = operations.run(environment="dev", select=["stg_orders+"])
assert plan.environment.name == "dev"
assert console.no_prompts is True
assert console.no_diff is True
assert console.auto_apply is True
assert plan.include_unmodified is False
assert plan.context_diff.create_from == "prod"
assert plan.context_diff.is_new_environment is True
assert console.plan_builder._enable_preview is True
# dev plans with --select have run=True, ignore_cron=True set
# as opposed to dev plans that dont have a specific selector
assert plan.end_bounded is False
assert plan.ignore_cron is True
assert plan.skip_backfill is False
# note: the new model in the dev environment is ignored in favour of the explicitly selected ones
assert plan.selected_models_to_backfill == {
'"jaffle_shop"."main"."customers"',
'"jaffle_shop"."main"."orders"',
'"jaffle_shop"."main"."stg_orders"',
}
@pytest.mark.parametrize(
"env_name,vde_mode",
[
("prod", VirtualEnvironmentMode.DEV_ONLY),
("prod", VirtualEnvironmentMode.FULL),
("dev", VirtualEnvironmentMode.DEV_ONLY),
("dev", VirtualEnvironmentMode.FULL),
],
)
def test_run_option_full_refresh(
create_empty_project: EmptyProjectCreator, env_name: str, vde_mode: VirtualEnvironmentMode
):
# create config file prior to load
project_path, models_path = create_empty_project(project_name="test")
config_path = project_path / "sqlmesh.yaml"
config = yaml.load(config_path)
config["virtual_environment_mode"] = vde_mode.value
with config_path.open("w") as f:
yaml.dump(config, f)
(models_path / "model_a.sql").write_text("select 1")
(models_path / "model_b.sql").write_text("select 2")
operations = create(project_dir=project_path)
assert operations.context.config.virtual_environment_mode == vde_mode
console = PlanCapturingConsole()
operations.context.console = console
plan = operations.run(environment=env_name, full_refresh=True)
# both models added as backfills + restatements regardless of env / vde mode setting
assert plan.environment.name == env_name
assert len(plan.restatements) == 2
assert list(plan.restatements)[0].name == '"test"."main"."model_a"'
assert list(plan.restatements)[1].name == '"test"."main"."model_b"'
assert plan.requires_backfill
assert not plan.empty_backfill
assert not plan.skip_backfill
assert plan.models_to_backfill == set(['"test"."main"."model_a"', '"test"."main"."model_b"'])
if vde_mode == VirtualEnvironmentMode.DEV_ONLY:
# We do not clear intervals across all model versions in the default DEV_ONLY mode, even when targeting prod,
# because dev data is hardcoded to preview only so by definition and can never be deployed
assert not plan.restate_all_snapshots
else:
if env_name == "prod":
# in FULL mode, we do it for prod
assert plan.restate_all_snapshots
else:
# but not dev
assert not plan.restate_all_snapshots
def test_run_option_full_refresh_with_selector(jaffle_shop_duckdb: Path):
operations = create(project_dir=jaffle_shop_duckdb)
assert len(operations.context.models) > 5
console = PlanCapturingConsole()
operations.context.console = console
plan = operations.run(select=["stg_customers"], full_refresh=True)
assert len(plan.restatements) == 1
assert list(plan.restatements)[0].name == '"jaffle_shop"."main"."stg_customers"'
assert plan.requires_backfill
assert not plan.empty_backfill
assert not plan.skip_backfill
assert plan.models_to_backfill == set(['"jaffle_shop"."main"."stg_customers"'])
def test_create_sets_concurrent_tasks_based_on_threads(create_empty_project: EmptyProjectCreator):
project_dir, _ = create_empty_project(project_name="test")
# add a postgres target because duckdb overrides to concurrent_tasks=1 regardless of what gets specified
profiles_yml_file = project_dir / "profiles.yml"
profiles_yml = yaml.load(profiles_yml_file)
profiles_yml["test"]["outputs"]["postgres"] = {
"type": "postgres",
"host": "localhost",
"port": 5432,
"user": "postgres",
"password": "postgres",
"dbname": "test",
"schema": "test",
}
profiles_yml_file.write_text(yaml.dump(profiles_yml))
operations = create(project_dir=project_dir, target="postgres")
assert operations.context.concurrent_tasks == 1 # 1 is the default
operations = create(project_dir=project_dir, threads=16, target="postgres")
assert operations.context.concurrent_tasks == 16
assert all(
g.connection and g.connection.concurrent_tasks == 16
for g in operations.context.config.gateways.values()
)
def test_create_configures_log_level(create_empty_project: EmptyProjectCreator):
project_dir, _ = create_empty_project()
create(project_dir=project_dir, log_level="info")
assert logging.getLogger("sqlmesh").getEffectiveLevel() == logging.INFO
create(project_dir=project_dir, log_level="error")
assert logging.getLogger("sqlmesh").getEffectiveLevel() == logging.ERROR