from __future__ import annotations import shutil from pathlib import Path import pytest from alembic import command from alembic.config import Config from fastapi.testclient import TestClient from sqlalchemy.orm import Session from canned_prompts_service.api import create_app from canned_prompts_service.db import make_engine, make_session_factory from canned_prompts_service.settings import Settings from canned_prompts_service import store ROOT = Path(__file__).resolve().parents[1] REPO = ROOT.parent @pytest.fixture() def db_url(tmp_path: Path) -> str: url = f"sqlite:///{tmp_path / 'svc.db'}" config = Config(str(ROOT / "alembic.ini")) config.set_main_option("script_location", str(ROOT / "migrations")) config.set_main_option("sqlalchemy.url", url) command.upgrade(config, "head") return url @pytest.fixture() def session(db_url: str) -> Session: with make_session_factory(make_engine(db_url))() as s: yield s @pytest.fixture() def client(db_url: str) -> TestClient: return TestClient(create_app(Settings(database_url=db_url), make_engine(db_url))) @pytest.fixture() def seeded(db_url: str) -> TestClient: """A store holding the repo's own example packages.""" engine = make_engine(db_url) with make_session_factory(engine)() as s: for name in ("house-style", "pqrst-estimate"): store.ingest(s, "default", "local", REPO / "examples" / name, source=f"examples/{name}", method="publish") s.commit() return TestClient(create_app(Settings(database_url=db_url), engine)) @pytest.fixture() def two_registries(db_url: str, tmp_path: Path) -> TestClient: """The same id in two registries — § 3.2 ambiguity.""" engine = make_engine(db_url) with make_session_factory(engine)() as s: for registry in ("local", "house"): store.ingest(s, "default", registry, REPO / "examples" / "house-style", source=registry, method="publish") s.commit() return TestClient(create_app(Settings(database_url=db_url), engine))