Skip to content

Commit 577b308

Browse files
committed
refactor(sql): update full model SQL and enhance test cases
- Change SQL model definition to use lowercase for `kind` and uppercase for `NAME`. - Add `item_name` to the SELECT statement and include `last_updated_at` in the output. - Update test configuration to exclude `last_updated_at` and mark the output as partial. - Improve test assertions by comparing the full model DataFrame against an expected DataFrame. - Ensure consistency in test context usage and enhance clarity in test logic.
1 parent ecdb4d4 commit 577b308

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
MODEL (
2-
name sqlmesh_example.full_model,
3-
kind FULL,
1+
model (
2+
NAME sqlmesh_example.full_model,
3+
kind full,
44
cron '@daily',
55
grain item_id,
66
audits (assert_positive_order_ids),
@@ -9,10 +9,14 @@ MODEL (
99
"full",
1010
)
1111
);
12-
1312
SELECT
1413
item_id,
15-
COUNT(DISTINCT id) AS num_orders,
14+
item_name,
15+
COUNT(
16+
DISTINCT id
17+
) AS num_orders,
18+
now() AS last_updated_at
1619
FROM
1720
sqlmesh_example.intermediate_model_1
18-
GROUP BY ALL
21+
GROUP BY
22+
ALL

sample/sqlmesh_project/tests/test_full_model.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
test_example_full_model:
22
model: sqlmesh_example.full_model
3+
exclude_columns: [last_updated_at]
34
inputs:
45
sqlmesh_example.intermediate_model_1:
56
rows:
@@ -11,6 +12,7 @@ test_example_full_model:
1112
item_id: 2
1213
outputs:
1314
query:
15+
partial: true
1416
rows:
1517
- item_id: 1
1618
num_orders: 2

tests/context/plan_and_run/test_model_code_change.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
22

3+
import pandas as pd
34
import pytest
5+
from pandas.testing import assert_frame_equal
46

57
from dagster_sqlmesh.controller.base import PlanOptions
68
from tests.conftest import SQLMeshTestContext
@@ -59,7 +61,6 @@ def test_given_model_chain_when_running_with_different_flags_then_behaves_as_exp
5961
environment="dev",
6062
)
6163

62-
6364
print(
6465
f"intermediate_model_1 first run: {
6566
sample_sqlmesh_test_context.query(
@@ -95,23 +96,22 @@ def test_given_model_chain_when_running_with_different_flags_then_behaves_as_exp
9596
main.id,
9697
main.item_id,
9798
main.event_date,
98-
CONCAT(sub.item_name, ' - modified1') as item_name
99+
CONCAT('item - ', main.item_id) as item_name
99100
FROM sqlmesh_example.staging_model_1 AS main
100101
INNER JOIN sqlmesh_example.staging_model_2 as sub
101102
ON main.id = sub.id
102103
WHERE
103104
event_date BETWEEN @start_date AND @end_date
104-
105105
""",
106106
)
107107

108-
109108
# Run with specified flags
110109
sample_sqlmesh_test_context.plan_and_run(
111110
environment="dev",
112111
plan_options=PlanOptions(
113112
skip_backfill=skip_backfill,
114113
enable_preview=True,
114+
skip_tests=True,
115115
),
116116
)
117117

@@ -133,8 +133,40 @@ def test_given_model_chain_when_running_with_different_flags_then_behaves_as_exp
133133
}"
134134
)
135135

136-
raise Exception("Stop here")
136+
full_model_df = (
137+
sample_sqlmesh_test_context.query(
138+
"""
139+
SELECT *
140+
FROM sqlmesh_example__dev.full_model
141+
""",
142+
return_df=True,
143+
)
144+
.sort_values(by="item_id")
145+
.reset_index(drop=True)
146+
)
147+
148+
expected_full_model_df = (
149+
pd.DataFrame(
150+
{
151+
"item_id": pd.Series([1, 2, 3], dtype="int32"),
152+
"item_name": ["item - 1", "item - 2", "item - 3"],
153+
"num_orders": [5, 1, 1],
154+
}
155+
)
156+
.sort_values(by="item_id")
157+
.reset_index(drop=True)
158+
)
137159

160+
print("full_model_df")
161+
print(full_model_df.drop(columns=["last_updated_at"]))
162+
print("expected_full_model_df")
163+
print(expected_full_model_df)
164+
165+
assert_frame_equal(
166+
full_model_df.drop(columns=["last_updated_at"]),
167+
expected_full_model_df,
168+
check_like=True,
169+
)
138170

139171

140172
if __name__ == "__main__":

0 commit comments

Comments
 (0)