Skip to content

Commit a99bfaa

Browse files
authored
Fix: always sort Python model dfs in unit tests (#5575)
1 parent a1b21e3 commit a99bfaa

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

sqlmesh/core/test/definition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ def runTest(self) -> None:
807807
actual_df.reset_index(drop=True, inplace=True)
808808
expected = self._create_df(values, columns=self.model.columns_to_types, partial=partial)
809809

810-
self.assert_equal(expected, actual_df, sort=False, partial=partial)
810+
self.assert_equal(expected, actual_df, sort=True, partial=partial)
811811

812812
def _execute_model(self) -> pd.DataFrame:
813813
"""Executes the python model and returns a DataFrame."""

tests/core/test_test.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3367,6 +3367,56 @@ def execute(context: ExecutionContext, **kwargs: t.Any) -> pd.DataFrame:
33673367
_check_successful_or_raise(test_default_vars.run())
33683368

33693369

3370+
def test_python_model_sorting(tmp_path: Path) -> None:
3371+
py_model = tmp_path / "models" / "test_sort_model.py"
3372+
py_model.parent.mkdir(parents=True, exist_ok=True)
3373+
py_model.write_text(
3374+
"""
3375+
import pandas as pd # noqa: TID253
3376+
from sqlmesh import model, ExecutionContext
3377+
import typing as t
3378+
3379+
@model(
3380+
name="test_sort_model",
3381+
columns={"id": "int", "value": "varchar"},
3382+
)
3383+
def execute(context: ExecutionContext, **kwargs: t.Any) -> pd.DataFrame:
3384+
# Return rows in a potentially non-deterministic order
3385+
# (simulating a model that doesn't guarantee order)
3386+
return pd.DataFrame([
3387+
{"id": 3, "value": "c"},
3388+
{"id": 1, "value": "a"},
3389+
{"id": 2, "value": "b"},
3390+
])"""
3391+
)
3392+
3393+
config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb"))
3394+
context = Context(config=config, paths=tmp_path)
3395+
3396+
python_model = context.models['"test_sort_model"']
3397+
3398+
_check_successful_or_raise(
3399+
_create_test(
3400+
body=load_yaml("""
3401+
test_without_sort:
3402+
model: test_sort_model
3403+
outputs:
3404+
query:
3405+
rows:
3406+
- id: 1
3407+
value: "a"
3408+
- id: 2
3409+
value: "b"
3410+
- id: 3
3411+
value: "c"
3412+
"""),
3413+
test_name="test_without_sort",
3414+
model=python_model,
3415+
context=context,
3416+
).run()
3417+
)
3418+
3419+
33703420
@use_terminal_console
33713421
def test_cte_failure(tmp_path: Path) -> None:
33723422
models_dir = tmp_path / "models"

0 commit comments

Comments
 (0)