@@ -216,47 +216,52 @@ Now, let's create a factory for our user model in `tests/factories.py`:
216216
217217``` python title="tests/factories.py"
218218import factory
219- from ellar_sql.factory import EllarSQLFactory, SESSION_PERSISTENCE_FLUSH
220219from db_learning.models import User
221- from . import common
220+ from ellar.app import current_injector
221+ from sqlalchemy.orm import Session
222+
223+ from ellar_sql.factory import SESSION_PERSISTENCE_FLUSH , EllarSQLFactory
224+
225+
226+ def _get_session ():
227+ session = current_injector.get(Session)
228+ return session
222229
223230class UserFactory (EllarSQLFactory ):
224231 class Meta :
225232 model = User
226233 sqlalchemy_session_persistence = SESSION_PERSISTENCE_FLUSH
227- sqlalchemy_session_factory = lambda : common.Session()
234+ sqlalchemy_session_factory = _get_session
228235
229236 username = factory.Faker(' username' )
230237 email = factory.Faker(' email' )
231238```
232239
233- The ` UserFactory ` depends on a database session. Since the pytest fixture we created applies to it,
234- we also need a session factory in ` tests/common.py ` :
235-
236- ``` python title="tests/common.py"
237- from sqlalchemy import orm
238-
239- Session = orm.scoped_session(orm.sessionmaker())
240- ```
240+ The ` UserFactory ` depends on a database Session as you see from ` _get_session() ` function.
241+ We need to ensure that test fixture provides ` ApplicationContext ` for ` current_injector ` to work.
241242
242- Additionally , we require a fixture responsible for configuring the Factory session in ` tests/conftest.py ` :
243+ So in ` tests/conftest.py ` , we make ` tm ` test fixture to run application context :
243244
244245``` python title="tests/conftest.py"
245246import os
247+
246248import pytest
247- import sqlalchemy as sa
249+ from db_learning.root_module import ApplicationModule
248250from ellar.common.constants import ELLAR_CONFIG_MODULE
249251from ellar.testing import Test
252+ from ellar.threading.sync_worker import execute_async_context_manager
253+
250254from ellar_sql import EllarSQLService
251- from db_learning.root_module import ApplicationModule
252- from . import common
253255
254256os.environ.setdefault(ELLAR_CONFIG_MODULE , " db_learning.config:TestConfig" )
255257
256258@pytest.fixture (scope = ' session' )
257259def tm ():
258260 test_module = Test.create_test_module(modules = [ApplicationModule])
259- yield test_module
261+ app = test_module.create_application()
262+
263+ with execute_async_context_manager(app.application_context()):
264+ yield test_module
260265
261266# Fixture for creating a database session for testing
262267@pytest.fixture (scope = ' session' )
@@ -270,29 +275,8 @@ def db(tm):
270275
271276 # Dropping all tables after the tests
272277 db_service.drop_all()
273-
274- # Fixture for creating a database session for testing
275- @pytest.fixture (scope = ' session' )
276- def db_session (db , tm ):
277- db_service = tm.get(EllarSQLService)
278-
279- yield db_service.session_factory()
280-
281- # Removing the session factory
282- db_service.session_factory.remove()
283-
284- @pytest.fixture
285- def factory_session (db , tm ):
286- engine = tm.get(sa.Engine)
287- common.Session.configure(bind = engine)
288- yield
289- common.Session.remove()
290278```
291279
292- In the ` factory_session ` fixture, we retrieve the ` Engine ` registered in the DI container by ** EllarSQLModule** .
293- Using this engine, we configure the common ` Session ` . It's important to note that if you are using an
294- async database driver, ** EllarSQLModule** will register ` AsyncEngine ` .
295-
296280With this setup, we can rewrite our ` test_username_must_be_unique ` test using ` UserFactory ` and ` factory_session ` :
297281
298282``` python title="tests/test_user_model.py"
0 commit comments