test: upgrade backend test suite, implement unit tests, and modernize CRUD patterns
Build Backend / build (push) Successful in 6m39s

This commit is contained in:
2026-06-08 16:17:11 +02:00
parent 4d53887fcc
commit a9e1e96622
13 changed files with 759 additions and 25 deletions
+13 -8
View File
@@ -5,15 +5,16 @@ from sqlmodel import SQLModel
from sqlalchemy.orm import sessionmaker
from httpx import AsyncClient, ASGITransport
from app.main import app
from app.database import get_session
@pytest_asyncio.fixture(scope="session")
@pytest_asyncio.fixture(scope="function")
async def engine():
"""Create a single in-memory SQLite engine for the entire test session."""
"""Create a fresh in-memory SQLite engine for each test."""
engine = create_async_engine(
"sqlite+aiosqlite:///:memory:",
echo=False,
)
# Create tables once for the whole session to avoid overhead
# Create tables for each test to ensure a clean state
async with engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)
yield engine
@@ -26,10 +27,10 @@ async def session_factory(engine):
@pytest_asyncio.fixture
async def db_session(session_factory):
"""Create a database session for a test. Rolls back after each test."""
"""Create a database session for a test."""
async with session_factory() as session:
yield session
await session.rollback()
# Rollback is still good practice, though engine is fresh each time
@pytest.fixture
def sample_session_data():
@@ -38,10 +39,14 @@ def sample_session_data():
"start_time": "2024-01-15T21:30:00Z",
}
@pytest.fixture
async def api_client(engine):
"""Provides an AsyncClient for testing API endpoints."""
@pytest_asyncio.fixture
async def api_client(db_session):
"""Provides an AsyncClient for testing API endpoints with the test database session."""
async def override():
yield db_session
app.dependency_overrides[get_session] = override
async with AsyncClient(
transport=ASGITransport(app=app), base_url="http://test"
) as client:
yield client
app.dependency_overrides.clear()