dodil/erp-brick-contracts

tests/test_schema.py

"""schema.sql is the tables, generated (scripts/gen_schema.py): a drift fails here, before a bucket finds it. And the
rules every brick's schema keeps (BRICKS.md, Data): every table is ct_ (the provider's capability tables are named by
the platform after the brick: contract_), has a primary key, no SERIAL; the brick composes alone and with its
neighbours with no collision."""
import importlib.util
import pathlib
import re

from erp_platform import compose

ROOT = pathlib.Path(__file__).resolve().parents[1]


def _gen():
    spec = importlib.util.spec_from_file_location("gen_schema", ROOT / "scripts" / "gen_schema.py")
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod.render()


def test_schema_sql_is_generated_from_the_tables():
    assert (ROOT / "src" / "brick_contracts" / "schema.sql").read_text() == _gen(), "run scripts/gen_schema.py"


def test_every_table_is_prefixed_keyed_and_never_serial():
    sql = (ROOT / "src" / "brick_contracts" / "schema.sql").read_text()
    tables = re.findall(r"CREATE TABLE IF NOT EXISTS (\w+) \(([\s\S]*?)\n\);", sql)
    assert len(tables) >= 30
    for name, body in tables:
        assert name.startswith(("ct_", "contracts_capability_")), name
        assert "PRIMARY KEY (" in body, name
        assert "SERIAL" not in body.upper(), name
    assert "ct_contract_versions" in sql and "ct_signature_event" in sql


def test_contracts_composes_with_business_partner_and_provides_its_capabilities():
    report = compose.check(compose.load(ROOT / "tests" / "scenarios" / "compose.yaml"))
    assert report["problems"] == [], report["problems"]
    ct = next(b for b in report["bricks"] if b["name"] == "contracts")
    assert ct["tools"] >= 50
    assert any(s["from"] == "contracts" and s["to"] == "business-partner" and s["present"] for s in report["seams"])
    wired = {c["capability"]: c["provider"] for c in report["capabilities"]}
    assert wired["contracts.draft"] == "contracts" and wired["mail.send"] == "manual"
    assert report["public"], "the signing pages are served by the public app"