"""End-to-end migration test against a temporary SQLite DB.""" from __future__ import annotations import os from pathlib import Path import pytest from alembic import command from alembic.config import Config from sqlalchemy import create_engine, inspect, select from artifactstore.db.schema import retention_classes from artifactstore.db.seed import RETENTION_CLASS_SEEDS REPO_ROOT = Path(__file__).resolve().parents[2] @pytest.fixture def temp_db_url(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> str: db_path = tmp_path / "artifactstore-test.db" url = f"sqlite+aiosqlite:///{db_path}" monkeypatch.setenv("ARTIFACTSTORE_DATABASE_URL", url) return url def test_alembic_upgrade_head_creates_all_tables_and_seeds(temp_db_url: str) -> None: cfg = Config(str(REPO_ROOT / "alembic.ini")) cfg.set_main_option("script_location", str(REPO_ROOT / "migrations")) cwd = os.getcwd() os.chdir(REPO_ROOT) try: command.upgrade(cfg, "head") finally: os.chdir(cwd) sync_url = temp_db_url.replace("+aiosqlite", "") engine = create_engine(sync_url, future=True) try: names = set(inspect(engine).get_table_names()) assert { "events", "retention_classes", "metadata_schemas", "artifact_packages", "artifact_files", "storage_locations", "retention_state", "alembic_version", }.issubset(names) with engine.connect() as conn: rows = conn.execute(select(retention_classes)).all() seed_ids = {s["class_id"] for s in RETENTION_CLASS_SEEDS} assert {r.class_id for r in rows} == seed_ids finally: engine.dispose()