Skip to content

Commit

Permalink
close cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
backmari committed Aug 30, 2024
1 parent 222d48e commit 450fb4d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
24 changes: 10 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,13 @@ def db_connection():
"""Fixture for database connection with config from env files"""
config = {**dotenv_values(".env"), **dotenv_values(".env.ci")}
assert config
conn = None
try:
conn = psycopg2.connect(
database=config["DATABASE_NAME"],
user=config["DATABASE_USER"],
password=config["DATABASE_PASS"],
port=config["DATABASE_PORT"],
host="localhost",
)
time.sleep(1)
yield conn
finally:
if conn is not None:
conn.close() # Close connection after tests have run
conn = psycopg2.connect(
database=config["DATABASE_NAME"],
user=config["DATABASE_USER"],
password=config["DATABASE_PASS"],
port=config["DATABASE_PORT"],
host="localhost",
)
time.sleep(1)
yield conn
conn.close()
11 changes: 9 additions & 2 deletions tests/utils/db_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def get_status_queue_id(db_connection, queue_name):
)
cursor.execute("SELECT id FROM report_statusqueue where name = %s;", (queue_name,))
queue_id = cursor.fetchone()
cursor.close()

return queue_id[0]

Expand All @@ -33,7 +34,9 @@ def check_run_status_exist(db_connection, run_id, queue_name):
cursor = db_connection.cursor()
queue_id = get_status_queue_id(db_connection, queue_name)
cursor.execute("SELECT * FROM report_runstatus WHERE run_id_id = %s AND queue_id_id = %s", (run_id, queue_id))
return cursor.fetchone() is not None
result = cursor.fetchone() is not None
cursor.close()
return result


def clear_previous_runstatus(db_connection, run_id):
Expand All @@ -57,6 +60,7 @@ def clear_previous_runstatus(db_connection, run_id):
)
cursor.execute("DELETE FROM report_runstatus WHERE run_id_id = %s;", run_id)
db_connection.commit()
cursor.close()


def add_instrument_data_run(conn, instrument, ipts, run_number, facility="SNS"):
Expand Down Expand Up @@ -117,6 +121,7 @@ def add_instrument_data_run(conn, instrument, ipts, run_number, facility="SNS"):
)
run_id = cursor.fetchone()
conn.commit()
cursor.close()

return run_id

Expand All @@ -136,4 +141,6 @@ def check_error_msg_contains(conn, run_id, error_msg):
"WHERE run_id_id = %s) AND description LIKE %s;",
(run_id, f"%{error_msg}%"), # surround err_msg by percentage signs for wildcard
)
return cursor.fetchone() is not None
result = cursor.fetchone() is not None
cursor.close()
return result

0 comments on commit 450fb4d

Please sign in to comment.