Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixtures: don't expire session objects on commit #104

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
Changes
=======

Version 2.1.6 (released 2023-10-31)

- Add ``db_session_options`` fixture.

Version 2.1.5 (released 2023-10-02)

- installation: pin Flask ``<2.3.0``.
Expand Down
2 changes: 1 addition & 1 deletion pytest_invenio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,6 @@ def test_browser(live_server, browser):
"""


__version__ = "2.1.5"
__version__ = "2.1.6"

__all__ = ("__version__",)
25 changes: 23 additions & 2 deletions pytest_invenio/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,24 @@ def database(appctx):


@pytest.fixture(scope="function")
def db(database):
def db_session_options():
"""Database session options.

Use to override options like ``expire_on_commit`` for the database session, which
helps with ``sqlalchemy.orm.exc.DetachedInstanceError`` when models are not bound
to the session between transactions/requests/service-calls.

.. code-block:: python

@pytest.fixture(scope='function')
def db_session_options():
return dict(expire_on_commit=False)
"""
return {}


@pytest.fixture(scope="function")
def db(database, db_session_options):
"""Creates a new database session for a test.

Scope: function
Expand All @@ -489,7 +506,11 @@ def db(database):
connection = database.engine.connect()
transaction = connection.begin()

options = dict(bind=connection, binds={})
options = dict(
bind=connection,
binds={},
**db_session_options,
)
session = database.create_scoped_session(options=options)

session.begin_nested()
Expand Down
1 change: 1 addition & 0 deletions pytest_invenio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
cli_runner,
database,
db,
db_session_options,
db_uri,
default_handler,
entry_points,
Expand Down
3 changes: 3 additions & 0 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import json
import os

import pytest


def test_version():
"""Test version import."""
Expand Down Expand Up @@ -402,6 +404,7 @@ def test_browser(live_server, browser):
conftest_testdir.runpytest().assert_outcomes(skipped=1)


@pytest.mark.skip(reason="outdated library")
def test_browser(conftest_testdir, monkeypatch):
"""Test live server and selenium."""
monkeypatch.setenv("E2E", "yes")
Expand Down
Loading