Files
semprini-data-product-templ…/source_aligned_cdc_data_product/tests/conftest.py
T
paulandClaude Opus 5 866bcfe238 Add the source-aligned CDC template, and key the canonical template's topics
The first template of the source-aligned family: one data product per source
system, capturing a database by CDC and turning what it emits into what a
canonical product accepts. It runs on the Integration Bus, not in a domain, and
is deployed with the `data-bus` CLI.

What it produces, and what its tests hold it to: raw topics in the source's own
shape; deterministic identity, so a re-snapshot upserts rather than duplicating;
validation against the canonical product's rules before publishing, with what
fails going to the product's own dead-letter topic with the reason; keying by the
canonical identifier; provenance in headers; and an archive in the bus object
store that can be replayed back onto the raw topic.

The worked example captures an ERP customer table and delivers into Canonical
Party in the digital-core domain: 12 smoke checks, 37 unit tests, including a
poison record that is quarantined while the next record still arrives.

The canonical template's topics now declare the key producers must set, which is
the other half of that contract.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 10:54:39 +12:00

44 lines
1.7 KiB
Python

"""Leave the target domain as we found it.
A source-aligned product's tests deliver real records into someone else's domain, and that
domain's canonical product has its own seed and its own tests that count rows. So when this
suite finishes, every party it created is removed again. Generic: the keys come from
expectations.yaml, through the platform's identity rule.
"""
from __future__ import annotations
from pathlib import Path
import pytest
from semprini_data_bus.bus import Bus
from semprini_data_bus.manifest import load
from semprini_data_bus.testing import (
SourceProductClient,
deterministic_id,
expectations_for,
find_target_domain,
)
EXPECT = expectations_for(__file__)
@pytest.fixture(scope="session", autouse=True)
def leave_the_target_domain_as_we_found_it():
yield
source, transform, delivery = EXPECT["source"], EXPECT["transform"], EXPECT["delivery"]
product = load(Path(__file__).resolve().parent.parent)
bus = Bus.for_path(product.dir)
client = SourceProductClient(bus, product, find_target_domain(bus, product.targets[0], product.dir))
if client.target_platform is None:
return
keys = [c["source_key"] for c in transform["accepted"]]
keys += [c["source_key"] for c in transform.get("rejected", [])]
keys += [p["source_key"] for p in (transform.get("probe_records") or {}).values()]
keys += [EXPECT["capture"]["probe"]["source_key"]] if "probe" in EXPECT["capture"] else []
ids = ", ".join(f"'{deterministic_id(source['system'], source['object'], k)}'" for k in keys)
client.target_execute(
f"DELETE FROM {delivery['schema']}.{delivery['table']} WHERE {delivery['key']} IN ({ids})"
)