From a61f30b2d26af134c89e040703d94239ea9a0bf6 Mon Sep 17 00:00:00 2001 From: Rafi Shamim Date: Tue, 7 Jul 2020 18:48:44 -0400 Subject: Handle failure in setup of IsolationLevelsTestCase If the CREATE TABLE statement fails, the setup would fail without committing or rolling back the active transaction, so the transaction would hold onto its resources indefinitely. Normally, the transaction would be closed when the connection is closed in the `tearDown` function. However, `tearDown` is not called if there was an error during `setUp` ([as specified by the `unittest` docs](https://docs.python.org/3/library/unittest.html#unittest.TestCase.tearDown)), so we need to handle this case specially. --- tests/test_connection.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/test_connection.py b/tests/test_connection.py index 8236801..af2c3c4 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -567,9 +567,11 @@ class IsolationLevelsTestCase(ConnectingTestCase): cur.execute("drop table isolevel;") except psycopg2.ProgrammingError: conn.rollback() - cur.execute("create table isolevel (id integer);") - conn.commit() - conn.close() + try: + cur.execute("create table isolevel (id integer);") + conn.commit() + finally: + conn.close() def test_isolation_level(self): conn = self.connect() -- cgit v1.2.1