diff --git a/tests/dbapi20.py b/tests/dbapi20.py index fe89bb0e..5209580f 100644 --- a/tests/dbapi20.py +++ b/tests/dbapi20.py @@ -232,6 +232,7 @@ class DatabaseAPI20Test(unittest.TestCase): self.failUnless(con.InternalError is drv.InternalError) self.failUnless(con.ProgrammingError is drv.ProgrammingError) self.failUnless(con.NotSupportedError is drv.NotSupportedError) + con.close() def test_commit(self): @@ -251,6 +252,7 @@ class DatabaseAPI20Test(unittest.TestCase): con.rollback() except self.driver.NotSupportedError: pass + con.close() def test_cursor(self): con = self._connect() diff --git a/tests/dbapi20_tpc.py b/tests/dbapi20_tpc.py index d4790f71..c6c87b24 100644 --- a/tests/dbapi20_tpc.py +++ b/tests/dbapi20_tpc.py @@ -24,19 +24,22 @@ class TwoPhaseCommitTests(unittest.TestCase): def test_xid(self): con = self.connect() try: - xid = con.xid(42, "global", "bqual") - except self.driver.NotSupportedError: - self.fail("Driver does not support transaction IDs.") + try: + xid = con.xid(42, "global", "bqual") + except self.driver.NotSupportedError: + self.fail("Driver does not support transaction IDs.") - self.assertEquals(xid[0], 42) - self.assertEquals(xid[1], "global") - self.assertEquals(xid[2], "bqual") + self.assertEquals(xid[0], 42) + self.assertEquals(xid[1], "global") + self.assertEquals(xid[2], "bqual") - # Try some extremes for the transaction ID: - xid = con.xid(0, "", "") - self.assertEquals(tuple(xid), (0, "", "")) - xid = con.xid(0x7fffffff, "a" * 64, "b" * 64) - self.assertEquals(tuple(xid), (0x7fffffff, "a" * 64, "b" * 64)) + # Try some extremes for the transaction ID: + xid = con.xid(0, "", "") + self.assertEquals(tuple(xid), (0, "", "")) + xid = con.xid(0x7fffffff, "a" * 64, "b" * 64) + self.assertEquals(tuple(xid), (0x7fffffff, "a" * 64, "b" * 64)) + finally: + con.close() def test_tpc_begin(self): con = self.connect() diff --git a/tests/test_async.py b/tests/test_async.py index d62eb3b0..8624be4a 100755 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -451,14 +451,16 @@ class AsyncTests(ConnectingTestCase): self.assertEqual(cur.fetchone(), (42,)) def test_async_connection_error_message(self): + cnn = psycopg2.connect('dbname=thisdatabasedoesntexist', async_=True) try: - cnn = psycopg2.connect('dbname=thisdatabasedoesntexist', async_=True) self.wait(cnn) except psycopg2.Error as e: self.assertNotEqual(str(e), "asynchronous connection failed", "connection error reason lost") else: self.fail("no exception raised") + finally: + cnn.close() @skip_before_postgres(8, 2) def test_copy_no_hang(self): diff --git a/tests/test_async_keyword.py b/tests/test_async_keyword.py index e1126928..f8e50afe 100755 --- a/tests/test_async_keyword.py +++ b/tests/test_async_keyword.py @@ -89,6 +89,8 @@ class AsyncTests(ConnectingTestCase): "connection error reason lost") else: self.fail("no exception raised") + finally: + cnn.close() class CancelTests(ConnectingTestCase): @@ -118,6 +120,7 @@ class CancelTests(ConnectingTestCase): cur.execute("select 1") extras.wait_select(async_conn) self.assertEqual(cur.fetchall(), [(1, )]) + async_conn.close() def test_async_connection_cancel(self): async_conn = psycopg2.connect(dsn, async=True) diff --git a/tests/test_cancel.py b/tests/test_cancel.py index 4c60c0b7..06477edc 100755 --- a/tests/test_cancel.py +++ b/tests/test_cancel.py @@ -105,6 +105,7 @@ class CancelTests(ConnectingTestCase): cur.execute("select 1") extras.wait_select(async_conn) self.assertEqual(cur.fetchall(), [(1, )]) + async_conn.close() def test_async_connection_cancel(self): async_conn = psycopg2.connect(dsn, async_=True) diff --git a/tests/testutils.py b/tests/testutils.py index 26f6cc71..cd69c468 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -248,15 +248,17 @@ def skip_if_tpc_disabled(f): @wraps(f) def skip_if_tpc_disabled_(self): cnn = self.connect() - cur = cnn.cursor() try: - cur.execute("SHOW max_prepared_transactions;") - except psycopg2.ProgrammingError: - return self.skipTest( - "server too old: two phase transactions not supported.") - else: - mtp = int(cur.fetchone()[0]) - cnn.close() + cur = cnn.cursor() + try: + cur.execute("SHOW max_prepared_transactions;") + except psycopg2.ProgrammingError: + return self.skipTest( + "server too old: two phase transactions not supported.") + else: + mtp = int(cur.fetchone()[0]) + finally: + cnn.close() if not mtp: return self.skipTest(