You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@fixture(autouse=True)defenable_transactional_tests(db_session):
# Automatically enable transactions for all tests,# without importing any extra fixtures.# set defulat session options, the same as we use by defaultforoption, valueinMY_SESSION_OPTIONS.items():
setattr(db_session, option, value)
db_session.session_factory.kw.update(MY_SESSION_OPTIONS)
# Override after_transaction_end event that is defined for pytest-flask-sqlalchemyevents_to_remove= []
forkey, valinsqlalchemy.event.registry._key_to_collection.items():
if"after_transaction_end"inkey:
weakref=list(val.values())[0]
fn=weakref()
if (
fnandfn.__name__=="restart_savepoint"andfn.__module__=="pytest_flask_sqlalchemy.fixtures"
):
events_to_remove.append((db_session, "after_transaction_end", fn))
forargsinevents_to_remove:
sqlalchemy.event.remove(*args)
@sqlalchemy.event.listens_for(db_session, "after_transaction_end")defrestart_savepoint(session, trans):
iftrans.nestedandnottrans._parent.nested:
# session.expire_all() <-- we don't want to expire objects in a sessionsession.begin_nested()
I have covered an endpoint with a test that counts DB queries and reveled n+1 issue.
The n+1 issue is only reproduced in tests so it seems to be related to this line (also if I remove this line the test pass):
https://github.com/jeancochrane/pytest-flask-sqlalchemy/blob/master/pytest_flask_sqlalchemy/fixtures.py#L59
In my app.py, I set following session_options:
I guess,
expire_on_commit
option should be considered in therestart_savepoint
function before expiring all objects.What do you think?
The text was updated successfully, but these errors were encountered: