CI: drop outer transaction/savepoint, brug per-test cleanup i stedet
CI/CD / test (push) Failing after 1m51s
CI/CD / build (push) Has been skipped

This commit is contained in:
2026-07-11 03:49:32 +02:00
parent 26d20a5fa9
commit 1c23e45261
+17 -23
View File
@@ -36,42 +36,36 @@ async def engine():
@pytest_asyncio.fixture
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.
commit() inde i testen opretter en savepoint (via create_savepoint),
så al data rulles tilbage efter testen — ingen krydskontaminering.
Data commited her er synlig for appens egen session via HTTP client.
Renses automatisk af _clean_db efter hver test.
"""
conn = await engine.connect()
transaction = await conn.begin()
maker = async_sessionmaker(
bind=conn,
expire_on_commit=False,
join_transaction_mode="create_savepoint",
)
maker = async_sessionmaker(bind=engine, expire_on_commit=False)
async with maker() as 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
async def client(session):
"""HTTP client with injected test DB session."""
async def override_get_db():
yield session
app.dependency_overrides[get_db] = override_get_db
async def client():
"""HTTP client — appen bruger sin egen DB session via get_db()."""
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as ac:
yield ac
app.dependency_overrides.clear()
@pytest_asyncio.fixture
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(
username="admin",
password_hash=hash_password("admin123"),
@@ -84,7 +78,7 @@ async def admin_user(session):
@pytest_asyncio.fixture
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(
username="testuser",
password_hash=hash_password("test123"),