import pytest from fastapi.testclient import TestClient from sqlalchemy import text from alembic.config import Config from alembic.runtime.migration import MigrationContext from alembic.script import ScriptDirectory from app.main import app from app.persistence.db import engine pytestmark = pytest.mark.integration client = TestClient(app) @pytest.fixture(scope="module", autouse=True) def require_postgres() -> None: try: with engine.connect() as connection: connection.execute(text("SELECT 1")) except Exception as exc: pytest.skip(f"PostgreSQL not available: {exc}") def test_health_ready_when_migrated() -> None: response = client.get("/health/ready") assert response.status_code == 200 body = response.json() assert body["status"] == "ready" assert "environment" in body assert "engine_build" in body def test_alembic_is_at_head() -> None: config = Config("alembic.ini") script = ScriptDirectory.from_config(config) with engine.connect() as connection: context = MigrationContext.configure(connection) assert set(context.get_current_heads()) == set(script.get_heads())