Skip to content

Commit 14a5e06

Browse files
committed
SQLAlchemy 1.3 pg compatibility workaround
1 parent cb877e2 commit 14a5e06

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

tests/conftest.py

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
import sogs.omq # noqa: E402
1212

13+
import sqlalchemy # noqa: E402
14+
1315

1416
sogs.omq.test_suite = True
1517

@@ -46,16 +48,51 @@ def client():
4648
db_counter_ = 0
4749

4850

51+
# Under postgresql & sqlalchemy 1.3.x we have to hack around a bit to get sqlalchemy to properly use
52+
# the pgsql search_path, most notably passing it in the connect string, but that fails if the schema
53+
# doesn't already exist; thus we establish an extra connection to the database to create it at
54+
# session startup and tear it down at session end.
55+
#
56+
# Under higher sqlalchemy this simply provides the pgsql url, and under sqlite this returns None.
57+
@pytest.fixture(scope="session")
58+
def pgsql(request):
59+
pgsql = request.config.getoption("--pgsql")
60+
if pgsql:
61+
if sqlalchemy.__version__.startswith("1.3."):
62+
import psycopg2
63+
64+
conn = psycopg2.connect(pgsql)
65+
with conn.cursor() as cur:
66+
cur.execute("CREATE SCHEMA IF NOT EXISTS sogs_tests")
67+
conn.commit()
68+
69+
pgsql_w_schema = pgsql + ('&' if '?' in pgsql else '?')
70+
pgsql_w_schema += 'options=--search_path%3Dsogs_tests'
71+
72+
yield pgsql_w_schema
73+
74+
if not request.config.getoption("--pgsql-no-drop-schema"):
75+
print("DROPPING SCHEMA")
76+
with conn.cursor() as cur:
77+
cur.execute("DROP SCHEMA sogs_tests CASCADE")
78+
conn.commit()
79+
80+
conn.close()
81+
else:
82+
yield pgsql
83+
else:
84+
yield None
85+
86+
4987
@pytest.fixture
50-
def db(request):
88+
def db(request, pgsql):
5189
"""
5290
Import this fixture to get a wiped, re-initialized database for db.engine. The actual fixture
5391
value is the db module itself (so typically you don't import it at all but instead get it
5492
through this fixture, which also creates an empty db for you).
5593
"""
5694

5795
trace = request.config.getoption("--sql-tracing")
58-
pgsql = request.config.getoption("--pgsql")
5996

6097
from sogs import db as db_
6198

@@ -69,9 +106,8 @@ def db(request):
69106

70107
def pg_setup_schema():
71108
# Run everything in a separate schema that we can easily drop when done
72-
from sqlalchemy import event
73109

74-
@event.listens_for(db_.engine, "connect", insert=True)
110+
@sqlalchemy.event.listens_for(db_.engine, "connect", insert=True)
75111
def setup_schema(dbapi_connection, connection_record):
76112
existing_autocommit = dbapi_connection.autocommit
77113
dbapi_connection.autocommit = True
@@ -109,9 +145,14 @@ def sqlite_connect():
109145
yield db_
110146

111147
web.app.logger.warning("closing db")
112-
if pgsql and not request.config.getoption("--pgsql-no-drop-schema"):
148+
if (
149+
pgsql
150+
and not sqlalchemy.__version__.startswith("1.3.")
151+
and not request.config.getoption("--pgsql-no-drop-schema")
152+
):
113153
web.app.logger.critical("DROPPING SCHEMA")
114154
db_.query("DROP SCHEMA sogs_tests CASCADE")
155+
115156
web.appdb.close()
116157

117158

0 commit comments

Comments
 (0)