inveniosoftware / pytest-invenio

Pytest fixtures for Invenio.
https://pytest-invenio.readthedocs.io
Other
3 stars 22 forks source link

db fixture incompatibility with Flask-SQLAlchemy #100

Open ntarocco opened 1 year ago

ntarocco commented 1 year ago

Flask-SQLAlchemy v3 introduced breaking changing related to scoped and nested session. The following PRs attempted to solve the issue:

While local tests were passing, tests in modules using pytest-invenio started failing.

Explanation

The db fixture has scope: function: it creates a nested session, which starts a transaction using a SAVEPOINT. When the test completes, the DB state is rolled back to the previous clean state. See doc here and how to use it in the tests here.

The new version of Flask-SQLAlchemy v3 removes the previously public method create_scoped_session and changes how the DB session are bound to the Flask context. This breaks the fixture.

Note: what is unclear is why the local tests pass (see test_db test), while tests in other modules using that fixtures fail.

Relevant references:

monotasker commented 1 year ago

Is there any fix for this in the works? When I'm running v. 2.1.2, I get an error when the db fixture tries to call database.create_scoped_session(options=options)? I got rid of the error by changing this to call database._make_scoped_session(options=options). But I don't know whether the resulting fixture will work as expected.

ntarocco commented 1 year ago

@utnapischtim is working on that to find a solution. At the moment, no fix available yet.