1
1
import typing
2
2
3
+ import modern_di
3
4
import modern_di_fastapi
4
5
import pytest
5
6
from httpx import ASGITransport , AsyncClient
@@ -18,19 +19,25 @@ async def client() -> typing.AsyncIterator[AsyncClient]:
18
19
yield client
19
20
20
21
22
+ @pytest .fixture
23
+ async def di_container () -> typing .AsyncIterator [modern_di .Container ]:
24
+ di_container_ : typing .Final = modern_di_fastapi .fetch_di_container (application )
25
+ async with di_container_ :
26
+ yield di_container_
27
+
28
+
21
29
@pytest .fixture (autouse = True )
22
- async def db_session () -> typing .AsyncIterator [AsyncSession ]:
23
- async with modern_di_fastapi .fetch_di_container (application ) as di_container :
24
- engine = await ioc .Dependencies .database_engine .async_resolve (di_container )
25
- connection = await engine .connect ()
26
- transaction = await connection .begin ()
27
- await connection .begin_nested ()
28
- ioc .Dependencies .database_engine .override (connection , di_container )
29
-
30
- try :
31
- yield AsyncSession (connection , expire_on_commit = False , autoflush = False )
32
- finally :
33
- if connection .in_transaction ():
34
- await transaction .rollback ()
35
- await connection .close ()
36
- await engine .dispose ()
30
+ async def db_session (di_container : modern_di .Container ) -> typing .AsyncIterator [AsyncSession ]:
31
+ engine = await ioc .Dependencies .database_engine .async_resolve (di_container )
32
+ connection = await engine .connect ()
33
+ transaction = await connection .begin ()
34
+ await connection .begin_nested ()
35
+ ioc .Dependencies .database_engine .override (connection , di_container )
36
+
37
+ try :
38
+ yield AsyncSession (connection , expire_on_commit = False , autoflush = False )
39
+ finally :
40
+ if connection .in_transaction ():
41
+ await transaction .rollback ()
42
+ await connection .close ()
43
+ await engine .dispose ()
0 commit comments