10
10
11
11
import sogs .omq # noqa: E402
12
12
13
+ import sqlalchemy # noqa: E402
14
+
13
15
14
16
sogs .omq .test_suite = True
15
17
@@ -46,16 +48,51 @@ def client():
46
48
db_counter_ = 0
47
49
48
50
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
+
49
87
@pytest .fixture
50
- def db (request ):
88
+ def db (request , pgsql ):
51
89
"""
52
90
Import this fixture to get a wiped, re-initialized database for db.engine. The actual fixture
53
91
value is the db module itself (so typically you don't import it at all but instead get it
54
92
through this fixture, which also creates an empty db for you).
55
93
"""
56
94
57
95
trace = request .config .getoption ("--sql-tracing" )
58
- pgsql = request .config .getoption ("--pgsql" )
59
96
60
97
from sogs import db as db_
61
98
@@ -69,9 +106,8 @@ def db(request):
69
106
70
107
def pg_setup_schema ():
71
108
# Run everything in a separate schema that we can easily drop when done
72
- from sqlalchemy import event
73
109
74
- @event .listens_for (db_ .engine , "connect" , insert = True )
110
+ @sqlalchemy . event .listens_for (db_ .engine , "connect" , insert = True )
75
111
def setup_schema (dbapi_connection , connection_record ):
76
112
existing_autocommit = dbapi_connection .autocommit
77
113
dbapi_connection .autocommit = True
@@ -109,9 +145,14 @@ def sqlite_connect():
109
145
yield db_
110
146
111
147
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
+ ):
113
153
web .app .logger .critical ("DROPPING SCHEMA" )
114
154
db_ .query ("DROP SCHEMA sogs_tests CASCADE" )
155
+
115
156
web .appdb .close ()
116
157
117
158
0 commit comments