Skip to content

Commit ab3d240

Browse files
fix(e2e): improve search_path handling for connection pooling
- Set search_path at engine level via connect_args (handles pooling) - Use temp engine for schema creation before setting search_path - Add note about pytest-xdist incompatibility - Remove redundant SET search_path from session fixture Addresses Copilot review feedback.
1 parent 5986408 commit ab3d240

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

tests/e2e/conftest.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
44
Uses a separate 'test_e2e' schema to isolate test data from production.
55
Tests are skipped if DATABASE_URL is not set.
6+
7+
Note: These tests must NOT be run with pytest-xdist parallelization
8+
as multiple workers would conflict on the shared test_e2e schema.
69
"""
710

811
import os
@@ -42,13 +45,20 @@ async def pg_engine():
4245
if not database_url:
4346
pytest.skip("DATABASE_URL not set - skipping PostgreSQL E2E tests")
4447

45-
engine = create_async_engine(database_url, echo=False)
46-
47-
# Create test schema and tables
48-
async with engine.begin() as conn:
48+
# First create schema with a temporary engine (no search_path yet)
49+
temp_engine = create_async_engine(database_url, echo=False)
50+
async with temp_engine.begin() as conn:
4951
await conn.execute(text(f"DROP SCHEMA IF EXISTS {TEST_SCHEMA} CASCADE"))
5052
await conn.execute(text(f"CREATE SCHEMA {TEST_SCHEMA}"))
51-
await conn.execute(text(f"SET search_path TO {TEST_SCHEMA}"))
53+
await temp_engine.dispose()
54+
55+
# Create engine with search_path set at connection level (handles pooling correctly)
56+
engine = create_async_engine(
57+
database_url, echo=False, connect_args={"server_settings": {"search_path": TEST_SCHEMA}}
58+
)
59+
60+
# Create tables in test schema
61+
async with engine.begin() as conn:
5262
await conn.run_sync(Base.metadata.create_all)
5363

5464
yield engine
@@ -61,11 +71,9 @@ async def pg_engine():
6171

6272
@pytest_asyncio.fixture
6373
async def pg_session(pg_engine):
64-
"""Create session with test schema."""
74+
"""Create session with test schema (search_path set at engine level)."""
6575
async_session = async_sessionmaker(pg_engine, class_=AsyncSession, expire_on_commit=False)
6676
async with async_session() as session:
67-
# Ensure we're in test schema
68-
await session.execute(text(f"SET search_path TO {TEST_SCHEMA}"))
6977
yield session
7078
await session.rollback()
7179

0 commit comments

Comments
 (0)