-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconftest.py
More file actions
87 lines (62 loc) · 2.13 KB
/
conftest.py
File metadata and controls
87 lines (62 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import typing
from typing import AsyncGenerator
from unittest.mock import AsyncMock, patch
import pytest
from httpx import ASGITransport, AsyncClient
from sqlalchemy import StaticPool
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from app.dependencies import get_db
from app.main import app
from app.models._base import AbstractBase
from app.settings import Settings
if typing.TYPE_CHECKING:
pass
settings = Settings() # type: ignore
# Uses an SQLITE In-memory DB for setting
DATABASE_URL = "sqlite+aiosqlite:///:memory:"
engine = create_async_engine(
DATABASE_URL,
connect_args={
"check_same_thread": False,
},
poolclass=StaticPool,
)
TestingSessionLocal = async_sessionmaker(bind=engine, expire_on_commit=False)
async def override_get_db() -> AsyncGenerator[AsyncSession, None]:
async with TestingSessionLocal() as session:
yield session
app.dependency_overrides[get_db] = override_get_db
@pytest.fixture(autouse=True)
async def run_migrations():
await setup_db()
@pytest.fixture(scope="session", autouse=True)
def mock_fastmail_send():
"""
Globally patches FastMail.send_message for the entire test session.
"""
# Use the string path to where FastMail is imported in your app
with patch("app.mailer.FastMail.send_message", new_callable=AsyncMock) as mock:
yield mock
@pytest.fixture
async def client():
base_url = "http://test"
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url=base_url) as client:
yield client
@pytest.fixture
async def session():
async for session_obj in override_get_db():
yield session_obj
@pytest.fixture
async def user(session: AsyncSession):
from app.models.tests.factories import UserFactory
user = UserFactory.build()
session.add(user)
await session.commit()
await session.refresh(user)
await session.flush()
return user
async def setup_db() -> None:
async with engine.begin() as conn:
await conn.run_sync(AbstractBase.metadata.drop_all)
await conn.run_sync(AbstractBase.metadata.create_all)