1
- import logging
2
1
import typing
3
2
3
+ import modern_di
4
4
import modern_di_litestar
5
5
import pytest
6
6
from httpx import ASGITransport , AsyncClient
10
10
from app .application import application
11
11
12
12
13
- logger = logging .getLogger (__name__ )
14
-
15
-
16
13
@pytest .fixture
17
14
async def client () -> typing .AsyncIterator [AsyncClient ]:
18
15
async with AsyncClient (
@@ -22,20 +19,25 @@ async def client() -> typing.AsyncIterator[AsyncClient]:
22
19
yield client
23
20
24
21
22
+ @pytest .fixture
23
+ async def di_container () -> typing .AsyncIterator [modern_di .Container ]:
24
+ di_container_ : typing .Final = modern_di_litestar .fetch_di_container (application )
25
+ async with di_container_ :
26
+ yield di_container_
27
+
28
+
25
29
@pytest .fixture (autouse = True )
26
- async def db_session () -> typing .AsyncIterator [AsyncSession ]:
27
- async with modern_di_litestar .fetch_di_container (application ) as di_container :
28
- engine = await ioc .Dependencies .database_engine .async_resolve (di_container )
29
- connection = await engine .connect ()
30
- transaction = await connection .begin ()
31
- await connection .begin_nested ()
32
- ioc .Dependencies .database_engine .override (connection , di_container )
33
-
34
- try :
35
- yield AsyncSession (connection , expire_on_commit = False , autoflush = False )
36
- finally :
37
- if connection .in_transaction ():
38
- await transaction .rollback ()
39
- await connection .close ()
40
- await engine .dispose ()
41
- logger .info ("Fixture db_session is closed" )
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