Skip to content

Commit bd49e17

Browse files
committed
Fix: normalize datetime values to strings in unit tests
1 parent f87d3f0 commit bd49e17

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

sqlmesh/core/test/definition.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,21 @@ def assert_equal(
258258
actual = actual.replace({np.nan: None})
259259
expected = expected.replace({np.nan: None})
260260

261+
# We define this here to avoid a top-level import of numpy and pandas
262+
DATETIME_TYPES = (
263+
datetime.datetime,
264+
datetime.date,
265+
datetime.time,
266+
np.datetime64,
267+
pd.Timestamp,
268+
)
269+
261270
def _to_hashable(x: t.Any) -> t.Any:
262271
if isinstance(x, (list, np.ndarray)):
263272
return tuple(_to_hashable(v) for v in x)
264273
if isinstance(x, dict):
265274
return tuple((k, _to_hashable(v)) for k, v in x.items())
266-
return str(x) if not isinstance(x, t.Hashable) else x
275+
return str(x) if isinstance(x, DATETIME_TYPES) or not isinstance(x, t.Hashable) else x
267276

268277
actual = actual.apply(lambda col: col.map(_to_hashable))
269278
expected = expected.apply(lambda col: col.map(_to_hashable))

tests/core/test_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2609,3 +2609,34 @@ def test_model_test_text_result_reporting_no_traceback(
26092609
prefix = "ERROR" if is_error else "FAIL"
26102610
assert f"{prefix}: test_foo (None)" in output
26112611
assert "Exception: failure" in output
2612+
2613+
2614+
def test_timestamp_normalization() -> None:
2615+
model = _create_model(
2616+
"SELECT id, array_agg(timestamp_col::timestamp) as agg_timestamp_col FROM temp_model_with_timestamp GROUP BY id",
2617+
meta="MODEL (name foo, kind FULL)",
2618+
)
2619+
2620+
_check_successful_or_raise(
2621+
_create_test(
2622+
body=load_yaml(
2623+
"""
2624+
test_foo:
2625+
model: temp_agg_model_with_timestamp
2626+
inputs:
2627+
temp_model_with_timestamp:
2628+
rows:
2629+
- id: "id1"
2630+
timestamp_col: "2024-01-02T15:00:00"
2631+
outputs:
2632+
query:
2633+
rows:
2634+
- id: id1
2635+
agg_timestamp_col: ["2024-01-02T15:00:00.000000"]
2636+
"""
2637+
),
2638+
test_name="test_foo",
2639+
model=model,
2640+
context=Context(config=Config(model_defaults=ModelDefaultsConfig(dialect="duckdb"))),
2641+
).run()
2642+
)

0 commit comments

Comments
 (0)