"""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})" )