dodil/erp-brick-hr

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 hr_, has a primary key, no SERIAL; the brick
composes with business-partner 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_hr" / "schema.sql").read_text() == _gen(), "run scripts/gen_schema.py"


def test_every_table_is_prefixed_keyed_and_never_serial():
    sql = (ROOT / "src" / "brick_hr" / "schema.sql").read_text()
    tables = re.findall(r"CREATE TABLE IF NOT EXISTS (\w+) \(([\s\S]*?)\n\);", sql)
    assert len(tables) > 40
    for name, body in tables:
        assert name.startswith("hr_"), name
        assert "PRIMARY KEY (" in body, name
        assert "SERIAL" not in body.upper(), name
    assert "NUMERIC(19,4)" in sql and "NUMERIC(18,2)" in sql            # rates 4 places, amounts 2


def test_hr_composes_with_business_partner_without_collisions():
    report = compose.check(compose.load(ROOT / "tests" / "tier1.compose.yaml"))
    assert report["problems"] == [], report["problems"]
    hr = next(b for b in report["bricks"] if b["name"] == "hr")
    assert hr["tools"] >= 50
    assert any(s["from"] == "hr" and s["to"] == "business-partner" and s["present"] for s in report["seams"])
    assert {c["capability"] for c in report["capabilities"]} >= {"identity.provision_user", "contracts.draft",
                                                                   "identity.invite_app_user"}
    assert all(c["provider"] == "manual" for c in report["capabilities"])