diff --git a/tests/conftest.py b/tests/conftest.py index 6701cc7..22b3d10 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -11,6 +11,7 @@ tests. import os import pytest +import psycopg2 from peewee import SqliteDatabase # Set DATABASE_TYPE=postgresql so that the module-level SKIP_TABLE_CREATION @@ -173,3 +174,39 @@ def track(): t3_threshold=448, t4_threshold=896, ) + + +# --------------------------------------------------------------------------- +# PostgreSQL integration fixture (used by test_evolution_*_api.py) +# --------------------------------------------------------------------------- + + +@pytest.fixture(scope="session") +def pg_conn(): + """Open a psycopg2 connection to the PostgreSQL instance for integration tests. + + Reads connection parameters from the standard POSTGRES_* env vars that the + CI workflow injects when a postgres service container is running. Skips the + entire session (via pytest.skip) when POSTGRES_HOST is not set, keeping + local runs clean. + + The connection is shared for the whole session (scope="session") because + the integration test modules use module-scoped fixtures that rely on it; + creating a new connection per test would break those module-scoped fixtures. + + Teardown: the connection is closed once all tests have finished. + """ + host = os.environ.get("POSTGRES_HOST") + if not host: + pytest.skip("POSTGRES_HOST not set — PostgreSQL integration tests skipped") + + conn = psycopg2.connect( + host=host, + port=int(os.environ.get("POSTGRES_PORT", "5432")), + dbname=os.environ.get("POSTGRES_DB", "paper_dynasty"), + user=os.environ.get("POSTGRES_USER", "postgres"), + password=os.environ.get("POSTGRES_PASSWORD", ""), + ) + conn.autocommit = False + yield conn + conn.close()