fix: restore original conftest.py from last working state
Revert to the exact per-test create/drop conftest that worked at
25cff5e4. The session-scoped and module-level patching approaches
both broke e2e tests in CI.
This commit is contained in:
parent
554da599ba
commit
877861c9e8
1 changed files with 14 additions and 35 deletions
|
|
@ -1,17 +1,11 @@
|
|||
"""Shared test fixtures — async DB session using a test database.
|
||||
|
||||
Unit tests use the ``session`` fixture, which provides a per-test
|
||||
savepoint-isolated session on a dedicated test engine. E2E tests do NOT
|
||||
use this fixture and are therefore unaffected — they keep using the real
|
||||
``wiregui.db.async_session`` that talks to the app's database.
|
||||
"""
|
||||
"""Shared test fixtures — async DB session using a test database."""
|
||||
|
||||
import os
|
||||
from collections.abc import AsyncGenerator
|
||||
|
||||
import pytest_asyncio
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
from sqlmodel import SQLModel
|
||||
|
||||
from wiregui.config import get_settings
|
||||
|
|
@ -57,34 +51,19 @@ def _ensure_test_db_sync():
|
|||
|
||||
_ensure_test_db_sync()
|
||||
|
||||
# Test engine — only used by the ``session`` fixture and unit tests.
|
||||
# NOT assigned to wiregui.db so e2e tests are unaffected.
|
||||
_test_engine = create_async_engine(TEST_DATABASE_URL)
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="session")
|
||||
async def _test_tables():
|
||||
"""Create all tables once per test session, drop at end."""
|
||||
async with _test_engine.begin() as conn:
|
||||
await conn.run_sync(SQLModel.metadata.create_all)
|
||||
yield
|
||||
async with _test_engine.begin() as conn:
|
||||
await conn.run_sync(SQLModel.metadata.drop_all)
|
||||
await _test_engine.dispose()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture
|
||||
async def session(_test_tables) -> AsyncGenerator[AsyncSession]:
|
||||
"""Per-test session with transaction isolation.
|
||||
async def session() -> AsyncGenerator[AsyncSession]:
|
||||
"""Fresh engine + session per test, with table setup/teardown."""
|
||||
engine = create_async_engine(TEST_DATABASE_URL)
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(SQLModel.metadata.create_all)
|
||||
|
||||
The session is bound to a connection-level transaction that is always
|
||||
rolled back at teardown. When tested code calls ``session.commit()``,
|
||||
SQLAlchemy only releases a SAVEPOINT — the outer transaction is never
|
||||
committed, so no test data persists between tests.
|
||||
"""
|
||||
async with _test_engine.connect() as conn:
|
||||
txn = await conn.begin()
|
||||
sess = AsyncSession(bind=conn, expire_on_commit=False, join_transaction_mode="create_savepoint")
|
||||
factory = async_sessionmaker(engine, expire_on_commit=False)
|
||||
async with factory() as sess:
|
||||
yield sess
|
||||
await sess.close()
|
||||
await txn.rollback()
|
||||
await sess.rollback()
|
||||
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(SQLModel.metadata.drop_all)
|
||||
await engine.dispose()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue