CI: rewrite session-fixture med transaction rollback og dedikeret connection
This commit is contained in:
@@ -2,7 +2,10 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import pytest_asyncio
|
import pytest_asyncio
|
||||||
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
|
from sqlalchemy.ext.asyncio import (
|
||||||
|
create_async_engine,
|
||||||
|
async_sessionmaker,
|
||||||
|
)
|
||||||
from httpx import ASGITransport, AsyncClient
|
from httpx import ASGITransport, AsyncClient
|
||||||
|
|
||||||
from app.database import Base, get_db
|
from app.database import Base, get_db
|
||||||
@@ -33,10 +36,23 @@ async def engine():
|
|||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
async def session(engine):
|
async def session(engine):
|
||||||
"""Per-test database session."""
|
"""Per-test database session med transaction rollback.
|
||||||
maker = async_sessionmaker(engine, expire_on_commit=False)
|
|
||||||
|
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.
|
||||||
|
"""
|
||||||
|
conn = await engine.connect()
|
||||||
|
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
|
@pytest_asyncio.fixture
|
||||||
@@ -55,7 +71,7 @@ async def client(session):
|
|||||||
|
|
||||||
@pytest_asyncio.fixture
|
@pytest_asyncio.fixture
|
||||||
async def admin_user(session):
|
async def admin_user(session):
|
||||||
"""Insert admin user into test DB."""
|
"""Insert admin user into test DB (rulles tilbage af session-fixture)."""
|
||||||
u = User(
|
u = User(
|
||||||
username="admin",
|
username="admin",
|
||||||
password_hash=hash_password("admin123"),
|
password_hash=hash_password("admin123"),
|
||||||
@@ -68,7 +84,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."""
|
"""Insert normal user into test DB (rulles tilbage af session-fixture)."""
|
||||||
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