paper-dynasty-database/tests/conftest.py
Cal Corum 094f445fea fix: add pg_conn fixture for integration tests (#84)
Adds the missing `pg_conn` session-scoped pytest fixture to conftest.py.
Without it, the 6 integration tests in test_evolution_migration.py fail
immediately with `fixture 'pg_conn' not found` whenever POSTGRES_HOST is set.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-12 18:31:49 -05:00

49 lines
1.7 KiB
Python

"""Pytest configuration for the paper-dynasty-database test suite.
Sets DATABASE_TYPE=postgresql before any app module is imported so that
db_engine.py sets SKIP_TABLE_CREATION=True and does not try to mutate the
production SQLite file during test collection. Each test module is
responsible for binding models to its own in-memory database.
"""
import os
import psycopg2
import pytest
os.environ["DATABASE_TYPE"] = "postgresql"
# Provide dummy credentials so PooledPostgresqlDatabase can be instantiated
# without raising a configuration error (it will not actually be used).
os.environ.setdefault("POSTGRES_PASSWORD", "test-dummy")
@pytest.fixture(scope="session")
def pg_conn():
"""Live PostgreSQL connection for integration tests.
Requires POSTGRES_HOST to be set; runs the migration before yielding the
connection so that all integration tests start from a migrated schema.
The connection is rolled back and closed at session teardown so that the
migration is not persisted to the dev database between test runs.
"""
conn = psycopg2.connect(
host=os.environ["POSTGRES_HOST"],
dbname=os.environ.get("POSTGRES_DB", "pd_master"),
user=os.environ.get("POSTGRES_USER", "pd_admin"),
password=os.environ["POSTGRES_PASSWORD"],
port=int(os.environ.get("POSTGRES_PORT", "5432")),
)
conn.autocommit = False
migration_path = os.path.join(
os.path.dirname(__file__),
"..",
"migrations",
"2026-03-12_add_evolution_tables.sql",
)
with open(migration_path) as fh:
conn.cursor().execute(fh.read())
conn.commit()
yield conn
conn.rollback()
conn.close()