Skip to content

Commit 7e60f36

Browse files
committed
bdd: make them work with psycopg3
1 parent 5b3f1c4 commit 7e60f36

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

tests/bdd/environment.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
from importlib.machinery import SourceFileLoader
1414

1515
from behave import *
16-
import psycopg2
17-
from psycopg2 import sql
16+
try:
17+
import psycopg2 as psycopg
18+
from psycopg2 import sql
19+
except ImportError:
20+
import psycopg
21+
from psycopg import sql
22+
1823

1924
from steps.geometry_factory import GeometryFactory
2025
from steps.replication_server_mock import ReplicationServerMock
@@ -44,14 +49,18 @@ def _connect_db(context, dbname):
4449
object as a context manager that automatically closes.
4550
Note that the connection does not commit automatically.
4651
"""
47-
return closing(psycopg2.connect(dbname=dbname))
52+
if psycopg.__version__.startswith('2'):
53+
conn = psycopg.connect(dbname=dbname)
54+
conn.autocommit = True
55+
return closing(conn)
56+
57+
return psycopg.connect(dbname=dbname, autocommit=True)
4858

4959

5060
def _drop_db(context, dbname, recreate_immediately=False):
5161
""" Drop the database with the given name if it exists.
5262
"""
5363
with _connect_db(context, 'postgres') as conn:
54-
conn.set_isolation_level(0)
5564
with conn.cursor() as cur:
5665
db = sql.Identifier(dbname)
5766
cur.execute(sql.SQL('DROP DATABASE IF EXISTS {}').format(db))
@@ -136,7 +145,6 @@ def test_db(context, **kwargs):
136145
_drop_db(context, dbname, recreate_immediately=True)
137146

138147
with _connect_db(context, dbname) as conn:
139-
conn.autocommit = True
140148

141149
with conn.cursor() as cur:
142150
cur.execute('CREATE EXTENSION postgis')

tests/bdd/steps/steps_db.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import re
1212
from typing import Iterable
1313

14-
from psycopg2 import sql
14+
try:
15+
from psycopg2 import sql
16+
except ImportError:
17+
from psycopg import sql
18+
1519

1620
@given("the database schema (?P<schema>.+)")
1721
def create_db_schema(context, schema):

0 commit comments

Comments
 (0)