14
14
sogs .omq .test_suite = True
15
15
16
16
17
+ def pgsql_url (arg ):
18
+ if not arg .startswith ('postgresql:' ):
19
+ raise ValueError ("Invalid postgresql url; see SQLAlchemy postgresql docs" )
20
+ return arg
21
+
22
+
17
23
def pytest_addoption (parser ):
18
24
parser .addoption (
19
25
"--sql-tracing" , action = "store_true" , default = False , help = "Log all SQL queries"
20
26
)
27
+ parser .addoption (
28
+ "--pgsql" , type = pgsql_url , help = "Use the given postgresql database url for testing"
29
+ )
30
+ parser .addoption (
31
+ "--pgsql-no-drop-schema" ,
32
+ action = "store_true" ,
33
+ default = False ,
34
+ help = "Don't clean up the final test schema; typically used with --maxfail=1" ,
35
+ )
21
36
22
37
23
38
@pytest .fixture
@@ -40,28 +55,63 @@ def db(request):
40
55
"""
41
56
42
57
trace = request .config .getoption ("--sql-tracing" )
58
+ pgsql = request .config .getoption ("--pgsql" )
43
59
44
60
from sogs import db as db_
45
61
46
62
global db_counter_
47
63
db_counter_ += 1
48
- sqlite_uri = f'file:sogs_testdb{ db_counter_ } ?mode=memory&cache=shared'
49
64
50
- web .app .logger .warning (f"using sqlite { sqlite_uri } " )
65
+ if pgsql :
66
+ web .app .logger .warning (f"using postgresql { pgsql } " )
67
+
68
+ first = True
69
+
70
+ def pg_setup_schema ():
71
+ # Run everything in a separate schema that we can easily drop when done
72
+ from sqlalchemy import event
73
+
74
+ @event .listens_for (db_ .engine , "connect" , insert = True )
75
+ def setup_schema (dbapi_connection , connection_record ):
76
+ existing_autocommit = dbapi_connection .autocommit
77
+ dbapi_connection .autocommit = True
78
+
79
+ cursor = dbapi_connection .cursor ()
80
+ nonlocal first
81
+ if first :
82
+ cursor .execute ("DROP SCHEMA IF EXISTS sogs_tests CASCADE" )
83
+ first = False
84
+ cursor .execute ("CREATE SCHEMA IF NOT EXISTS sogs_tests" )
85
+ cursor .execute ("SET search_path TO sogs_tests" )
86
+ cursor .close ()
87
+
88
+ dbapi_connection .autocommit = existing_autocommit
89
+
90
+ db_ ._init_engine (pgsql , echo = trace , sogs_preinit = pg_setup_schema )
91
+
92
+ else :
93
+ sqlite_uri = f'file:sogs_testdb{ db_counter_ } ?mode=memory&cache=shared'
94
+
95
+ web .app .logger .warning (f"using sqlite { sqlite_uri } " )
96
+
97
+ def sqlite_connect ():
98
+ import sqlite3
51
99
52
- def sqlite_connect ():
53
- import sqlite3
100
+ web . app . logger . warning ( f"connecting to { sqlite_uri } " )
101
+ return sqlite3 . connect ( sqlite_uri , uri = True )
54
102
55
- web .app .logger .warning (f"connecting to { sqlite_uri } " )
56
- return sqlite3 .connect (sqlite_uri , uri = True )
103
+ db_ ._init_engine ("sqlite://" , creator = sqlite_connect , echo = trace )
57
104
58
- db_ ._init_engine ( "sqlite://" , creator = sqlite_connect , echo = trace )
105
+ db_ .database_init ( )
59
106
60
107
web .appdb = db_ .get_conn ()
61
108
62
109
yield db_
63
110
64
111
web .app .logger .warning ("closing db" )
112
+ if pgsql and not request .config .getoption ("--pgsql-no-drop-schema" ):
113
+ web .app .logger .critical ("DROPPING SCHEMA" )
114
+ db_ .query ("DROP SCHEMA sogs_tests CASCADE" )
65
115
web .appdb .close ()
66
116
67
117
0 commit comments