From bd49e17cb740d0e6eb5695656ec066b24097516e Mon Sep 17 00:00:00 2001 From: George Sittas Date: Mon, 9 Jun 2025 19:56:11 +0300 Subject: [PATCH] Fix: normalize datetime values to strings in unit tests --- sqlmesh/core/test/definition.py | 11 ++++++++++- tests/core/test_test.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/sqlmesh/core/test/definition.py b/sqlmesh/core/test/definition.py index a766706801..e43b8b215c 100644 --- a/sqlmesh/core/test/definition.py +++ b/sqlmesh/core/test/definition.py @@ -258,12 +258,21 @@ def assert_equal( actual = actual.replace({np.nan: None}) expected = expected.replace({np.nan: None}) + # We define this here to avoid a top-level import of numpy and pandas + DATETIME_TYPES = ( + datetime.datetime, + datetime.date, + datetime.time, + np.datetime64, + pd.Timestamp, + ) + def _to_hashable(x: t.Any) -> t.Any: if isinstance(x, (list, np.ndarray)): return tuple(_to_hashable(v) for v in x) if isinstance(x, dict): return tuple((k, _to_hashable(v)) for k, v in x.items()) - return str(x) if not isinstance(x, t.Hashable) else x + return str(x) if isinstance(x, DATETIME_TYPES) or not isinstance(x, t.Hashable) else x actual = actual.apply(lambda col: col.map(_to_hashable)) expected = expected.apply(lambda col: col.map(_to_hashable)) diff --git a/tests/core/test_test.py b/tests/core/test_test.py index a05e66e48f..9478f4aa6b 100644 --- a/tests/core/test_test.py +++ b/tests/core/test_test.py @@ -2609,3 +2609,34 @@ def test_model_test_text_result_reporting_no_traceback( prefix = "ERROR" if is_error else "FAIL" assert f"{prefix}: test_foo (None)" in output assert "Exception: failure" in output + + +def test_timestamp_normalization() -> None: + model = _create_model( + "SELECT id, array_agg(timestamp_col::timestamp) as agg_timestamp_col FROM temp_model_with_timestamp GROUP BY id", + meta="MODEL (name foo, kind FULL)", + ) + + _check_successful_or_raise( + _create_test( + body=load_yaml( + """ + test_foo: + model: temp_agg_model_with_timestamp + inputs: + temp_model_with_timestamp: + rows: + - id: "id1" + timestamp_col: "2024-01-02T15:00:00" + outputs: + query: + rows: + - id: id1 + agg_timestamp_col: ["2024-01-02T15:00:00.000000"] + """ + ), + test_name="test_foo", + model=model, + context=Context(config=Config(model_defaults=ModelDefaultsConfig(dialect="duckdb"))), + ).run() + )