Skip to content

Commit 118846c

Browse files
authored
Normalize array before timestamps during test creation (#2351)
1 parent de6bfc4 commit 118846c

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

sqlmesh/core/test/definition.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,9 @@ def generate_test(
432432
inputs = {
433433
models[dep]
434434
.name: pandas_timestamp_to_pydatetime(
435-
engine_adapter.fetchdf(query), models[dep].columns_to_types
435+
engine_adapter.fetchdf(query).apply(lambda col: col.map(_normalize_dataframe)),
436+
models[dep].columns_to_types,
436437
)
437-
.apply(lambda col: col.map(_normalize_dataframe))
438438
.to_dict(orient="records")
439439
for dep, query in input_queries.items()
440440
}
@@ -471,11 +471,9 @@ def generate_test(
471471
else:
472472
output = t.cast(PythonModelTest, test)._execute_model()
473473

474-
outputs["query"] = (
475-
pandas_timestamp_to_pydatetime(output, model.columns_to_types)
476-
.apply(lambda col: col.map(_normalize_dataframe))
477-
.to_dict(orient="records")
478-
)
474+
outputs["query"] = pandas_timestamp_to_pydatetime(
475+
output.apply(lambda col: col.map(_normalize_dataframe)), model.columns_to_types
476+
).to_dict(orient="records")
479477

480478
test.tearDown()
481479

tests/core/test_test.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,3 +832,41 @@ def test_test_generation_with_array(tmp_path: Path) -> None:
832832
"sqlmesh_example.bar": [{"array_col": ["value1", "value2"]}]
833833
}
834834
assert test["test_foo"]["outputs"] == {"query": [{"array_col": ["value1", "value2"]}]}
835+
836+
837+
def test_test_generation_with_timestamp(tmp_path: Path) -> None:
838+
init_example_project(tmp_path, dialect="duckdb")
839+
840+
config = Config(
841+
default_connection=DuckDBConnectionConfig(),
842+
model_defaults=ModelDefaultsConfig(dialect="duckdb"),
843+
)
844+
foo_sql_file = tmp_path / "models" / "foo.sql"
845+
foo_sql_file.write_text(
846+
"MODEL (name sqlmesh_example.foo); SELECT ts_col FROM sqlmesh_example.bar;"
847+
)
848+
bar_sql_file = tmp_path / "models" / "bar.sql"
849+
bar_sql_file.write_text("MODEL (name sqlmesh_example.bar); SELECT ts_col FROM external_table;")
850+
851+
context = Context(paths=tmp_path, config=config)
852+
853+
input_queries = {
854+
"sqlmesh_example.bar": "SELECT TIMESTAMP '2024-09-20 11:30:00.123456789' AS ts_col"
855+
}
856+
857+
context.create_test(
858+
"sqlmesh_example.foo",
859+
input_queries=input_queries,
860+
overwrite=True,
861+
)
862+
863+
test = load_yaml(context.path / c.TESTS / "test_foo.yaml")
864+
865+
assert len(test) == 1
866+
assert "test_foo" in test
867+
assert test["test_foo"]["inputs"] == {
868+
"sqlmesh_example.bar": [{"ts_col": datetime.datetime(2024, 9, 20, 11, 30, 0, 123456)}]
869+
}
870+
assert test["test_foo"]["outputs"] == {
871+
"query": [{"ts_col": datetime.datetime(2024, 9, 20, 11, 30, 0, 123456)}]
872+
}

0 commit comments

Comments
 (0)