dodil/erp-brick-consolidation

DODIL ERP brick: consolidation

tests/contract_checks.py

"""Every call this brick makes to a neighbour is checked against the CALLEE's signed spec (erp-method). A missing checkout is an error,
never a skip. The fake (which replaces the whole port and never reaches a Seam) calls `spec_check` with the exact request the real
adapter would send; the real Seam calls are checked by `checked_seams` (conftest)."""
import os
import pathlib

from erp_platform import contract as _contract
from erp_platform.contract import Recorder

HERE = pathlib.Path(__file__).resolve().parent
METHOD_DIR = os.environ.get("ERP_METHOD_DIR") or str(HERE.parents[1] / "erp-method")
if not (pathlib.Path(METHOD_DIR) / "modules" / "shared.yaml").exists():
    raise RuntimeError(f"seam contract checks need an erp-method checkout: set ERP_METHOD_DIR (tried {METHOD_DIR})")

CONTRACT = Recorder()


def spec_check(callee, method, path, body=None):
    bad = _contract.check(callee, method, path, body, caller="consolidation", method_dir=METHOD_DIR)
    if bad:
        msg = f"consolidation -> {callee} {method} {path} breaks the callee's spec:\n  " + "\n  ".join(bad)
        CONTRACT.violations.append(msg)
        raise AssertionError(msg)