-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathtest_plan_options.py
More file actions
529 lines (429 loc) · 18.5 KB
/
test_plan_options.py
File metadata and controls
529 lines (429 loc) · 18.5 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
from __future__ import annotations
import typing as t
import pytest
from sqlmesh.core.console import (
set_console,
get_console,
TerminalConsole,
)
import time_machine
from sqlmesh.core import dialect as d
from sqlmesh.core.console import get_console
from sqlmesh.core.model import (
SqlModel,
load_sql_based_model,
)
from sqlmesh.core.plan import SnapshotIntervals
from sqlmesh.core.snapshot import (
SnapshotChangeCategory,
)
from sqlmesh.utils.date import to_datetime, to_timestamp
from sqlmesh.utils.errors import (
NoChangesPlanError,
)
from tests.core.integration.utils import (
add_projection_to_model,
)
pytestmark = pytest.mark.slow
@time_machine.travel("2023-01-08 15:00:00 UTC")
def test_empty_backfill(init_and_plan_context: t.Callable):
context, _ = init_and_plan_context("examples/sushi")
plan = context.plan_builder("prod", skip_tests=True, empty_backfill=True).build()
assert plan.missing_intervals
assert plan.empty_backfill
assert not plan.requires_backfill
context.apply(plan)
for model in context.models.values():
if model.is_seed or model.kind.is_symbolic:
continue
row_num = context.engine_adapter.fetchone(f"SELECT COUNT(*) FROM {model.name}")[0]
assert row_num == 0
plan = context.plan_builder("prod", skip_tests=True).build()
assert not plan.requires_backfill
assert not plan.has_changes
assert not plan.missing_intervals
snapshots = plan.snapshots
for snapshot in snapshots.values():
if not snapshot.intervals:
continue
assert snapshot.intervals[-1][1] <= to_timestamp("2023-01-08")
@time_machine.travel("2023-01-08 15:00:00 UTC")
def test_empty_backfill_new_model(init_and_plan_context: t.Callable):
context, plan = init_and_plan_context("examples/sushi")
context.apply(plan)
new_model = load_sql_based_model(
d.parse(
"""
MODEL (
name memory.sushi.new_model,
kind FULL,
cron '0 8 * * *',
start '2023-01-01',
);
SELECT 1 AS one;
"""
)
)
new_model_name = context.upsert_model(new_model).fqn
with time_machine.travel("2023-01-09 00:00:00 UTC"):
plan = context.plan_builder("dev", skip_tests=True, empty_backfill=True).build()
assert plan.end == to_datetime("2023-01-09")
assert plan.missing_intervals
assert plan.empty_backfill
assert not plan.requires_backfill
context.apply(plan)
for model in context.models.values():
if model.is_seed or model.kind.is_symbolic:
continue
row_num = context.engine_adapter.fetchone(f"SELECT COUNT(*) FROM sushi__dev.new_model")[
0
]
assert row_num == 0
plan = context.plan_builder("prod", skip_tests=True).build()
assert not plan.requires_backfill
assert not plan.missing_intervals
snapshots = plan.snapshots
for snapshot in snapshots.values():
if not snapshot.intervals:
continue
elif snapshot.name == new_model_name:
assert snapshot.intervals[-1][1] == to_timestamp("2023-01-09")
else:
assert snapshot.intervals[-1][1] <= to_timestamp("2023-01-08")
@time_machine.travel("2023-01-08 15:00:00 UTC")
def test_plan_explain(init_and_plan_context: t.Callable):
old_console = get_console()
set_console(TerminalConsole())
context, plan = init_and_plan_context("examples/sushi")
context.apply(plan)
waiter_revenue_by_day_model = context.get_model("sushi.waiter_revenue_by_day")
waiter_revenue_by_day_model = add_projection_to_model(
t.cast(SqlModel, waiter_revenue_by_day_model)
)
context.upsert_model(waiter_revenue_by_day_model)
waiter_revenue_by_day_snapshot = context.get_snapshot(waiter_revenue_by_day_model.name)
top_waiters_snapshot = context.get_snapshot("sushi.top_waiters")
common_kwargs = dict(skip_tests=True, no_prompts=True, explain=True)
# For now just making sure the plan doesn't error
context.plan("dev", **common_kwargs)
context.plan("dev", **common_kwargs, skip_backfill=True)
context.plan("dev", **common_kwargs, empty_backfill=True)
context.plan("dev", **common_kwargs, forward_only=True, enable_preview=True)
context.plan("prod", **common_kwargs)
context.plan("prod", **common_kwargs, forward_only=True)
context.plan("prod", **common_kwargs, restate_models=[waiter_revenue_by_day_model.name])
set_console(old_console)
# Make sure that the now changes were actually applied
for target_env in ("dev", "prod"):
plan = context.plan_builder(target_env, skip_tests=True).build()
assert plan.has_changes
assert plan.missing_intervals
assert plan.directly_modified == {waiter_revenue_by_day_snapshot.snapshot_id}
assert len(plan.new_snapshots) == 2
assert {s.snapshot_id for s in plan.new_snapshots} == {
waiter_revenue_by_day_snapshot.snapshot_id,
top_waiters_snapshot.snapshot_id,
}
@time_machine.travel("2023-01-08 15:00:00 UTC")
def test_plan_ignore_cron(
init_and_plan_context: t.Callable,
):
context, _ = init_and_plan_context("examples/sushi")
expressions = d.parse(
f"""
MODEL (
name memory.sushi.test_allow_partials,
kind INCREMENTAL_UNMANAGED,
allow_partials true,
start '2023-01-01',
);
SELECT @end_ts AS end_ts
"""
)
model = load_sql_based_model(expressions)
context.upsert_model(model)
context.plan("prod", skip_tests=True, auto_apply=True, no_prompts=True)
assert (
context.engine_adapter.fetchone("SELECT MAX(end_ts) FROM memory.sushi.test_allow_partials")[
0
]
== "2023-01-07 23:59:59.999999"
)
plan_no_ignore_cron = context.plan_builder(
"prod", run=True, ignore_cron=False, skip_tests=True
).build()
assert not plan_no_ignore_cron.missing_intervals
plan = context.plan_builder("prod", run=True, ignore_cron=True, skip_tests=True).build()
assert plan.missing_intervals == [
SnapshotIntervals(
snapshot_id=context.get_snapshot(model, raise_if_missing=True).snapshot_id,
intervals=[
(to_timestamp("2023-01-08"), to_timestamp("2023-01-08 15:00:00")),
],
)
]
context.apply(plan)
assert (
context.engine_adapter.fetchone("SELECT MAX(end_ts) FROM memory.sushi.test_allow_partials")[
0
]
== "2023-01-08 14:59:59.999999"
)
@time_machine.travel("2023-01-08 15:00:00 UTC")
def test_plan_with_run(
init_and_plan_context: t.Callable,
):
context, plan = init_and_plan_context("examples/sushi")
context.apply(plan)
model = context.get_model("sushi.waiter_revenue_by_day")
context.upsert_model(add_projection_to_model(t.cast(SqlModel, model)))
with time_machine.travel("2023-01-09 00:00:00 UTC"):
plan = context.plan(run=True)
assert plan.has_changes
assert plan.missing_intervals
context.apply(plan)
snapshots = context.state_sync.state_sync.get_snapshots(context.snapshots.values())
assert {s.name: s.intervals[0][1] for s in snapshots.values() if s.intervals} == {
'"memory"."sushi"."waiter_revenue_by_day"': to_timestamp("2023-01-09"),
'"memory"."sushi"."order_items"': to_timestamp("2023-01-09"),
'"memory"."sushi"."orders"': to_timestamp("2023-01-09"),
'"memory"."sushi"."items"': to_timestamp("2023-01-09"),
'"memory"."sushi"."customer_revenue_lifetime"': to_timestamp("2023-01-09"),
'"memory"."sushi"."customer_revenue_by_day"': to_timestamp("2023-01-09"),
'"memory"."sushi"."latest_order"': to_timestamp("2023-01-09"),
'"memory"."sushi"."waiter_names"': to_timestamp("2023-01-08"),
'"memory"."sushi"."raw_marketing"': to_timestamp("2023-01-09"),
'"memory"."sushi"."marketing"': to_timestamp("2023-01-09"),
'"memory"."sushi"."waiter_as_customer_by_day"': to_timestamp("2023-01-09"),
'"memory"."sushi"."top_waiters"': to_timestamp("2023-01-09"),
'"memory"."raw"."demographics"': to_timestamp("2023-01-09"),
"assert_item_price_above_zero": to_timestamp("2023-01-09"),
'"memory"."sushi"."active_customers"': to_timestamp("2023-01-09"),
'"memory"."sushi"."customers"': to_timestamp("2023-01-09"),
'"memory"."sushi"."count_customers_active"': to_timestamp("2023-01-09"),
'"memory"."sushi"."count_customers_inactive"': to_timestamp("2023-01-09"),
}
@time_machine.travel("2023-01-08 15:00:00 UTC")
def test_select_models(init_and_plan_context: t.Callable):
context, plan = init_and_plan_context("examples/sushi")
context.apply(plan)
# Modify 2 models.
model = context.get_model("sushi.waiter_revenue_by_day")
kwargs = {
**model.dict(),
# Make a breaking change.
"query": model.query.order_by("waiter_id"), # type: ignore
}
context.upsert_model(SqlModel.parse_obj(kwargs))
model = context.get_model("sushi.customer_revenue_by_day")
context.upsert_model(add_projection_to_model(t.cast(SqlModel, model)))
expected_intervals = [
(to_timestamp("2023-01-01"), to_timestamp("2023-01-02")),
(to_timestamp("2023-01-02"), to_timestamp("2023-01-03")),
(to_timestamp("2023-01-03"), to_timestamp("2023-01-04")),
(to_timestamp("2023-01-04"), to_timestamp("2023-01-05")),
(to_timestamp("2023-01-05"), to_timestamp("2023-01-06")),
(to_timestamp("2023-01-06"), to_timestamp("2023-01-07")),
(to_timestamp("2023-01-07"), to_timestamp("2023-01-08")),
]
waiter_revenue_by_day_snapshot_id = context.get_snapshot(
"sushi.waiter_revenue_by_day", raise_if_missing=True
).snapshot_id
# Select one of the modified models.
plan_builder = context.plan_builder(
"dev", select_models=["*waiter_revenue_by_day"], skip_tests=True
)
snapshot = plan_builder._context_diff.snapshots[waiter_revenue_by_day_snapshot_id]
plan_builder.set_choice(snapshot, SnapshotChangeCategory.BREAKING)
plan = plan_builder.build()
assert plan.missing_intervals == [
SnapshotIntervals(
snapshot_id=waiter_revenue_by_day_snapshot_id,
intervals=expected_intervals,
),
]
context.apply(plan)
dev_df = context.engine_adapter.fetchdf(
"SELECT DISTINCT event_date FROM sushi__dev.waiter_revenue_by_day ORDER BY event_date"
)
assert len(dev_df) == 7
# Make sure that we only create a view for the selected model.
schema_objects = context.engine_adapter.get_data_objects("sushi__dev")
assert len(schema_objects) == 1
assert schema_objects[0].name == "waiter_revenue_by_day"
# Validate the other modified model.
assert not context.get_snapshot("sushi.customer_revenue_by_day").change_category
assert not context.get_snapshot("sushi.customer_revenue_by_day").version
# Validate the downstream model.
assert not context.engine_adapter.table_exists(
context.get_snapshot("sushi.top_waiters").table_name()
)
assert not context.engine_adapter.table_exists(
context.get_snapshot("sushi.top_waiters").table_name(False)
)
# Make sure that tables are created when deploying to prod.
plan = context.plan("prod", skip_tests=True)
context.apply(plan)
assert context.engine_adapter.table_exists(
context.get_snapshot("sushi.top_waiters").table_name()
)
@time_machine.travel("2023-01-08 15:00:00 UTC")
def test_select_models_for_backfill(init_and_plan_context: t.Callable):
context, _ = init_and_plan_context("examples/sushi")
expected_intervals = [
(to_timestamp("2023-01-07"), to_timestamp("2023-01-08")),
]
plan = context.plan_builder(
"dev", backfill_models=["+*waiter_revenue_by_day"], skip_tests=True
).build()
assert plan.missing_intervals == [
SnapshotIntervals(
snapshot_id=context.get_snapshot("sushi.items", raise_if_missing=True).snapshot_id,
intervals=expected_intervals,
),
SnapshotIntervals(
snapshot_id=context.get_snapshot(
"sushi.order_items", raise_if_missing=True
).snapshot_id,
intervals=expected_intervals,
),
SnapshotIntervals(
snapshot_id=context.get_snapshot("sushi.orders", raise_if_missing=True).snapshot_id,
intervals=expected_intervals,
),
SnapshotIntervals(
snapshot_id=context.get_snapshot(
"sushi.waiter_revenue_by_day", raise_if_missing=True
).snapshot_id,
intervals=expected_intervals,
),
]
context.apply(plan)
dev_df = context.engine_adapter.fetchdf(
"SELECT DISTINCT event_date FROM sushi__dev.waiter_revenue_by_day ORDER BY event_date"
)
assert len(dev_df) == 1
schema_objects = context.engine_adapter.get_data_objects("sushi__dev")
assert {o.name for o in schema_objects} == {
"items",
"order_items",
"orders",
"waiter_revenue_by_day",
}
assert not context.engine_adapter.table_exists(
context.get_snapshot("sushi.customer_revenue_by_day").table_name()
)
# Make sure that tables are created when deploying to prod.
plan = context.plan("prod")
context.apply(plan)
assert context.engine_adapter.table_exists(
context.get_snapshot("sushi.customer_revenue_by_day").table_name()
)
@time_machine.travel("2023-01-08 15:00:00 UTC")
def test_select_unchanged_model_for_backfill(init_and_plan_context: t.Callable):
context, plan = init_and_plan_context("examples/sushi")
context.apply(plan)
# Modify 2 models.
model = context.get_model("sushi.waiter_revenue_by_day")
kwargs = {
**model.dict(),
# Make a breaking change.
"query": d.parse_one(
f"{model.query.sql(dialect='duckdb')} ORDER BY waiter_id", dialect="duckdb"
),
}
context.upsert_model(SqlModel.parse_obj(kwargs))
model = context.get_model("sushi.customer_revenue_by_day")
context.upsert_model(add_projection_to_model(t.cast(SqlModel, model)))
expected_intervals = [
(to_timestamp("2023-01-01"), to_timestamp("2023-01-02")),
(to_timestamp("2023-01-02"), to_timestamp("2023-01-03")),
(to_timestamp("2023-01-03"), to_timestamp("2023-01-04")),
(to_timestamp("2023-01-04"), to_timestamp("2023-01-05")),
(to_timestamp("2023-01-05"), to_timestamp("2023-01-06")),
(to_timestamp("2023-01-06"), to_timestamp("2023-01-07")),
(to_timestamp("2023-01-07"), to_timestamp("2023-01-08")),
]
waiter_revenue_by_day_snapshot_id = context.get_snapshot(
"sushi.waiter_revenue_by_day", raise_if_missing=True
).snapshot_id
# Select one of the modified models.
plan_builder = context.plan_builder(
"dev", select_models=["*waiter_revenue_by_day"], skip_tests=True
)
snapshot = plan_builder._context_diff.snapshots[waiter_revenue_by_day_snapshot_id]
plan_builder.set_choice(snapshot, SnapshotChangeCategory.BREAKING)
plan = plan_builder.build()
assert plan.missing_intervals == [
SnapshotIntervals(
snapshot_id=waiter_revenue_by_day_snapshot_id,
intervals=expected_intervals,
),
]
context.apply(plan)
# Make sure that we only create a view for the selected model.
schema_objects = context.engine_adapter.get_data_objects("sushi__dev")
assert {o.name for o in schema_objects} == {"waiter_revenue_by_day"}
# Now select a model downstream from the previously modified one in order to backfill it.
plan = context.plan_builder("dev", select_models=["*top_waiters"], skip_tests=True).build()
assert not plan.has_changes
assert plan.missing_intervals == [
SnapshotIntervals(
snapshot_id=context.get_snapshot(
"sushi.top_waiters", raise_if_missing=True
).snapshot_id,
intervals=expected_intervals,
),
]
context.apply(plan)
# Make sure that a view has been created for the downstream selected model.
schema_objects = context.engine_adapter.get_data_objects("sushi__dev")
assert {o.name for o in schema_objects} == {"waiter_revenue_by_day", "top_waiters"}
@time_machine.travel("2023-01-08 15:00:00 UTC")
def test_create_environment_no_changes_with_selector(init_and_plan_context: t.Callable):
context, plan = init_and_plan_context("examples/sushi")
context.apply(plan)
with pytest.raises(NoChangesPlanError):
context.plan_builder("dev").build()
plan = context.plan_builder("dev", select_models=["*top_waiters"]).build()
assert not plan.missing_intervals
context.apply(plan)
schema_objects = context.engine_adapter.get_data_objects("sushi__dev")
assert {o.name for o in schema_objects} == {"top_waiters"}
@time_machine.travel("2023-01-08 15:00:00 UTC")
def test_include_unmodified(init_and_plan_context: t.Callable):
context, plan = init_and_plan_context("examples/sushi")
context.apply(plan)
plan = context.plan_builder(
"dev",
include_unmodified=True,
skip_tests=True,
).build()
all_snapshots = context.snapshots
assert len(plan.environment.snapshots) == len(all_snapshots)
assert plan.environment.promoted_snapshot_ids is None
context.apply(plan)
data_objs = context.engine_adapter.get_data_objects("sushi__dev")
assert len(data_objs) == len(
[s for s in all_snapshots.values() if s.is_model and not s.is_symbolic]
)
@time_machine.travel("2023-01-08 15:00:00 UTC")
def test_select_models_with_include_unmodified(init_and_plan_context: t.Callable):
context, plan = init_and_plan_context("examples/sushi")
context.apply(plan)
plan = context.plan_builder(
"dev",
select_models=["*top_waiters", "*customer_revenue_by_day"],
include_unmodified=True,
skip_tests=True,
).build()
assert len(plan.environment.snapshots) == len(context.snapshots)
promoted_set = {s_id.name for s_id in plan.environment.promoted_snapshot_ids}
assert promoted_set == {
'"memory"."sushi"."customer_revenue_by_day"',
'"memory"."sushi"."top_waiters"',
}
context.apply(plan)
data_objs = context.engine_adapter.get_data_objects("sushi__dev")
assert len(data_objs) == 2
assert {o.name for o in data_objs} == {"customer_revenue_by_day", "top_waiters"}