]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Close all the resources during tests
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 13 Nov 2021 14:56:43 +0000 (15:56 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 13 Nov 2021 16:34:51 +0000 (17:34 +0100)
This allows to run the test suite with -Werror.

14 files changed:
tests/dbapi20.py
tests/pool/test_pool.py
tests/pool/test_pool_async.py
tests/test_adapt.py
tests/test_concurrency.py
tests/test_concurrency_async.py
tests/test_connection.py
tests/test_connection_async.py
tests/test_conninfo.py
tests/test_module.py
tests/test_psycopg_dbapi20.py
tests/test_server_cursor.py
tests/test_server_cursor_async.py
tests/types/test_hstore.py

index b47cc05e584dbdce0b95c6366f92609ec8168b22..c873a4e66b63b385e138000a59609207e07a852b 100644 (file)
@@ -235,6 +235,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):
@@ -254,6 +255,7 @@ class DatabaseAPI20Test(unittest.TestCase):
                 con.rollback()
             except self.driver.NotSupportedError:
                 pass
+        con.close()
 
     def test_cursor(self):
         con = self._connect()
index 703bf70e0254b5f556c1393d38e9753e976b683e..c0779dbb33b122f79d92c62be8470c3cc0b82146 100644 (file)
@@ -577,6 +577,8 @@ def test_putconn_no_pool(dsn):
         with pytest.raises(ValueError):
             p.putconn(conn)
 
+    conn.close()
+
 
 def test_putconn_wrong_pool(dsn):
     with pool.ConnectionPool(dsn, min_size=1) as p1:
index 608c35e0cd26799e27532ff03421f5e769d3d3ce..96c39ed90cdc7db6e4f0a14a4166f42137a72e22 100644 (file)
@@ -592,6 +592,8 @@ async def test_putconn_no_pool(dsn):
         with pytest.raises(ValueError):
             await p.putconn(conn)
 
+    await conn.close()
+
 
 async def test_putconn_wrong_pool(dsn):
     async with pool.AsyncConnectionPool(dsn, min_size=1) as p1:
index 499e8ea4eaf6e6dcd95420b5541c1f49039f9c7b..863ceef59a8d4662e150b077b73d90e0571f082b 100644 (file)
@@ -73,6 +73,7 @@ def test_dump_global_ctx(dsn, global_adapters):
     assert cur.fetchone() == ("hellogb",)
     cur = conn.execute("select %t", [MyStr("hello")])
     assert cur.fetchone() == ("hellogt",)
+    conn.close()
 
 
 def test_dump_connection_ctx(conn):
@@ -209,6 +210,7 @@ def test_load_global_ctx(dsn, global_adapters):
     assert cur.fetchone() == ("hellogt",)
     cur = conn.cursor(binary=True).execute("select 'hello'::text")
     assert cur.fetchone() == ("hellogb",)
+    conn.close()
 
 
 def test_load_connection_ctx(conn):
index 37e917606149f00aceb377fc9bcb2b5a326298f5..625e89d5a70cbab734fd2a473638493f91bbaab7 100644 (file)
@@ -117,6 +117,7 @@ def test_notifies(conn, dsn):
         nconn.cursor().execute("notify foo, '1'")
         time.sleep(0.25)
         nconn.cursor().execute("notify foo, '2'")
+        nconn.close()
 
     conn.autocommit = True
     conn.cursor().execute("listen foo")
@@ -202,3 +203,6 @@ def test_identify_closure(dsn, retries):
                 conn.execute("select 1")
             t1 = time.time()
             assert 0.3 < t1 - t0 < 0.6
+
+            conn.close()
+            conn2.close()
index f535d28ac3cb6946be363e8c24ab36b073f700bc..3192a7abe731ae17b52078f637e047f59760ba08 100644 (file)
@@ -158,3 +158,6 @@ async def test_identify_closure(dsn, retries):
                 await aconn.execute("select 1")
             t1 = time.time()
             assert 0.3 < t1 - t0 < 0.6
+
+            await aconn.close()
+            await conn2.close()
index e64832f530a0ac47c420d1a4726e03114b65c512..fed04d7ef0d5a36b9849d215e73fa7925f5b08e8 100644 (file)
@@ -27,6 +27,7 @@ def test_connect(dsn):
     conn = Connection.connect(dsn)
     assert not conn.closed
     assert conn.pgconn.status == conn.ConnStatus.OK
