CI: drop outer transaction/savepoint, brug per-test cleanup i stedet
This commit is contained in:
+17
-23
@@ -36,42 +36,36 @@ async def engine():
|
|||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
async def session(engine):
|
async def session(engine):
|
||||||
"""Per-test database session med transaction rollback.
|
"""Per-test database session.
|
||||||
|
|
||||||
Hver test får sin egen connection med en ydre transaction.
|
Data commited her er synlig for appens egen session via HTTP client.
|
||||||
commit() inde i testen opretter en savepoint (via create_savepoint),
|
Renses automatisk af _clean_db efter hver test.
|
||||||
så al data rulles tilbage efter testen — ingen krydskontaminering.
|
|
||||||
"""
|
"""
|
||||||
conn = await engine.connect()
|
maker = async_sessionmaker(bind=engine, expire_on_commit=False)
|
||||||
transaction = await conn.begin()
|
|
||||||
maker = async_sessionmaker(
|
|
||||||
bind=conn,
|
|
||||||
expire_on_commit=False,
|
|
||||||
join_transaction_mode="create_savepoint",
|
|
||||||
)
|
|
||||||
async with maker() as s:
|
async with maker() as s:
|
||||||
yield s
|
yield s
|
||||||
await transaction.rollback()
|
|
||||||
await conn.close()
|
|
||||||
|
@pytest_asyncio.fixture(autouse=True)
|
||||||
|
async def _clean_db(engine):
|
||||||
|
"""Slet alt data fra alle tabeller efter hver test."""
|
||||||
|
yield
|
||||||
|
async with engine.begin() as conn:
|
||||||
|
for table in reversed(Base.metadata.sorted_tables):
|
||||||
|
await conn.execute(table.delete())
|
||||||
|
|
||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
async def client(session):
|
async def client():
|
||||||
"""HTTP client with injected test DB session."""
|
"""HTTP client — appen bruger sin egen DB session via get_db()."""
|
||||||
|
|
||||||
async def override_get_db():
|
|
||||||
yield session
|
|
||||||
|
|
||||||
app.dependency_overrides[get_db] = override_get_db
|
|
||||||
transport = ASGITransport(app=app)
|
transport = ASGITransport(app=app)
|
||||||
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
async with AsyncClient(transport=transport, base_url="http://test") as ac:
|
||||||
yield ac
|
yield ac
|
||||||
app.dependency_overrides.clear()
|
|
||||||
|
|
||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
async def admin_user(session):
|
async def admin_user(session):
|
||||||
"""Insert admin user into test DB (rulles tilbage af session-fixture)."""
|
"""Insert admin user into test DB (ryddes af _clean_db)."""
|
||||||
u = User(
|
u = User(
|
||||||
username="admin",
|
username="admin",
|
||||||
password_hash=hash_password("admin123"),
|
password_hash=hash_password("admin123"),
|
||||||
@@ -84,7 +78,7 @@ async def admin_user(session):
|
|||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
async def normal_user(session):
|
async def normal_user(session):
|
||||||
"""Insert normal user into test DB (rulles tilbage af session-fixture)."""
|
"""Insert normal user into test DB (ryddes af _clean_db)."""
|
||||||
u = User(
|
u = User(
|
||||||
username="testuser",
|
username="testuser",
|
||||||
password_hash=hash_password("test123"),
|
password_hash=hash_password("test123"),
|
||||||
|
|||||||
Reference in New Issue
Block a user