|
| 1 | +from collections.abc import AsyncGenerator |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +import pytest |
| 5 | +from fastapi import FastAPI |
| 6 | +from httpx import ASGITransport, AsyncClient |
| 7 | +from sqlalchemy.ext.asyncio import ( |
| 8 | + AsyncEngine, |
| 9 | + AsyncSession, |
| 10 | + async_sessionmaker, |
| 11 | + create_async_engine, |
| 12 | +) |
| 13 | + |
| 14 | +from src.app import create_app |
| 15 | +from src.core.config import get_app_settings |
| 16 | +from src.db.dependencies import get_db_session |
| 17 | +from src.db.meta import meta |
| 18 | +from src.db.models import load_all_models |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture(scope="session") |
| 22 | +async def _engine() -> AsyncGenerator[AsyncEngine, None]: |
| 23 | + """ |
| 24 | + Create engine and databases. |
| 25 | +
|
| 26 | + :yield: new engine. |
| 27 | + """ |
| 28 | + settings = get_app_settings() |
| 29 | + |
| 30 | + load_all_models() |
| 31 | + |
| 32 | + engine = create_async_engine(str(settings.DATABASE_URL)) |
| 33 | + async with engine.begin() as conn: |
| 34 | + await conn.run_sync(meta.create_all) |
| 35 | + |
| 36 | + try: |
| 37 | + yield engine |
| 38 | + finally: |
| 39 | + await engine.dispose() |
| 40 | + |
| 41 | + |
| 42 | +@pytest.fixture |
| 43 | +async def dbsession( |
| 44 | + _engine: AsyncEngine, |
| 45 | +) -> AsyncGenerator[AsyncSession, None]: |
| 46 | + """ |
| 47 | + Get session to database. |
| 48 | +
|
| 49 | + Fixture that returns a SQLAlchemy session with a SAVEPOINT, and the rollback to it |
| 50 | + after the test completes. |
| 51 | +
|
| 52 | + :param _engine: current engine. |
| 53 | + :yields: async session. |
| 54 | + """ |
| 55 | + connection = await _engine.connect() |
| 56 | + trans = await connection.begin() |
| 57 | + |
| 58 | + session_maker = async_sessionmaker( |
| 59 | + connection, |
| 60 | + expire_on_commit=False, |
| 61 | + ) |
| 62 | + session = session_maker() |
| 63 | + |
| 64 | + try: |
| 65 | + yield session |
| 66 | + finally: |
| 67 | + await session.close() |
| 68 | + await trans.rollback() |
| 69 | + await connection.close() |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture |
| 73 | +async def fastapi_app( |
| 74 | + dbsession: AsyncSession, |
| 75 | +) -> FastAPI: |
| 76 | + """ |
| 77 | + Fixture for creating FastAPI app. |
| 78 | +
|
| 79 | + :return: fastapi app with mocked dependencies. |
| 80 | + """ |
| 81 | + application = create_app() |
| 82 | + application.dependency_overrides[get_db_session] = lambda: dbsession |
| 83 | + return application |
| 84 | + |
| 85 | + |
| 86 | +@pytest.fixture(scope="session") |
| 87 | +def anyio_backend() -> str: |
| 88 | + """ |
| 89 | + Backend for anyio pytest plugin. |
| 90 | +
|
| 91 | + :return: backend name. |
| 92 | + """ |
| 93 | + return "asyncio" |
| 94 | + |
| 95 | + |
| 96 | +@pytest.fixture |
| 97 | +async def client( |
| 98 | + fastapi_app: FastAPI, |
| 99 | + anyio_backend: Any, |
| 100 | +) -> AsyncGenerator[AsyncClient, None]: |
| 101 | + """ |
| 102 | + Fixture that creates client for requesting server. |
| 103 | +
|
| 104 | + :param fastapi_app: the application. |
| 105 | + :yield: client for the app. |
| 106 | + """ |
| 107 | + transport = ASGITransport(fastapi_app) |
| 108 | + async with AsyncClient( |
| 109 | + transport=transport, |
| 110 | + base_url="http://test", |
| 111 | + ) as client: |
| 112 | + yield client |
0 commit comments