feat: enhance database session management with commit and rollback; add user and deal API tests
Test / test (push) Successful in 14s
Test / test (push) Successful in 14s
This commit is contained in:
@@ -3,15 +3,31 @@ from __future__ import annotations
|
||||
|
||||
from collections.abc import AsyncGenerator
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from httpx import ASGITransport, AsyncClient
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
|
||||
from app.api.deps import get_db_session
|
||||
from app.core.security import password_hasher
|
||||
from app.main import create_app
|
||||
from app.models import Base
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def stub_password_hasher(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Replace bcrypt-dependent hashing with deterministic helpers for tests."""
|
||||
|
||||
def fake_hash(password: str) -> str:
|
||||
return f"hashed-{password}"
|
||||
|
||||
def fake_verify(password: str, hashed_password: str) -> bool:
|
||||
return hashed_password == f"hashed-{password}"
|
||||
|
||||
monkeypatch.setattr(password_hasher, "hash", fake_hash)
|
||||
monkeypatch.setattr(password_hasher, "verify", fake_verify)
|
||||
|
||||
|
||||
@pytest_asyncio.fixture()
|
||||
async def session_factory() -> AsyncGenerator[async_sessionmaker[AsyncSession], None]:
|
||||
engine = create_async_engine("sqlite+aiosqlite:///:memory:", future=True)
|
||||
@@ -30,7 +46,12 @@ async def client(
|
||||
|
||||
async def _get_session_override() -> AsyncGenerator[AsyncSession, None]:
|
||||
async with session_factory() as session:
|
||||
yield session
|
||||
try:
|
||||
yield session
|
||||
await session.commit()
|
||||
except Exception:
|
||||
await session.rollback()
|
||||
raise
|
||||
|
||||
app.dependency_overrides[get_db_session] = _get_session_override
|
||||
transport = ASGITransport(app=app)
|
||||
|
||||
Reference in New Issue
Block a user