Repository: jeancochrane/pytest-flask-sqlalchemy (language: python)

Issue:
[Bug] SQLAlchemy 1.4.x doesn't work
With 1.3: https://github.com/codeforboston/police-data-trust/actions/runs/1006498785

With 1.4: https://github.com/codeforboston/police-data-trust/actions/runs/1006417486

The error message received in 1.4:

`AttributeError: Mock object has no attribute '_contextual_connect'`


Discussion / hints:

Fix patch:
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 49e0325..8b6f3a6 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -15,7 +15,7 @@ jobs:
     strategy:
       matrix:
         python: [3.6.x, 3.7.x]
-        sqlalchemy: [1.2.*, 1.3.*]
+        sqlalchemy: [1.2.*, 1.3.*, 1.4.*]
     services:
       postgres:
         image: postgres:11


Test patch:
diff --git a/pytest_flask_sqlalchemy/fixtures.py b/pytest_flask_sqlalchemy/fixtures.py
index 0aee994..0396bd6 100644
--- a/pytest_flask_sqlalchemy/fixtures.py
+++ b/pytest_flask_sqlalchemy/fixtures.py
@@ -102,7 +102,7 @@ def _engine(pytestconfig, request, _transaction, mocker):
     # https://docs.sqlalchemy.org/en/latest/changelog/migration_13.html
     if version.parse(sa.__version__) < version.parse('1.3'):
         engine.contextual_connect.return_value = connection
-    else:
+    elif version.parse(sa.__version__) < version.parse('1.4'):
         engine._contextual_connect.return_value = connection
 
     # References to `Engine.dialect` should redirect to the Connection (this
diff --git a/tests/_conftest.py b/tests/_conftest.py
index d9fc9e6..5dfa0b8 100644
--- a/tests/_conftest.py
+++ b/tests/_conftest.py
@@ -6,8 +6,7 @@
 import sqlalchemy as sa
 from flask import Flask
 from flask_sqlalchemy import SQLAlchemy
-from pytest_postgresql.factories import (init_postgresql_database,
-                                         drop_postgresql_database)
+from pytest_postgresql.janitor import DatabaseJanitor
 
 # Retrieve a database connection string from the shell environment
 try:
@@ -33,11 +32,10 @@ def database(request):
     pg_pass = DB_OPTS.get("password")
     pg_db = DB_OPTS["database"]
 
-    init_postgresql_database(pg_user, pg_host, pg_port, pg_db, pg_pass)
-
-    @request.addfinalizer
-    def drop_database():
-        drop_postgresql_database(pg_user, pg_host, pg_port, pg_db, 9.6, pg_pass)
+    janitor = DatabaseJanitor(pg_user, pg_host, pg_port, pg_db, 9.6, pg_pass)
+    janitor.init()
+    yield
+    janitor.drop()
 
 
 @pytest.fixture(scope='session')
diff --git a/tests/test_configs.py b/tests/test_configs.py
index f30d9f4..759a650 100644
--- a/tests/test_configs.py
+++ b/tests/test_configs.py
@@ -75,8 +75,7 @@ def test_missing_db_fixture(testdir):
         import sqlalchemy as sa
         from flask import Flask
         from flask_sqlalchemy import SQLAlchemy
-        from pytest_postgresql.factories import (init_postgresql_database,
-                                                drop_postgresql_database)
+        from pytest_postgresql.janitor import DatabaseJanitor
 
         # Retrieve a database connection string from the shell environment
         try:
@@ -102,11 +101,11 @@ def database(request):
             pg_pass = DB_OPTS.get("password")
             pg_db = DB_OPTS["database"]
 
-            init_postgresql_database(pg_user, pg_host, pg_port, pg_db, pg_pass)
+            janitor = DatabaseJanitor(pg_user, pg_host, pg_port, pg_db, 9.6, pg_pass)
+            janitor.init()
+            yield
+            janitor.drop()
 
-            @request.addfinalizer
-            def drop_database():
-                drop_postgresql_database(pg_user, pg_host, pg_port, pg_db, 9.6, pg_pass)
 
 
         @pytest.fixture(scope='session')
@@ -130,7 +129,7 @@ def test_missing_db_fixture(db_session):
     """)
 
     result = testdir.runpytest()
-    result.assert_outcomes(error=1)
+    result.assert_outcomes(errors=1)
     result.stdout.fnmatch_lines([
         '*NotImplementedError: _db fixture not defined*'
     ])