Files
semprini-data-product-templ…/canonical_data_product/example/tests/test_lake.py
T
2026-09-18 08:56:37 +12:00

34 lines
1.6 KiB
Python

"""Analytic SQL: pg_duckpipe has replicated every seed row into the lake, and Trino serves it."""
import pytest
from semprini_data_domain.testing import expectations_for, same_value
TABLES = list(expectations_for(__file__)["tables"].items())
SAMPLES = [(table, sample) for table, spec in TABLES for sample in spec.get("samples", [])]
def key_of(dp, table):
return next(t.key for t in dp.product.tables if t.name == table)
@pytest.mark.parametrize("table,spec", TABLES, ids=[t for t, _ in TABLES])
def test_lake_matches_ods(dp, table, spec):
lake = f"{dp.product.trino_catalog}.{dp.product.lake_schema}.{table}"
ods = dp.sql(f"SELECT count(*) AS n FROM {dp.product.schema}.{table}")[0]["n"]
assert ods == spec["rows"]
dp.eventually(lambda: dp.trino(f"SELECT count(*) AS n FROM {lake}") == [{"n": ods}])
@pytest.mark.parametrize("table,sample", SAMPLES, ids=[t for t, _ in SAMPLES])
def test_seed_row_in_lake(dp, table, sample):
key = key_of(dp, table)
lake = f"{dp.product.trino_catalog}.{dp.product.lake_schema}.{table}"
# Every column is cast to varchar so dates and uuids compare as text, the way the seed writes them.
columns = ", ".join(f"CAST({column} AS varchar) AS {column}" for column in sample)
rows = dp.trino(f"SELECT {columns} FROM {lake} WHERE CAST({key} AS varchar) = '{sample[key]}'")
assert len(rows) == 1, f"{sample[key]} not in {lake}"
for column, expected in sample.items():
want = None if expected is None else (str(expected).lower() if isinstance(expected, bool) else expected)
assert same_value(want, rows[0][column]), f"{column}: {rows[0][column]!r} != {expected!r}"