homelab-codex-ws/packages/kb-mail/tests/test_migration.py

90 lines
3.1 KiB
Python
Raw Normal View History

"""Sanity tests for SQL migration files — no DB or network required."""
from __future__ import annotations
from pathlib import Path
REPO_ROOT = Path(__file__).parent.parent.parent.parent
INIT_DIR = REPO_ROOT / "services" / "kb-postgres" / "init"
def test_migration_001_exists():
assert (INIT_DIR / "001_envelope.sql").exists(), \
f"Expected {INIT_DIR / '001_envelope.sql'} to exist"
def test_migration_001_enables_vector_extension():
sql = (INIT_DIR / "001_envelope.sql").read_text()
assert "CREATE EXTENSION" in sql
assert "vector" in sql.lower()
def test_migration_001_creates_envelope_table():
sql = (INIT_DIR / "001_envelope.sql").read_text()
assert "CREATE TABLE" in sql
assert "envelope" in sql.lower()
def test_migration_001_has_all_frozen_columns():
sql = (INIT_DIR / "001_envelope.sql").read_text()
frozen_columns = ("id", "source", "ts", "geo", "raw_ref", "entities")
for col in frozen_columns:
assert col in sql, f"Frozen column '{col}' missing from 001_envelope.sql"
def test_migration_001_geo_is_nullable():
sql = (INIT_DIR / "001_envelope.sql").read_text()
# geo must NOT have NOT NULL constraint
lines = [l for l in sql.splitlines() if "geo" in l.lower()]
assert lines, "No 'geo' line found in migration SQL"
for line in lines:
assert "NOT NULL" not in line.upper(), \
f"geo must be nullable but found: {line.strip()}"
def test_migration_001_entities_has_default():
sql = (INIT_DIR / "001_envelope.sql").read_text()
lines = [l for l in sql.splitlines() if "entities" in l.lower()]
assert any("DEFAULT" in l.upper() for l in lines), \
"entities column must have a DEFAULT '[]'"
2026-07-13 21:08:41 +02:00
def test_migration_002_exists():
assert (INIT_DIR / "002_chunks.sql").exists(), \
f"Expected {INIT_DIR / '002_chunks.sql'} to exist"
def test_migration_002_does_not_touch_envelope():
sql = (INIT_DIR / "002_chunks.sql").read_text()
assert "ALTER TABLE envelope" not in sql
assert "DROP TABLE envelope" not in sql.upper()
def test_migration_002_creates_document_chunk_table():
sql = (INIT_DIR / "002_chunks.sql").read_text()
assert "CREATE TABLE IF NOT EXISTS document_chunk" in sql
def test_migration_002_references_envelope_with_cascade():
sql = (INIT_DIR / "002_chunks.sql").read_text()
assert "REFERENCES envelope(id) ON DELETE CASCADE" in sql
def test_migration_002_embedding_dimension_matches_bge_m3():
sql = (INIT_DIR / "002_chunks.sql").read_text()
# bge-m3 dense embedding dimension is 1024 (docs/kb/modules/05-faza2-plan.md §1.4)
assert "VECTOR(1024)" in sql.upper()
def test_migration_002_has_unique_envelope_chunk_index():
sql = (INIT_DIR / "002_chunks.sql").read_text()
assert "UNIQUE (envelope_id, chunk_index)" in sql
def test_migration_002_uses_idempotent_ddl():
sql = (INIT_DIR / "002_chunks.sql").read_text()
assert "CREATE TABLE IF NOT EXISTS" in sql
for line in sql.splitlines():
if line.strip().upper().startswith("CREATE INDEX"):
assert "IF NOT EXISTS" in line.upper(), \
f"CREATE INDEX must be idempotent: {line.strip()}"