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>
85 lines
4.6 KiB
Python
85 lines
4.6 KiB
Python
"""Publish input and subscribe output.
|
|
|
|
Every row is on the table's compacted outbound topic, and one record published to an inbound
|
|
topic travels through every interface: ODS, outbound topic, lake, then a GraphQL update and a
|
|
GraphQL delete that follow it back out. The record is checked against the ODS constraints
|
|
before it is published, because a record the ODS refuses stops the product's Flink job.
|
|
"""
|
|
|
|
import pytest
|
|
|
|
from semprini_data_domain.platform import camel, pascal
|
|
from semprini_data_domain.testing import contains, expectations_for
|
|
|
|
EXPECT = expectations_for(__file__)
|
|
TABLES = list(EXPECT["tables"].items())
|
|
ROUND_TRIP = EXPECT.get("round_trip")
|
|
|
|
|
|
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_outbound_topic_carries_every_row(dp, table, spec):
|
|
key = key_of(dp, table)
|
|
ods_keys = {r[key] for r in dp.sql(f"SELECT {key} FROM {dp.product.schema}.{table}")}
|
|
topic = dp.topic(table, "subscribe-output")
|
|
dp.eventually(lambda: ods_keys <= {k for k, v in dp.latest(topic).items() if v is not None}, timeout=60)
|
|
|
|
|
|
@pytest.mark.skipif(not ROUND_TRIP, reason="no round_trip in expectations.yaml")
|
|
def test_record_round_trip(dp):
|
|
table, record, update = ROUND_TRIP["table"], ROUND_TRIP["record"], ROUND_TRIP["update"]
|
|
schema, key = dp.product.schema, key_of(dp, table)
|
|
record_key = record[key]
|
|
lake = f"{dp.product.trino_catalog}.{dp.product.lake_schema}.{table}"
|
|
outbound = dp.topic(table, "subscribe-output")
|
|
columns = ", ".join(record)
|
|
values = ", ".join(dp.sql_literal(v) for v in record.values())
|
|
where = f"{key} = '{record_key}'"
|
|
|
|
assert not dp.sql(f"SELECT 1 FROM {schema}.{table} WHERE {where}"), f"{record_key} already exists; clean up first"
|
|
refused = dp.sql_error(f"INSERT INTO {schema}.{table} ({columns}) VALUES ({values})")
|
|
assert refused is None, f"the ODS would refuse the round-trip record, so it is not published: {refused}"
|
|
|
|
inbound = next(t for t in dp.product.topics if t.table == table and t.role == "publish-input")
|
|
assert inbound.key, f"{inbound.name} declares no key in product.yaml; producers cannot keep changes in order"
|
|
|
|
try:
|
|
# Publish input -> Flink -> ODS, keyed as the contract requires
|
|
dp.publish(inbound.name, [record], key_field=inbound.key)
|
|
dp.eventually(lambda: contains(record, (dp.sql(f"SELECT * FROM {schema}.{table} WHERE {where}") or [None])[0]))
|
|
|
|
# ODS -> Flink CDC -> subscribe output, and ODS -> pg_duckpipe -> lake
|
|
dp.eventually(lambda: contains(record, dp.latest(outbound).get(record_key)))
|
|
dp.eventually(lambda: dp.trino(f"SELECT count(*) AS n FROM {lake} WHERE CAST({key} AS varchar) = '{record_key}'") == [{"n": 1}])
|
|
|
|
# Two more, published in order under the same key: one partition, so the last one wins
|
|
column, value = next(iter(update.items()))
|
|
ordered = [dict(record, **{column: f"{value} first"}), dict(record, **{column: f"{value} second"})]
|
|
dp.publish(inbound.name, ordered, key_field=inbound.key)
|
|
dp.eventually(lambda: (dp.sql(f"SELECT {column} FROM {schema}.{table} WHERE {where}") or [{}])[0].get(column)
|
|
== f"{value} second")
|
|
|
|
# CRUD API update -> subscribe output and lake
|
|
assignments = ", ".join(f'{camel(c)}: "{v}"' for c, v in update.items())
|
|
data = dp.graphql(
|
|
f"mutation {{ update{pascal(table)}Collection(set: {{{assignments}}}, "
|
|
f'filter: {{{camel(key)}: {{eq: "{record_key}"}}}}) {{ affectedCount }} }}'
|
|
)
|
|
assert data[f"update{pascal(table)}Collection"]["affectedCount"] == 1
|
|
dp.eventually(lambda: contains(update, dp.latest(outbound).get(record_key)))
|
|
dp.eventually(lambda: dp.trino(f"SELECT CAST({column} AS varchar) AS v FROM {lake} WHERE CAST({key} AS varchar) = '{record_key}'") == [{"v": str(value)}])
|
|
|
|
# CRUD API delete -> tombstone on subscribe output, row gone from the lake
|
|
data = dp.graphql(
|
|
f"mutation {{ deleteFrom{pascal(table)}Collection("
|
|
f'filter: {{{camel(key)}: {{eq: "{record_key}"}}}}, atMost: 1) {{ affectedCount }} }}'
|
|
)
|
|
assert data[f"deleteFrom{pascal(table)}Collection"]["affectedCount"] == 1
|
|
dp.eventually(lambda: (lambda state: record_key in state and state[record_key] is None)(dp.latest(outbound)))
|
|
dp.eventually(lambda: dp.trino(f"SELECT count(*) AS n FROM {lake} WHERE CAST({key} AS varchar) = '{record_key}'") == [{"n": 0}])
|
|
finally:
|
|
dp.execute(f"DELETE FROM {schema}.{table} WHERE {where}")
|