Skip to content

Commit ccd8651

Browse files
committed
Add SQL model with python macro on new test
1 parent 08398fd commit ccd8651

2 files changed

Lines changed: 41 additions & 13 deletions

File tree

sqlmesh/core/test/definition.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -544,13 +544,9 @@ def _concurrent_render_context(self) -> t.Iterator[None]:
544544
with lock_ctx, time_ctx, dialect_patch_ctx:
545545
yield
546546

547-
def _execute(self, query: exp.Query) -> pd.DataFrame:
547+
def _execute(self, query: exp.Query | str) -> pd.DataFrame:
548548
"""Executes the given query using the testing engine adapter and returns a DataFrame."""
549-
550-
with self._concurrent_render_context():
551-
sql = query.sql(self._test_adapter_dialect, pretty=self.engine_adapter._pretty_sql)
552-
553-
return self.engine_adapter.fetchdf(sql)
549+
return self.engine_adapter.fetchdf(query)
554550

555551
def _create_df(
556552
self,
@@ -611,7 +607,10 @@ def test_ctes(self, ctes: t.Dict[str, exp.Expression], recursive: bool = False)
611607
self.assert_equal(expected, actual, sort=sort, partial=partial)
612608

613609
def runTest(self) -> None:
614-
query = self._render_model_query()
610+
with self._concurrent_render_context():
611+
query = self._render_model_query()
612+
sql = query.sql(self._test_adapter_dialect, pretty=self.engine_adapter._pretty_sql)
613+
615614
with_clause = query.args.get("with")
616615

617616
if with_clause:
@@ -628,7 +627,7 @@ def runTest(self) -> None:
628627
partial = values.get("partial")
629628
sort = query.args.get("order") is None
630629

631-
actual = self._execute(query)
630+
actual = self._execute(sql)
632631
expected = self._create_df(values, columns=self.model.columns_to_types, partial=partial)
633632

634633
self.assert_equal(expected, actual, sort=sort, partial=partial)

tests/core/test_test.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,7 +2376,37 @@ def test_freeze_time_concurrent(tmp_path: Path) -> None:
23762376
tests_dir = tmp_path / "tests"
23772377
tests_dir.mkdir()
23782378

2379-
for model_name in ["sql_model", "py_model"]:
2379+
macros_dir = tmp_path / "macros"
2380+
macros_dir.mkdir()
2381+
2382+
macro_file = macros_dir / "test_datetime_now.py"
2383+
macro_file.write_text(
2384+
"""
2385+
from sqlglot import exp
2386+
import datetime
2387+
from sqlmesh.core.macros import macro
2388+
2389+
@macro()
2390+
def test_datetime_now(evaluator):
2391+
return exp.cast(exp.Literal.string(datetime.datetime.now(tz=datetime.timezone.utc)), exp.DataType.Type.DATE)
2392+
2393+
@macro()
2394+
def test_sqlglot_expr(evaluator):
2395+
return exp.CurrentDate().sql(evaluator.dialect)
2396+
"""
2397+
)
2398+
2399+
models_dir = tmp_path / "models"
2400+
models_dir.mkdir()
2401+
sql_model1 = models_dir / "sql_model1.sql"
2402+
sql_model1.write_text(
2403+
"""
2404+
MODEL(NAME sql_model1);
2405+
SELECT @test_datetime_now() AS col_exec_ds_time, @test_sqlglot_expr() AS col_current_date;
2406+
"""
2407+
)
2408+
2409+
for model_name in ["sql_model1", "sql_model2", "py_model"]:
23802410
for i in range(5):
23812411
test_2019 = tmp_path / "tests" / f"test_2019_{model_name}_{i}.yaml"
23822412
test_2019.write_text(
@@ -2428,16 +2458,15 @@ def execute(context, start, end, execution_time, **kwargs):
24282458
)
24292459

24302460
python_model = model.get_registry()["py_model"].model(module_path=Path("."), path=Path("."))
2461+
ctx.upsert_model(python_model)
24312462

24322463
ctx.upsert_model(
24332464
_create_model(
2434-
meta="MODEL(NAME sql_model)",
2465+
meta="MODEL(NAME sql_model2)",
24352466
query="SELECT @execution_ds::timestamp_ntz AS col_exec_ds_time, current_date()::date AS col_current_date",
24362467
default_catalog=ctx.default_catalog,
24372468
)
24382469
)
24392470

2440-
ctx.upsert_model(python_model)
2441-
24422471
results = ctx.test()
2443-
assert len(results.successes) == 20
2472+
assert len(results.successes) == 30

0 commit comments

Comments
 (0)