File tree Expand file tree Collapse file tree 4 files changed +53
-12
lines changed
samples/db-learning/tests Expand file tree Collapse file tree 4 files changed +53
-12
lines changed Original file line number Diff line number Diff line change 1+ import inspect
12import typing as t
23
4+ import factory
35import sqlalchemy as sa
46import sqlalchemy .orm as sa_orm
57from ellar .threading import run_as_sync
@@ -118,3 +120,37 @@ def _save(
118120 cls ._session_execute (session .commit )
119121 cls ._session_execute (session .refresh , obj )
120122 return obj
123+
124+
125+ class EllarSQLSubFactoryId (factory .SubFactory ):
126+ """
127+ A SubFactory that returns the id of the created object.
128+ """
129+
130+ def evaluate (self , instance , step , extra ):
131+ value = super ().evaluate (instance , step , extra )
132+ if inspect .isawaitable (value ):
133+
134+ async def resolve_value ():
135+ resolved_value = await value
136+ return resolved_value .id
137+
138+ return resolve_value ()
139+ return value
140+
141+
142+ class EllarSQLSubFactory (factory .SubFactory ):
143+ """
144+ A SubFactory that returns the created object.
145+ """
146+
147+ def evaluate (self , instance , step , extra ):
148+ value = super ().evaluate (instance , step , extra )
149+ if inspect .isawaitable (value ):
150+
151+ async def resolve_value ():
152+ resolved_value = await value
153+ return resolved_value
154+
155+ return resolve_value ()
156+ return value
Original file line number Diff line number Diff line change @@ -27,6 +27,18 @@ def _raise_exception():
2727 return _raise_exception
2828
2929
30+ async def _session_cleanup (
31+ db_service : EllarSQLService , session : t .Union [Session , AsyncSession ]
32+ ) -> None :
33+ res = db_service .session_factory .remove ()
34+ if isinstance (res , t .Coroutine ):
35+ await res
36+
37+ res = session .close ()
38+ if isinstance (res , t .Coroutine ):
39+ await res
40+
41+
3042@as_middleware
3143async def session_middleware (
3244 context : IHostContext , call_next : t .Callable [..., t .Coroutine ]
@@ -40,15 +52,8 @@ async def session_middleware(
4052
4153 try :
4254 await call_next ()
43- except Exception as ex :
44- res = session .rollback ()
45- if isinstance (res , t .Coroutine ):
46- await res
47- raise ex
48-
49- res = db_service .session_factory .remove ()
50- if isinstance (res , t .Coroutine ):
51- await res
55+ finally :
56+ await _session_cleanup (db_service , session )
5257
5358
5459@Module (
Original file line number Diff line number Diff line change @@ -4,9 +4,9 @@ autoflake
44ellar-cli >= 0.3.7
55factory-boy >= 3.3.0
66httpx
7- Pillow >=10.4.0, <11.2.0
87mypy == 1.15.0
8+ Pillow >=10.4.0, <11.2.0
99pytest >= 7.1.3,< 9.0.0
1010pytest-asyncio
1111pytest-cov >= 2.12.0,< 7.0.0
12- ruff ==0.9.9
12+ ruff ==0.13.3
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ def tm():
1616 test_module = Test .create_test_module (modules = [ApplicationModule ])
1717 app = test_module .create_application ()
1818
19- with execute_async_context_manager (app .application_context ()):
19+ with execute_async_context_manager (app .with_injector_context ()):
2020 yield test_module
2121
2222
You can’t perform that action at this time.
0 commit comments