Skip to content

Commit ee1ba6a

Browse files
committed
Support SQLAlchemy 1.3 deprecated threadlocal strategy
SQLAlchemy 1.3 has deprecated the threadlocal engine strategy. This change has the side effect of making the Engine.contextual_connect() method private, which interferes with our mocking. Depending on the installed version of SQLAlchemy, mock this method differently. See: - https://docs.sqlalchemy.org/en/latest/changelog/migration_13.html
1 parent e9ddaab commit ee1ba6a

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

pytest_flask_sqlalchemy/fixtures.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55
import sqlalchemy as sa
6+
from packaging import version
67

78

89
@pytest.fixture(scope='module')
@@ -95,7 +96,14 @@ def _engine(pytestconfig, request, _transaction, mocker):
9596
engine = mocker.MagicMock(spec=sa.engine.Engine)
9697

9798
engine.connect.return_value = connection
98-
engine.contextual_connect.return_value = connection
99+
100+
# Threadlocal engine strategies were deprecated in SQLAlchemy 1.3, which
101+
# resulted in contextual_connect becoming a private method. See:
102+
# https://docs.sqlalchemy.org/en/latest/changelog/migration_13.html
103+
if version.parse(sa.__version__) < version.parse('1.3'):
104+
engine.contextual_connect.return_value = connection
105+
else:
106+
engine._contextual_connect.return_value = connection
99107

100108
# References to `Engine.dialect` should redirect to the Connection (this
101109
# is primarily useful for the `autoload` flag in SQLAlchemy, which references

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ def readme():
1919
install_requires=['pytest>=3.2.1',
2020
'pytest-mock>=1.6.2',
2121
'SQLAlchemy>=1.2.2',
22-
'Flask-SQLAlchemy>=2.3'],
22+
'Flask-SQLAlchemy>=2.3',
23+
'packaging>=14.1'],
2324
extras_require={'tests': ['pytest-postgresql']},
2425
classifiers=[
2526
'Development Status :: 4 - Beta',

0 commit comments

Comments
 (0)