From 10bdf7abf1ff2d8716f1ee033ef5591b626394f4 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 13 Nov 2021 15:56:43 +0100 Subject: [PATCH] Close all the resources during tests This allows to run the test suite with -Werror. --- tests/dbapi20.py | 2 ++ tests/pool/test_pool.py | 2 ++ tests/pool/test_pool_async.py | 2 ++ tests/test_adapt.py | 2 ++ tests/test_concurrency.py | 4 ++++ tests/test_concurrency_async.py | 3 +++ tests/test_connection.py | 10 +++++++++- tests/test_connection_async.py | 10 +++++++++- tests/test_conninfo.py | 1 + tests/test_module.py | 3 ++- tests/test_psycopg_dbapi20.py | 3 ++- tests/test_server_cursor.py | 18 ++++++++++++++++++ tests/test_server_cursor_async.py | 18 ++++++++++++++++++ tests/types/test_hstore.py | 1 + 14 files changed, 75 insertions(+), 4 deletions(-) diff --git a/tests/dbapi20.py b/tests/dbapi20.py index b47cc05e5..c873a4e66 100644 --- a/tests/dbapi20.py +++ b/tests/dbapi20.py @@ -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() diff --git a/tests/pool/test_pool.py b/tests/pool/test_pool.py index 703bf70e0..c0779dbb3 100644 --- a/tests/pool/test_pool.py +++ b/tests/pool/test_pool.py @@ -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: diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index 608c35e0c..96c39ed90 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -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: diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 499e8ea4e..863ceef59 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -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): diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index 37e917606..625e89d5a 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -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() diff --git a/tests/test_concurrency_async.py b/tests/test_concurrency_async.py index f535d28ac..3192a7abe 100644 --- a/tests/test_concurrency_async.py +++ b/tests/test_concurrency_async.py @@ -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() diff --git a/tests/test_connection.py b/tests/test_connection.py index e64832f53..fed04d7ef 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -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() diff --git a/tests/test_connection_async.py b/tests/test_connection_async.py index 67c297a19..e940a89ae 100644 --- a/tests/test_connection_async.py +++ b/tests/test_connection_async.py @@ -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") diff --git a/tests/test_conninfo.py b/tests/test_conninfo.py index 834e6fb47..4d56b1fe8 100644 --- a/tests/test_conninfo.py +++ b/tests/test_conninfo.py @@ -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() diff --git a/tests/test_module.py b/tests/test_module.py index 16740d8c2..2df4d4705 100644 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -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): diff --git a/tests/test_psycopg_dbapi20.py b/tests/test_psycopg_dbapi20.py index d3da1304c..3d284bcd8 100644 --- a/tests/test_psycopg_dbapi20.py +++ b/tests/test_psycopg_dbapi20.py @@ -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( diff --git a/tests/test_server_cursor.py b/tests/test_server_cursor.py index 196c703f2..d4b62ed03 100644 --- a/tests/test_server_cursor.py +++ b/tests/test_server_cursor.py @@ -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): diff --git a/tests/test_server_cursor_async.py b/tests/test_server_cursor_async.py index 6da867351..417c4adb6 100644 --- a/tests/test_server_cursor_async.py +++ b/tests/test_server_cursor_async.py @@ -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): diff --git a/tests/types/test_hstore.py b/tests/types/test_hstore.py index c465af33f..ac9d87c24 100644 --- a/tests/types/test_hstore.py +++ b/tests/types/test_hstore.py @@ -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))) -- 2.47.2