Skip to content

Commit f720c14

Browse files
authored
Fix: Add tests that validate correctness of data previews in dev for models which only support full history restatement (#2740)
1 parent 58a7fd7 commit f720c14

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

sqlmesh/core/plan/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ def _apply_effective_from(self) -> None:
644644
raise PlanError("Effective date cannot be in the future.")
645645

646646
for snapshot in self._context_diff.new_snapshots.values():
647-
if not snapshot.disable_restatement:
647+
if not snapshot.disable_restatement and not snapshot.full_history_restatement_only:
648648
snapshot.effective_from = self._effective_from
649649

650650
def _is_forward_only_change(self, s_id: SnapshotId) -> bool:

tests/core/test_integration.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,66 @@ def test_forward_only_model_regular_plan_preview_enabled(init_and_plan_context:
335335
assert dev_df["event_date"].tolist() == [pd.to_datetime("2023-01-07")]
336336

337337

338+
@freeze_time("2023-01-08 15:00:00")
339+
def test_full_history_restatement_model_regular_plan_preview_enabled(
340+
init_and_plan_context: t.Callable,
341+
):
342+
context, plan = init_and_plan_context("examples/sushi")
343+
context.apply(plan)
344+
345+
model_name = "sushi.marketing" # SCD2 model
346+
347+
model = context.get_model(model_name)
348+
model = add_projection_to_model(t.cast(SqlModel, model))
349+
350+
context.upsert_model(model)
351+
snapshot = context.get_snapshot(model, raise_if_missing=True)
352+
customers_snapshot = context.get_snapshot("sushi.customers", raise_if_missing=True)
353+
waiter_as_customer_snapshot = context.get_snapshot(
354+
"sushi.waiter_as_customer_by_day", raise_if_missing=True
355+
)
356+
357+
plan = context.plan("dev", no_prompts=True, skip_tests=True, enable_preview=True)
358+
359+
assert len(plan.new_snapshots) == 3
360+
assert (
361+
plan.context_diff.snapshots[snapshot.snapshot_id].change_category
362+
== SnapshotChangeCategory.FORWARD_ONLY
363+
)
364+
assert (
365+
plan.context_diff.snapshots[customers_snapshot.snapshot_id].change_category
366+
== SnapshotChangeCategory.FORWARD_ONLY
367+
)
368+
assert (
369+
plan.context_diff.snapshots[waiter_as_customer_snapshot.snapshot_id].change_category
370+
== SnapshotChangeCategory.FORWARD_ONLY
371+
)
372+
373+
assert plan.start == to_date("2023-01-07")
374+
assert plan.missing_intervals == [
375+
SnapshotIntervals(
376+
snapshot_id=customers_snapshot.snapshot_id,
377+
intervals=[
378+
(to_timestamp("2023-01-07"), to_timestamp("2023-01-08")),
379+
],
380+
),
381+
SnapshotIntervals(
382+
snapshot_id=snapshot.snapshot_id,
383+
intervals=[
384+
(to_timestamp("2023-01-07"), to_timestamp("2023-01-08")),
385+
],
386+
),
387+
SnapshotIntervals(
388+
snapshot_id=waiter_as_customer_snapshot.snapshot_id,
389+
intervals=[
390+
(to_timestamp("2023-01-07"), to_timestamp("2023-01-08")),
391+
],
392+
),
393+
]
394+
395+
context.apply(plan)
396+
397+
338398
@freeze_time("2023-01-08 15:00:00")
339399
def test_hourly_model_with_lookback_no_backfill_in_dev(init_and_plan_context: t.Callable):
340400
context, plan = init_and_plan_context("examples/sushi")

tests/core/test_snapshot.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,14 @@ def test_get_removal_intervals_full_history_restatement_model(make_snapshot):
611611
snapshot = make_snapshot(model)
612612
snapshot.add_interval("2020-01-01", "2024-01-01")
613613

614+
interval = snapshot.get_removal_interval(
615+
"2023-01-01",
616+
"2023-01-01",
617+
execution_time=execution_time,
618+
is_preview=True,
619+
)
620+
assert interval == (to_timestamp("2023-01-01"), execution_time)
621+
614622
interval = snapshot.get_removal_interval(
615623
"2023-01-01", "2023-01-01", execution_time=execution_time
616624
)

0 commit comments

Comments
 (0)