|
| 1 | +"""Tests for Alembic migration infrastructure. |
| 2 | +
|
| 3 | +Runs migrations against an in-memory SQLite database to verify: |
| 4 | +- upgrade to head works |
| 5 | +- downgrade to base works |
| 6 | +- round-trip (up → down → up) works |
| 7 | +- schema matches expected columns and foreign keys |
| 8 | +""" |
| 9 | + |
| 10 | +import pytest |
| 11 | +from alembic import command |
| 12 | +from alembic.config import Config |
| 13 | +from sqlalchemy import create_engine, inspect |
| 14 | + |
| 15 | +ALEMBIC_INI = "alembic.ini" |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture() |
| 19 | +def alembic_engine(): |
| 20 | + return create_engine("sqlite://") |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture() |
| 24 | +def alembic_cfg(alembic_engine): |
| 25 | + cfg = Config(ALEMBIC_INI) |
| 26 | + cfg.set_main_option("sqlalchemy.url", "sqlite://") |
| 27 | + cfg.attributes["connection"] = alembic_engine.connect() |
| 28 | + return cfg |
| 29 | + |
| 30 | + |
| 31 | +def test_upgrade_head(alembic_cfg, alembic_engine): |
| 32 | + command.upgrade(alembic_cfg, "head") |
| 33 | + |
| 34 | + inspector = inspect(alembic_engine) |
| 35 | + tables = inspector.get_table_names() |
| 36 | + assert "template" in tables |
| 37 | + assert "formsubmission" in tables |
| 38 | + assert "alembic_version" in tables |
| 39 | + |
| 40 | + |
| 41 | +def test_downgrade_base(alembic_cfg, alembic_engine): |
| 42 | + command.upgrade(alembic_cfg, "head") |
| 43 | + command.downgrade(alembic_cfg, "base") |
| 44 | + |
| 45 | + inspector = inspect(alembic_engine) |
| 46 | + tables = inspector.get_table_names() |
| 47 | + assert "template" not in tables |
| 48 | + assert "formsubmission" not in tables |
| 49 | + |
| 50 | + |
| 51 | +def test_round_trip(alembic_cfg, alembic_engine): |
| 52 | + command.upgrade(alembic_cfg, "head") |
| 53 | + command.downgrade(alembic_cfg, "-1") |
| 54 | + command.upgrade(alembic_cfg, "head") |
| 55 | + |
| 56 | + inspector = inspect(alembic_engine) |
| 57 | + tables = inspector.get_table_names() |
| 58 | + assert "template" in tables |
| 59 | + assert "formsubmission" in tables |
| 60 | + |
| 61 | + |
| 62 | +def test_template_columns(alembic_cfg, alembic_engine): |
| 63 | + command.upgrade(alembic_cfg, "head") |
| 64 | + |
| 65 | + inspector = inspect(alembic_engine) |
| 66 | + columns = {c["name"] for c in inspector.get_columns("template")} |
| 67 | + assert columns == {"id", "name", "fields", "pdf_path", "created_at"} |
| 68 | + |
| 69 | + |
| 70 | +def test_formsubmission_columns(alembic_cfg, alembic_engine): |
| 71 | + command.upgrade(alembic_cfg, "head") |
| 72 | + |
| 73 | + inspector = inspect(alembic_engine) |
| 74 | + columns = {c["name"] for c in inspector.get_columns("formsubmission")} |
| 75 | + assert columns == {"id", "template_id", "input_text", "output_pdf_path", "created_at"} |
| 76 | + |
| 77 | + |
| 78 | +def test_formsubmission_fk(alembic_cfg, alembic_engine): |
| 79 | + command.upgrade(alembic_cfg, "head") |
| 80 | + |
| 81 | + inspector = inspect(alembic_engine) |
| 82 | + fks = inspector.get_foreign_keys("formsubmission") |
| 83 | + assert len(fks) == 1 |
| 84 | + assert fks[0]["referred_table"] == "template" |
| 85 | + assert fks[0]["referred_columns"] == ["id"] |
0 commit comments