dodil/erp-brick-consolidation
DODIL ERP brick: consolidation
tests/test_schema.py
"""schema.sql and the tables in infrastructure/tables.py are one column model: they must not drift."""
import pathlib
import re
from brick_consolidation.infrastructure.tables import md
SRC = pathlib.Path(__file__).resolve().parents[1] / "src" / "brick_consolidation"
def expected_columns(sql: str) -> dict:
"""table -> [column], as a schema file declares it (erp_platform.migrate's reading, without a BUCKET)."""
out = {}
for name, body in re.findall(r"CREATE TABLE IF NOT EXISTS (\w+) \(\n(.*?)\n\);", sql, re.S):
lines = [ln.strip() for ln in body.splitlines()]
out[name] = [ln.split()[0] for ln in lines if ln and not ln.startswith(("PRIMARY KEY", "--"))]
return out
def test_schema_sql_is_the_tables():
declared = expected_columns((SRC / "schema.sql").read_text())
assert declared == {t.name: [c.name for c in t.columns] for t in md.sorted_tables}
def test_the_first_migration_creates_the_same_tables():
up = expected_columns((SRC / "migrations" / "00001_init" / "up.sql").read_text())
assert {t.name for t in md.sorted_tables} - {"cons_setting_row"} <= set(up) # 00001 is not edited: slice C's table is in 00002
def test_slice_c_migration_adds_what_schema_sql_has_beyond_the_first():
"""00002_slice_c adds every column and table schema.sql has that 00001_init does not."""
first = expected_columns((SRC / "migrations" / "00001_init" / "up.sql").read_text())
now = expected_columns((SRC / "schema.sql").read_text())
text = (SRC / "migrations" / "00002_slice_c" / "up.sql").read_text() + (SRC / "migrations" / "00003_audit_fixes" / "up.sql").read_text() \
+ (SRC / "migrations" / "00004_v3" / "up.sql").read_text() # + the audit fixes, + v3
added = expected_columns(text)
for table, columns in now.items():
if table not in first:
assert added[table] == columns
for column in columns[len(first.get(table, columns)):]:
assert f"ALTER TABLE {table} ADD COLUMN IF NOT EXISTS {column} " in text, (table, column)