+    conn.close()
 
 
 def test_connect_str_subclass(dsn):
@@ -36,6 +37,7 @@ def test_connect_str_subclass(dsn):
     conn = Connection.connect(MyString(dsn))
     assert not conn.closed
     assert conn.pgconn.status == conn.ConnStatus.OK
+    conn.close()
 
 
 def test_connect_bad():
@@ -281,6 +283,7 @@ def test_autocommit(conn):
 def test_autocommit_connect(dsn):
     conn = Connection.connect(dsn, autocommit=True)
     assert conn.autocommit
+    conn.close()
 
 
 def test_autocommit_intrans(conn):
@@ -335,8 +338,9 @@ def test_connect_args(monkeypatch, pgconn, args, kwargs, want):
         yield
 
     monkeypatch.setattr(psycopg.connection, "connect", fake_connect)
-    psycopg.Connection.connect(*args, **kwargs)
+    conn = psycopg.Connection.connect(*args, **kwargs)
     assert conninfo_to_dict(the_conninfo) == conninfo_to_dict(want)
+    conn.close()
 
 
 @pytest.mark.parametrize(
@@ -472,6 +476,7 @@ def test_execute_binary(conn):
 def test_row_factory(dsn):
     defaultconn = Connection.connect(dsn)
     assert defaultconn.row_factory is tuple_row  # type: ignore[comparison-overlap]
+    defaultconn.close()
 
     conn = Connection.connect(dsn, row_factory=my_row_factory)
     assert conn.row_factory is my_row_factory  # type: ignore[comparison-overlap]
@@ -492,6 +497,7 @@ def test_row_factory(dsn):
     cur3 = conn.execute("select 'vale'")
     r = cur3.fetchone()
     assert r and r == ("vale",)  # type: ignore[comparison-overlap]
+    conn.close()
 
 
 def test_str(conn):
@@ -711,6 +717,7 @@ def test_connect_context(dsn):
     assert cur.fetchone()[0] == "hellot"  # type: ignore[index]
     cur = conn.execute("select %b", ["hello"])
     assert cur.fetchone()[0] == "hellob"  # type: ignore[index]
+    conn.close()
 
 
 def test_connect_context_copy(dsn, conn):
@@ -723,3 +730,4 @@ def test_connect_context_copy(dsn, conn):
     assert cur.fetchone()[0] == "hellot"  # type: ignore[index]
     cur = conn2.execute("select %b", ["hello"])
     assert cur.fetchone()[0] == "hellob"  # type: ignore[index]
+    conn2.close()
index 67c297a19645f2b0ab2e79803133b1db98437354..e940a89ae73f58e7a28603ddf8101a218274de34 100644 (file)
@@ -24,6 +24,7 @@ async def test_connect(dsn):
     conn = await AsyncConnection.connect(dsn)
     assert not conn.closed
     assert conn.pgconn.status == conn.ConnStatus.OK
+    await conn.close()
 
 
 async def test_connect_bad():
@@ -38,6 +39,7 @@ async def test_connect_str_subclass(dsn):
     conn = await AsyncConnection.connect(MyString(dsn))
     assert not conn.closed
     assert conn.pgconn.status == conn.ConnStatus.OK
+    await conn.close()
 
 
 @pytest.mark.slow
@@ -285,6 +287,7 @@ async def test_autocommit(aconn):
 async def test_autocommit_connect(dsn):
     aconn = await psycopg.AsyncConnection.connect(dsn, autocommit=True)
     assert aconn.autocommit
+    await aconn.close()
 
 
 async def test_autocommit_intrans(aconn):
@@ -340,8 +343,9 @@ async def test_connect_args(monkeypatch, pgconn, args, kwargs, want):
         yield
 
     monkeypatch.setattr(psycopg.connection, "connect", fake_connect)
-    await psycopg.AsyncConnection.connect(*args, **kwargs)
+    conn = await psycopg.AsyncConnection.connect(*args, **kwargs)
     assert conninfo_to_dict(the_conninfo) == conninfo_to_dict(want)
+    await conn.close()
 
 
 @pytest.mark.parametrize(
@@ -476,6 +480,7 @@ async def test_execute_binary(aconn):
 async def test_row_factory(dsn):
     defaultconn = await AsyncConnection.connect(dsn)
     assert defaultconn.row_factory is tuple_row  # type: ignore[comparison-overlap]
+    await defaultconn.close()
 
     conn = await AsyncConnection.connect(dsn, row_factory=my_row_factory)
     assert conn.row_factory is my_row_factory  # type: ignore[comparison-overlap]
@@ -496,6 +501,7 @@ async def test_row_factory(dsn):
     cur3 = await conn.execute("select 'vale'")
     r = await cur3.fetchone()
     assert r and r == ("vale",)  # type: ignore[comparison-overlap]
+    await conn.close()
 
 
 async def test_str(aconn):
@@ -661,6 +667,7 @@ async def test_connect_context_adapters(dsn):
     assert (await cur.fetchone())[0] == "hellot"  # type: ignore[index]
     cur = await conn.execute("select %b", ["hello"])
     assert (await cur.fetchone())[0] == "hellob"  # type: ignore[index]
+    await conn.close()
 
 
 async def test_connect_context_copy(dsn, aconn):
@@ -673,6 +680,7 @@ async def test_connect_context_copy(dsn, aconn):
     assert (await cur.fetchone())[0] == "hellot"  # type: ignore[index]
     cur = await aconn2.execute("select %b", ["hello"])
     assert (await cur.fetchone())[0] == "hellob"  # type: ignore[index]
+    await aconn2.close()
 
 
 @pytest.mark.skipif(sys.platform != "win32", reason="windows only test")
index 834e6fb470cc01006e0c44be2aab87ae55a77a37..4d56b1fe8281885aaf35bca21b6a7d08e3f1f96b 100644 (file)
@@ -284,6 +284,7 @@ class TestConnectionInfo:
         conn = psycopg.connect(dsn)
         assert conn.info.parameter_status("client_encoding") == out
         assert conn.info.encoding == codec
+        conn.close()
 
     def test_set_encoding_unsupported(self, conn):
         cur = conn.cursor()
index 16740d8c298ba6ce6c5313a2a1845615d55673fd..2df4d4705837320997cf0c0a2d8ee9ac6ee03ca7 100644 (file)
@@ -28,8 +28,9 @@ def test_connect(monkeypatch, dsn, args, kwargs, want_conninfo):
 
     monkeypatch.setattr(psycopg.connection, "connect", mock_connect)
 
-    psycopg.connect(*args, **kwargs)
+    conn = psycopg.connect(*args, **kwargs)
     assert got_conninfo == want_conninfo
+    conn.close()
 
 
 def test_version(mypy):
index d3da1304c6e9118fb4c98c05cbd5bc38ace58339..3d284bcd80b22cbc2a92ebe7b031de603311501b 100644 (file)
@@ -130,8 +130,9 @@ def test_connect_args(monkeypatch, pgconn, args, kwargs, want):
         yield
 
     monkeypatch.setattr(psycopg.connection, "connect", fake_connect)
-    psycopg.connect(*args, **kwargs)
+    conn = psycopg.connect(*args, **kwargs)
     assert conninfo_to_dict(the_conninfo) == conninfo_to_dict(want)
+    conn.close()
 
 
 @pytest.mark.parametrize(
index 196c703f2acefebf8e011561fd3785e8bcbbe684..d4b62ed0307f086e48e45fb2ec7b8af8b21f1119 100644 (file)
@@ -10,17 +10,20 @@ def test_funny_name(conn):
     cur.execute("select generate_series(1, 3) as bar")
     assert cur.fetchall() == [(1,), (2,), (3,)]
     assert cur.name == "1-2-3"
+    cur.close()
 
 
 def test_repr(conn):
     cur = conn.cursor("my-name")
     assert "ServerCursor" in repr(cur)
     assert "my-name" in repr(cur)
+    cur.close()
 
 
 def test_connection(conn):
     cur = conn.cursor("foo")
     assert cur.connection is conn
+    cur.close()
 
 
 def test_description(conn):
@@ -31,14 +34,17 @@ def test_description(conn):
     assert cur.description[0].name == "bar"
     assert cur.description[0].type_code == cur.adapters.types["int4"].oid
     assert cur.pgresult.ntuples == 0
+    cur.close()
 
 
 def test_format(conn):
     cur = conn.cursor("foo")
     assert cur.format == Format.TEXT
+    cur.close()
 
     cur = conn.cursor("foo", binary=True)
     assert cur.format == Format.BINARY
+    cur.close()
 
 
 def test_query_params(conn):
@@ -60,6 +66,7 @@ def test_binary_cursor_execute(conn):
     assert cur.fetchone() == (2,)
     assert cur.pgresult.fformat(0) == 1
     assert cur.pgresult.get_value(0, 0) == b"\x00\x00\x00\x02"
+    cur.close()
 
 
 def test_execute_binary(conn):
@@ -76,6 +83,7 @@ def test_execute_binary(conn):
     assert cur.fetchone() == (1,)
     assert cur.pgresult.fformat(0) == 0
     assert cur.pgresult.get_value(0, 0) == b"1"
+    cur.close()
 
 
 def test_binary_cursor_text_override(conn):
@@ -92,6 +100,7 @@ def test_binary_cursor_text_override(conn):
     assert cur.fetchone() == (1,)
     assert cur.pgresult.fformat(0) == 1
     assert cur.pgresult.get_value(0, 0) == b"\x00\x00\x00\x01"
+    cur.close()
 
 
 def test_close(conn, recwarn, retries):
@@ -245,12 +254,14 @@ def test_execute_error(conn, stmt):
     cur = conn.cursor("foo")
     with pytest.raises(e.ProgrammingError):
         cur.execute(stmt)
+    cur.close()
 
 
 def test_executemany(conn):
     cur = conn.cursor("foo")
     with pytest.raises(e.NotSupportedError):
         cur.executemany("select %s", [(1,), (2,)])
+    cur.close()
 
 
 def test_fetchone(conn):
@@ -318,6 +329,7 @@ def test_row_factory(conn):
     cur.scroll(0, "absolute")
     cur.row_factory = dict_row
     assert cur.fetchone() == {"x": 1}
+    cur.close()
 
 
 def test_rownumber(conn):
@@ -335,6 +347,7 @@ def test_rownumber(conn):
     assert cur.rownumber == 12
     cur.fetchall()
     assert cur.rownumber == 42
+    cur.close()
 
 
 def test_iter(conn):
@@ -376,6 +389,7 @@ def test_cant_scroll_by_default(conn):
     assert cur.scrollable is None
     with pytest.raises(e.ProgrammingError):
         cur.scroll(0)
+    cur.close()
 
 
 def test_scroll(conn):
@@ -392,6 +406,7 @@ def test_scroll(conn):
 
     with pytest.raises(ValueError):
         cur.scroll(9, mode="wat")
+    cur.close()
 
 
 def test_scrollable(conn):
@@ -403,6 +418,7 @@ def test_scrollable(conn):
         curs.scroll(-1)
         assert i == curs.fetchone()[0]
         curs.scroll(-1)
+    curs.close()
 
 
 def test_non_scrollable(conn):
@@ -412,6 +428,7 @@ def test_non_scrollable(conn):
     curs.scroll(5)
     with pytest.raises(e.OperationalError):
         curs.scroll(-1)
+    curs.close()
 
 
 @pytest.mark.parametrize("kwargs", [{}, {"withhold": False}])
@@ -443,6 +460,7 @@ def test_steal_cursor(conn):
     assert cur2.fetchone() == (1,)
     assert cur2.fetchmany(3) == [(2,), (3,), (4,)]
     assert cur2.fetchall() == [(5,), (6,)]
+    cur2.close()
 
 
 def test_stolen_cursor_close(conn):
index 6da86735163df537e0b6aa2ecc75e78d0c4a2362..417c4adb6ae08be543bf1dff3424f8bf2f9c72b7 100644 (file)
@@ -12,17 +12,20 @@ async def test_funny_name(aconn):
     await cur.execute("select generate_series(1, 3) as bar")
     assert await cur.fetchall() == [(1,), (2,), (3,)]
     assert cur.name == "1-2-3"
+    await cur.close()
 
 
 async def test_repr(aconn):
     cur = aconn.cursor("my-name")
     assert "AsyncServerCursor" in repr(cur)
     assert "my-name" in repr(cur)
+    await cur.close()
 
 
 async def test_connection(aconn):
     cur = aconn.cursor("foo")
     assert cur.connection is aconn
+    await cur.close()
 
 
 async def test_description(aconn):
@@ -33,14 +36,17 @@ async def test_description(aconn):
     assert cur.description[0].name == "bar"
     assert cur.description[0].type_code == cur.adapters.types["int4"].oid
     assert cur.pgresult.ntuples == 0
+    await cur.close()
 
 
 async def test_format(aconn):
     cur = aconn.cursor("foo")
     assert cur.format == Format.TEXT
+    await cur.close()
 
     cur = aconn.cursor("foo", binary=True)
     assert cur.format == Format.BINARY
+    await cur.close()
 
 
 async def test_query_params(aconn):
@@ -62,6 +68,7 @@ async def test_binary_cursor_execute(aconn):
     assert (await cur.fetchone()) == (2,)
     assert cur.pgresult.fformat(0) == 1
     assert cur.pgresult.get_value(0, 0) == b"\x00\x00\x00\x02"
+    await cur.close()
 
 
 async def test_execute_binary(aconn):
@@ -78,6 +85,7 @@ async def test_execute_binary(aconn):
     assert (await cur.fetchone()) == (1,)
     assert cur.pgresult.fformat(0) == 0
     assert cur.pgresult.get_value(0, 0) == b"1"
+    await cur.close()
 
 
 async def test_binary_cursor_text_override(aconn):
@@ -94,6 +102,7 @@ async def test_binary_cursor_text_override(aconn):
     assert (await cur.fetchone()) == (1,)
     assert cur.pgresult.fformat(0) == 1
     assert cur.pgresult.get_value(0, 0) == b"\x00\x00\x00\x01"
+    await cur.close()
 
 
 async def test_close(aconn, recwarn, retries):
@@ -254,12 +263,14 @@ async def test_execute_error(aconn, stmt):
     cur = aconn.cursor("foo")
     with pytest.raises(e.ProgrammingError):
         await cur.execute(stmt)
+    await cur.close()
 
 
 async def test_executemany(aconn):
     cur = aconn.cursor("foo")
     with pytest.raises(e.NotSupportedError):
         await cur.executemany("select %s", [(1,), (2,)])
+    await cur.close()
 
 
 async def test_fetchone(aconn):
@@ -329,6 +340,7 @@ async def test_row_factory(aconn):
     await cur.scroll(0, "absolute")
     cur.row_factory = dict_row
     assert await cur.fetchone() == {"x": 1}
+    await cur.close()
 
 
 async def test_rownumber(aconn):
@@ -346,6 +358,7 @@ async def test_rownumber(aconn):
     assert cur.rownumber == 12
     await cur.fetchall()
     assert cur.rownumber == 42
+    await cur.close()
 
 
 async def test_iter(aconn):
@@ -392,6 +405,7 @@ async def test_cant_scroll_by_default(aconn):
     assert cur.scrollable is None
     with pytest.raises(e.ProgrammingError):
         await cur.scroll(0)
+    await cur.close()
 
 
 async def test_scroll(aconn):
@@ -408,6 +422,7 @@ async def test_scroll(aconn):
 
     with pytest.raises(ValueError):
         await cur.scroll(9, mode="wat")
+    await cur.close()
 
 
 async def test_scrollable(aconn):
@@ -419,6 +434,7 @@ async def test_scrollable(aconn):
         await curs.scroll(-1)
         assert i == (await curs.fetchone())[0]
         await curs.scroll(-1)
+    await curs.close()
 
 
 async def test_non_scrollable(aconn):
@@ -428,6 +444,7 @@ async def test_non_scrollable(aconn):
     await curs.scroll(5)
     with pytest.raises(e.OperationalError):
         await curs.scroll(-1)
+    await curs.close()
 
 
 @pytest.mark.parametrize("kwargs", [{}, {"withhold": False}])
@@ -461,6 +478,7 @@ async def test_steal_cursor(aconn):
     assert await cur2.fetchone() == (1,)
     assert await cur2.fetchmany(3) == [(2,), (3,), (4,)]
     assert await cur2.fetchall() == [(5,), (6,)]
+    await cur2.close()
 
 
 async def test_stolen_cursor_close(aconn):
index c465af33f2da38ea64cff5237e61809623eaa918..ac9d87c2466df783b08e3dcef27ae20aacc93969 100644 (file)
@@ -75,6 +75,7 @@ def test_register_globally(hstore, dsn, svcconn, global_adapters):
 
     cur = conn.execute("select null::hstore, ''::hstore, 'a => b'::hstore")
     assert cur.fetchone() == (None, {}, {"a": "b"})
+    conn.close()
 
 
 ab = list(map(chr, range(32, 128)))