Skip to content

Commit 87084a6

Browse files
authored
fix: ensure app under test uses session fixture - DM-6561 (#55)
1 parent 3510c08 commit 87084a6

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

fastapi_sqla/_pytest_plugin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,15 @@ def sqla_reflection(sqla_modules, sqla_connection, db_url):
102102
@fixture
103103
def patch_engine_from_config(request, db_url, sqla_connection, sqla_transaction):
104104
"""So that all DB operations are never written to db for real."""
105+
from fastapi_sqla import _Session
106+
105107
if "dont_patch_engines" in request.keywords:
106108
yield
107109

108110
else:
109111
with patch("fastapi_sqla.engine_from_config") as engine_from_config:
110112
engine_from_config.return_value = sqla_connection
113+
_Session.configure(bind=sqla_connection)
111114
yield engine_from_config
112115

113116

@@ -163,6 +166,8 @@ async def patch_create_async_engine(
163166
async_sqlalchemy_url, async_sqla_connection, request
164167
):
165168
"""So that all async DB operations are never written to db for real."""
169+
from fastapi_sqla.asyncio_support import _AsyncSession
170+
166171
if "dont_patch_engines" in request.keywords:
167172
yield
168173

@@ -171,6 +176,9 @@ async def patch_create_async_engine(
171176
"fastapi_sqla.asyncio_support.create_async_engine"
172177
) as create_async_engine:
173178
create_async_engine.return_value = async_sqla_connection
179+
_AsyncSession.configure(
180+
bind=async_sqla_connection, expire_on_commit=False
181+
)
174182
yield create_async_engine
175183

176184
@fixture

0 commit comments

Comments
 (0)