diff --git a/sqlmesh/core/test/definition.py b/sqlmesh/core/test/definition.py index a3179ef6ee..1c9807cfa1 100644 --- a/sqlmesh/core/test/definition.py +++ b/sqlmesh/core/test/definition.py @@ -807,7 +807,7 @@ def runTest(self) -> None: actual_df.reset_index(drop=True, inplace=True) expected = self._create_df(values, columns=self.model.columns_to_types, partial=partial) - self.assert_equal(expected, actual_df, sort=False, partial=partial) + self.assert_equal(expected, actual_df, sort=True, partial=partial) def _execute_model(self) -> pd.DataFrame: """Executes the python model and returns a DataFrame.""" diff --git a/tests/core/test_test.py b/tests/core/test_test.py index 13e31703a1..c5df73d810 100644 --- a/tests/core/test_test.py +++ b/tests/core/test_test.py @@ -3367,6 +3367,56 @@ def execute(context: ExecutionContext, **kwargs: t.Any) -> pd.DataFrame: _check_successful_or_raise(test_default_vars.run()) +def test_python_model_sorting(tmp_path: Path) -> None: + py_model = tmp_path / "models" / "test_sort_model.py" + py_model.parent.mkdir(parents=True, exist_ok=True) + py_model.write_text( + """ +import pandas as pd # noqa: TID253 +from sqlmesh import model, ExecutionContext +import typing as t + +@model( + name="test_sort_model", + columns={"id": "int", "value": "varchar"}, +) +def execute(context: ExecutionContext, **kwargs: t.Any) -> pd.DataFrame: + # Return rows in a potentially non-deterministic order + # (simulating a model that doesn't guarantee order) + return pd.DataFrame([ + {"id": 3, "value": "c"}, + {"id": 1, "value": "a"}, + {"id": 2, "value": "b"}, + ])""" + ) + + config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb")) + context = Context(config=config, paths=tmp_path) + + python_model = context.models['"test_sort_model"'] + + _check_successful_or_raise( + _create_test( + body=load_yaml(""" + test_without_sort: + model: test_sort_model + outputs: + query: + rows: + - id: 1 + value: "a" + - id: 2 + value: "b" + - id: 3 + value: "c" + """), + test_name="test_without_sort", + model=python_model, + context=context, + ).run() + ) + + @use_terminal_console def test_cte_failure(tmp_path: Path) -> None: models_dir = tmp_path / "models"