Skip to content

Commit d4bccaa

Browse files
authored
Merge pull request #1530 from lonvia/fix-connection-creation
Regression tests: fix usage of psycopg2.connect
2 parents f0f8b2d + 96b17db commit d4bccaa

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

tests/regression.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,18 @@ def setUpModule():
5050
logging.info("Setting up test database")
5151
dbname = CONFIG['test_database']
5252

53-
with psycopg2.connect("dbname='template1'") as conn:
53+
conn = psycopg2.connect("dbname='template1'")
54+
try:
5455
conn.autocommit = True
5556
with conn.cursor() as cur:
5657
cur.execute('DROP DATABASE IF EXISTS "{}"'.format(dbname))
5758
cur.execute("CREATE DATABASE \"{}\" WITH ENCODING 'UTF8'"
5859
.format(dbname))
60+
finally:
61+
conn.close()
5962

60-
with psycopg2.connect("dbname='{}'".format(dbname)) as conn:
63+
conn = psycopg2.connect("dbname='{}'".format(dbname))
64+
try:
6165
conn.autocommit = True
6266
with conn.cursor() as cur:
6367
# Check if there is a dataspace, we will skip tests otherwise.
@@ -68,17 +72,22 @@ def setUpModule():
6872

6973
cur.execute('CREATE EXTENSION postgis')
7074
cur.execute('CREATE EXTENSION hstore')
75+
finally:
76+
conn.close()
7177

7278
def tearDownModule():
7379
""" Destroy the global database.
7480
"""
7581
logging.info("Cleaning up test database")
7682

77-
with psycopg2.connect("dbname='template1'") as conn:
83+
conn = psycopg2.connect("dbname='template1'")
84+
try:
85+
conn.autocommit = True
7886
with conn.cursor() as cur:
79-
conn.autocommit = True
8087
cur.execute('DROP DATABASE IF EXISTS "{}"'
8188
.format(CONFIG['test_database']))
89+
finally:
90+
conn.close()
8291

8392
########################################################################
8493
#
@@ -111,13 +120,17 @@ def setUpClass(cls):
111120
cls.skipTest(None, "No Lua configured.")
112121
if 'tablespacetest' in cls.extra_params and not CONFIG['have_lua']:
113122
cls.skipTest(None, "Test tablespace 'tablespacetest' not configured.")
114-
with psycopg2.connect("dbname='{}'".format(CONFIG['test_database'])) as conn:
123+
conn = psycopg2.connect("dbname='{}'".format(CONFIG['test_database']))
124+
try:
115125
with conn.cursor() as cur:
116126
for t in ('nodes', 'ways', 'rels', 'point', 'line', 'roads', 'polygon'):
117127
cur.execute("DROP TABLE IF EXISTS planet_osm_" + t)
118128
cur.execute("DROP SCHEMA IF EXISTS osm CASCADE")
119129
if cls.schema:
120130
cur.execute("CREATE SCHEMA " + cls.schema)
131+
conn.commit()
132+
finally:
133+
conn.close()
121134

122135
if cls.import_file:
123136
cls.run_import(cls.get_def_params() + cls.extra_params,
@@ -134,8 +147,11 @@ def tearDownClass(cls):
134147
BaseRunner.conn.close()
135148
BaseRunner.conn = None
136149
if cls.schema:
137-
with psycopg2.connect("dbname='{}'".format(CONFIG['test_database'])) as conn:
150+
conn = psycopg2.connect("dbname='{}'".format(CONFIG['test_database']))
151+
try:
138152
conn.cursor().execute("DROP SCHEMA IF EXISTS {} CASCADE".format(cls.schema))
153+
finally:
154+
conn.close()
139155
try:
140156
os.remove('flat.nodes')
141157
except (FileNotFoundError):

0 commit comments

Comments
 (0)