/ fastapi-template / tests / fixtures.py
fixtures.py
 1  from typing import AsyncIterator
 2  from unittest.mock import AsyncMock
 3  
 4  import pytest
 5  from _pytest.monkeypatch import MonkeyPatch
 6  from httpx import AsyncClient
 7  from pytest_mock import MockerFixture
 8  from sqlalchemy.ext.asyncio import create_async_engine
 9  
10  from api.app import app
11  from api.database import db
12  
13  
14  @pytest.fixture(autouse=True)
15  async def database(monkeypatch: MonkeyPatch) -> None:
16      monkeypatch.setattr(db, "engine", create_async_engine("sqlite+aiosqlite:///:memory:"))
17      await db.create_tables()
18  
19  
20  @pytest.fixture
21  async def client() -> AsyncIterator[AsyncClient]:
22      async with AsyncClient(app=app, base_url="http://test") as client:
23          yield client
24  
25  
26  @pytest.fixture
27  async def auth_client(client: AsyncClient, mocker: MockerFixture) -> AsyncIterator[AsyncClient]:
28      mocker.patch("api.auth.StaticTokenAuth._check_token", AsyncMock(return_value=True))
29      mocker.patch("api.auth.JWTAuth.__call__", AsyncMock(return_value={"foo": "bar"}))
30      yield client