feat: enhance database session management with commit and rollback; add user and deal API tests
Test / test (push) Successful in 14s

This commit is contained in:
k1nq
2025-11-28 11:35:27 +05:00
parent 0ab3bfbb34
commit 193fa73c78
6 changed files with 330 additions and 2 deletions
+22 -1
View File
@@ -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)