51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
|
|
"""Integration-level fixtures: testcontainers Postgres + Flask app + db session.
|
||
|
|
|
||
|
|
NF-state: SQLite is reserved for pure-logic unit tests; anything touching
|
||
|
|
constraints, RBAC role grants, or audit-log SQL behavior must run on a real
|
||
|
|
Postgres via testcontainers (spec H38).
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from collections.abc import Iterator
|
||
|
|
|
||
|
|
import pytest
|
||
|
|
|
||
|
|
try:
|
||
|
|
from testcontainers.postgres import PostgresContainer
|
||
|
|
except ImportError: # pragma: no cover
|
||
|
|
PostgresContainer = None # type: ignore[assignment]
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture(scope="session")
|
||
|
|
def postgres_dsn() -> Iterator[str]:
|
||
|
|
if PostgresContainer is None:
|
||
|
|
pytest.skip("testcontainers not installed")
|
||
|
|
with PostgresContainer("postgres:16-alpine") as pg:
|
||
|
|
url = pg.get_connection_url().replace(
|
||
|
|
"postgresql+psycopg2", "postgresql+psycopg"
|
||
|
|
)
|
||
|
|
yield url
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def app(postgres_dsn: str, monkeypatch: pytest.MonkeyPatch):
|
||
|
|
monkeypatch.setenv("MIMIC_DATABASE_URL", postgres_dsn)
|
||
|
|
monkeypatch.setenv("MIMIC_ENV", "testing")
|
||
|
|
monkeypatch.setenv("MIMIC_SECRET_KEY", "test-not-real")
|
||
|
|
|
||
|
|
from mimic.app import create_app # noqa: PLC0415 (must follow env mutation)
|
||
|
|
from mimic.extensions import db # noqa: PLC0415
|
||
|
|
|
||
|
|
application = create_app()
|
||
|
|
with application.app_context():
|
||
|
|
db.create_all()
|
||
|
|
yield application
|
||
|
|
db.session.remove()
|
||
|
|
db.drop_all()
|
||
|
|
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def client(app):
|
||
|
|
return app.test_client()
|