convert connect() into a context manager

This commit is contained in:
Sarah Hoffmann
2021-02-23 10:11:21 +01:00
parent 204fe20b4b
commit e520613362
12 changed files with 53 additions and 59 deletions

View File

@@ -85,9 +85,8 @@ def temp_db_with_extensions(temp_db):
def temp_db_conn(temp_db):
""" Connection to the test database.
"""
conn = connection.connect('dbname=' + temp_db)
yield conn
conn.close()
with connection.connect('dbname=' + temp_db) as conn:
yield conn
@pytest.fixture

View File

@@ -7,9 +7,8 @@ from nominatim.db.connection import connect
@pytest.fixture
def db(temp_db):
conn = connect('dbname=' + temp_db)
yield conn
conn.close()
with connect('dbname=' + temp_db) as conn:
yield conn
def test_connection_table_exists(db, temp_db_cursor):

View File

@@ -9,9 +9,8 @@ from nominatim.tools import admin
@pytest.fixture
def db(temp_db, placex_table):
conn = connect('dbname=' + temp_db)
yield conn
conn.close()
with connect('dbname=' + temp_db) as conn:
yield conn
def test_analyse_indexing_no_objects(db):
with pytest.raises(UsageError):

View File

@@ -10,6 +10,10 @@ def test_check_database_unknown_db(def_config, monkeypatch):
assert 1 == chkdb.check_database(def_config)
def test_check_database_fatal_test(def_config, temp_db):
assert 1 == chkdb.check_database(def_config)
def test_check_conection_good(temp_db_conn, def_config):
assert chkdb.check_connection(temp_db_conn, def_config) == chkdb.CheckState.OK

View File

@@ -11,9 +11,8 @@ SQL_DIR = (Path(__file__) / '..' / '..' / '..' / 'lib-sql').resolve()
@pytest.fixture
def db(temp_db):
conn = connect('dbname=' + temp_db)
yield conn
conn.close()
with connect('dbname=' + temp_db) as conn:
yield conn
@pytest.fixture
def db_with_tables(db):