From f59a45d90e241cd1c3c497bd626fb18949de2c31 Mon Sep 17 00:00:00 2001 From: Artur Shiriev Date: Mon, 28 Sep 2026 09:23:27 +0300 Subject: [PATCH] refactor: build the test session with create_session --- app/resources/db.py | 2 +- tests/conftest.py | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/app/resources/db.py b/app/resources/db.py index d54aacc..d51db5b 100644 --- a/app/resources/db.py +++ b/app/resources/db.py @@ -50,7 +50,7 @@ def choose_sa_engine( return primary_engine -def create_session(engine: sa.AsyncEngine) -> sa.AsyncSession: +def create_session(engine: sa.AsyncEngine | sa.AsyncConnection) -> sa.AsyncSession: # join_transaction_mode is inert in production (the session binds to an engine); when tests bind # the session to a connection already in a transaction, it makes the session own a savepoint so # the outer transaction survives commits and the per-test rollback stays clean. diff --git a/tests/conftest.py b/tests/conftest.py index 64fd022..d064b3e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,7 +9,7 @@ from app import ioc from app.application import build_app -from app.resources.db import create_primary_sa_engine +from app.resources.db import create_primary_sa_engine, create_session if typing.TYPE_CHECKING: @@ -50,12 +50,7 @@ async def db_session(di_container: modern_di.Container) -> typing.AsyncIterator[ di_container.override(ioc.Dependencies.dynamic_engine, connection) try: - yield AsyncSession( - connection, - expire_on_commit=False, - autoflush=False, - join_transaction_mode="create_savepoint", - ) + yield create_session(connection) finally: if connection.in_transaction(): await transaction.rollback()