Repository navigation
Expand file tree
/
Copy pathconftest.py
More file actions
106 lines (83 loc) · 3.27 KB
/
Copy pathconftest.py
File metadata and controls
106 lines (83 loc) · 3.27 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import typing
import modern_di_fastapi
import pytest
from asgi_lifespan import LifespanManager
from httpx import ASGITransport, AsyncClient
from polyfactory.factories.sqlalchemy_factory import SQLAlchemyFactory
from sqlalchemy import event
from app import ioc
from app.application import build_app
from app.resources.db import create_sa_engine, create_session
from app.settings import settings
if typing.TYPE_CHECKING:
import fastapi
import modern_di
from sqlalchemy.engine import Connection
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession
SAVEPOINT_PREFIXES: typing.Final = ("SAVEPOINT", "RELEASE SAVEPOINT", "ROLLBACK TO SAVEPOINT")
@pytest.fixture
async def app() -> typing.AsyncIterator[fastapi.FastAPI]:
app_ = build_app()
async with LifespanManager(app_):
yield app_
@pytest.fixture
async def client(app: fastapi.FastAPI) -> typing.AsyncIterator[AsyncClient]:
async with AsyncClient(
transport=ASGITransport(app=app),
base_url="http://test",
) as client:
yield client
@pytest.fixture
async def di_container(app: fastapi.FastAPI) -> typing.AsyncIterator[modern_di.Container]:
container = modern_di_fastapi.fetch_di_container(app)
try:
yield container
finally:
await container.close_async()
@pytest.fixture
async def db_session(di_container: modern_di.Container) -> typing.AsyncIterator[AsyncSession]:
engine = create_sa_engine(settings=settings, url=settings.db_dsn_parsed)
connection = await engine.connect()
transaction = await connection.begin()
di_container.override(ioc.Database.dynamic_engine, connection)
# Switching the shared connection to AUTOCOMMIT would roll back the outer transaction.
di_container.override(ioc.Database.autocommit_isolation_level, None)
try:
yield create_session(connection)
finally:
if connection.in_transaction():
await transaction.rollback()
await connection.close()
await engine.dispose()
di_container.reset_override()
@pytest.fixture
async def set_async_session_in_base_sqlalchemy_factory(
db_session: AsyncSession,
) -> typing.AsyncIterator[None]:
try:
SQLAlchemyFactory.__async_session__ = db_session
yield
finally:
SQLAlchemyFactory.__async_session__ = None
@pytest.fixture
def statements(db_session: AsyncSession) -> typing.Iterator[list[str]]:
sync_connection: typing.Final = typing.cast("Connection", db_session.sync_session.bind)
recorded: typing.Final[list[str]] = []
def record(*args: object) -> None:
statement: typing.Final = typing.cast("str", args[2])
if not statement.startswith(SAVEPOINT_PREFIXES):
recorded.append(statement)
event.listen(sync_connection, "before_cursor_execute", record)
try:
yield recorded
finally:
event.remove(sync_connection, "before_cursor_execute", record)
@pytest.fixture
async def committing_engine(di_container: modern_di.Container) -> typing.AsyncIterator[AsyncEngine]:
engine = create_sa_engine(settings=settings, url=settings.db_dsn_parsed)
di_container.override(ioc.Database.dynamic_engine, engine)
try:
yield engine
finally:
di_container.reset_override(ioc.Database.dynamic_engine)
await engine.dispose()