32 lines
1.3 KiB
Python
32 lines
1.3 KiB
Python
"""Seed data: every ODS table holds the rows ods/040_seed.sql inserts, with the values it inserts.
|
|
|
|
Generic canonical-product test, copied unchanged into every product. What to expect comes
|
|
from expectations.yaml beside it. Run from the domain repository: data-domain unit <product>.
|
|
"""
|
|
|
|
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_seed_row_count(dp, table, spec):
|
|
rows = dp.sql(f"SELECT count(*) AS n FROM {dp.product.schema}.{table}")
|
|
assert rows[0]["n"] == spec["rows"]
|
|
|
|
|
|
@pytest.mark.parametrize("table,sample", SAMPLES, ids=[t for t, _ in SAMPLES])
|
|
def test_seed_row_values(dp, table, sample):
|
|
key = key_of(dp, table)
|
|
rows = dp.sql(f"SELECT * FROM {dp.product.schema}.{table} WHERE {key} = '{sample[key]}'")
|
|
assert len(rows) == 1, f"seed row {sample[key]} not in {table}"
|
|
for column, expected in sample.items():
|
|
assert same_value(expected, rows[0][column]), f"{table}.{column}: {rows[0][column]!r} != {expected!r}"
|