From: Daniele Varrazzo Date: Tue, 15 Aug 2023 18:05:58 +0000 (+0100) Subject: refactor(tests): auto-generate sync version of cursor subclasses X-Git-Tag: pool-3.2.0~12^2~53 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=da59cf46af22cf1e3744655e0f1119e3219a6467;p=thirdparty%2Fpsycopg.git refactor(tests): auto-generate sync version of cursor subclasses --- diff --git a/tests/test_client_cursor.py b/tests/test_client_cursor.py index ee6a69ee7..5d56b3ebf 100644 --- a/tests/test_client_cursor.py +++ b/tests/test_client_cursor.py @@ -22,6 +22,11 @@ def test_default_cursor(conn): assert type(cur) is psycopg.ClientCursor +def test_str(conn): + cur = conn.cursor() + assert "psycopg.%s" % psycopg.ClientCursor.__name__ in str(cur) + + def test_from_cursor_factory(conn_cls, dsn): with conn_cls.connect(dsn, cursor_factory=psycopg.ClientCursor) as conn: cur = conn.cursor() diff --git a/tests/test_client_cursor_async.py b/tests/test_client_cursor_async.py index 43505cb4c..d1abbe849 100644 --- a/tests/test_client_cursor_async.py +++ b/tests/test_client_cursor_async.py @@ -19,6 +19,11 @@ async def test_default_cursor(aconn): assert type(cur) is psycopg.AsyncClientCursor +async def test_str(aconn): + cur = aconn.cursor() + assert "psycopg.%s" % psycopg.AsyncClientCursor.__name__ in str(cur) + + async def test_from_cursor_factory(aconn_cls, dsn): async with await aconn_cls.connect( dsn, cursor_factory=psycopg.AsyncClientCursor diff --git a/tests/test_default_cursor.py b/tests/test_default_cursor.py index 92be4461b..9cd230659 100644 --- a/tests/test_default_cursor.py +++ b/tests/test_default_cursor.py @@ -1,3 +1,6 @@ +# WARNING: this file is auto-generated by 'async_to_sync.py' +# from the original file 'test_default_cursor_async.py' +# DO NOT CHANGE! Change the original file instead. """ Tests for psycopg.Cursor that are not supposed to pass for subclasses. """ @@ -23,7 +26,7 @@ def test_from_cursor_factory(conn_cls, dsn): def test_str(conn): cur = conn.cursor() - assert "psycopg.Cursor" in str(cur) + assert "psycopg.%s" % psycopg.Cursor.__name__ in str(cur) def test_execute_many_results_param(conn): @@ -103,4 +106,5 @@ def test_leak(conn_cls, dsn, faker, fmt, fmt_out, fetch, row_factory): work() gc_collect() n.append(gc_count()) + assert n[0] == n[1] == n[2], f"objects leaked: {n[1] - n[0]}, {n[2] - n[1]}" diff --git a/tests/test_default_cursor_async.py b/tests/test_default_cursor_async.py index 64c5bfad5..1ebc827cd 100644 --- a/tests/test_default_cursor_async.py +++ b/tests/test_default_cursor_async.py @@ -25,7 +25,7 @@ async def test_from_cursor_factory(aconn_cls, dsn): async def test_str(aconn): cur = aconn.cursor() - assert "psycopg.AsyncCursor" in str(cur) + assert "psycopg.%s" % psycopg.AsyncCursor.__name__ in str(cur) async def test_execute_many_results_param(aconn): diff --git a/tests/test_raw_cursor.py b/tests/test_raw_cursor.py index fd6fe9bc5..887cdc164 100644 --- a/tests/test_raw_cursor.py +++ b/tests/test_raw_cursor.py @@ -1,3 +1,6 @@ +# WARNING: this file is auto-generated by 'async_to_sync.py' +# from the original file 'test_raw_cursor_async.py' +# DO NOT CHANGE! Change the original file instead. import pytest import psycopg from psycopg import pq, rows, errors as e @@ -8,7 +11,7 @@ from .utils import gc_collect, gc_count @pytest.fixture -def conn(conn): +def conn(conn, anyio_backend): conn.cursor_factory = psycopg.RawCursor return conn @@ -20,7 +23,7 @@ def test_default_cursor(conn): def test_str(conn): cur = conn.cursor() - assert "psycopg.RawCursor" in str(cur) + assert "psycopg.%s" % psycopg.RawCursor.__name__ in str(cur) def test_sequence_only(conn): @@ -79,29 +82,30 @@ def test_leak(conn_cls, dsn, faker, fmt, fmt_out, fetch, row_factory): row_factory = getattr(rows, row_factory) def work(): - with conn_cls.connect(dsn) as conn, conn.transaction(force_rollback=True): - with conn.cursor(binary=fmt_out, row_factory=row_factory) as cur: - cur.execute(faker.drop_stmt) - cur.execute(faker.create_stmt) - with faker.find_insert_problem(conn): - cur.executemany(faker.insert_stmt, faker.records) - cur.execute(ph(cur, faker.select_stmt)) - - if fetch == "one": - while True: - tmp = cur.fetchone() - if tmp is None: - break - elif fetch == "many": - while True: - tmp = cur.fetchmany(3) - if not tmp: - break - elif fetch == "all": - cur.fetchall() - elif fetch == "iter": - for rec in cur: - pass + with conn_cls.connect(dsn) as conn: + with conn.transaction(force_rollback=True): + with conn.cursor(binary=fmt_out, row_factory=row_factory) as cur: + cur.execute(faker.drop_stmt) + cur.execute(faker.create_stmt) + with faker.find_insert_problem(conn): + cur.executemany(faker.insert_stmt, faker.records) + cur.execute(ph(cur, faker.select_stmt)) + + if fetch == "one": + while True: + tmp = cur.fetchone() + if tmp is None: + break + elif fetch == "many": + while True: + tmp = cur.fetchmany(3) + if not tmp: + break + elif fetch == "all": + cur.fetchall() + elif fetch == "iter": + for rec in cur: + pass n = [] gc_collect() diff --git a/tests/test_raw_cursor_async.py b/tests/test_raw_cursor_async.py index 189a208f1..b207b28c8 100644 --- a/tests/test_raw_cursor_async.py +++ b/tests/test_raw_cursor_async.py @@ -20,7 +20,7 @@ async def test_default_cursor(aconn): async def test_str(aconn): cur = aconn.cursor() - assert "psycopg.AsyncRawCursor" in str(cur) + assert "psycopg.%s" % psycopg.AsyncRawCursor.__name__ in str(cur) async def test_sequence_only(aconn): diff --git a/tests/test_server_cursor.py b/tests/test_server_cursor.py index 385dcd774..3a9e4f680 100644 --- a/tests/test_server_cursor.py +++ b/tests/test_server_cursor.py @@ -1,9 +1,13 @@ +# WARNING: this file is auto-generated by 'async_to_sync.py' +# from the original file 'test_server_cursor_async.py' +# DO NOT CHANGE! Change the original file instead. import pytest import psycopg from psycopg import rows, errors as e from psycopg.pq import Format + pytestmark = pytest.mark.crdb_skip("server-side cursor") @@ -45,7 +49,7 @@ def test_funny_name(conn): def test_repr(conn): cur = conn.cursor("my-name") - assert "psycopg.ServerCursor" in str(cur) + assert "psycopg.%s" % psycopg.ServerCursor.__name__ in str(cur) assert "my-name" in repr(cur) cur.close() @@ -81,7 +85,7 @@ def test_query_params(conn): with conn.cursor("foo") as cur: assert cur._query is None cur.execute("select generate_series(1, %s) as bar", (3,)) - assert cur._query + assert cur._query is not None assert b"declare" in cur._query.query.lower() assert b"(1, $1)" in cur._query.query.lower() assert cur._query.params == [bytes([0, 3])] # 3 as binary int2 diff --git a/tests/test_server_cursor_async.py b/tests/test_server_cursor_async.py index 99ff9cc76..26db29ef3 100644 --- a/tests/test_server_cursor_async.py +++ b/tests/test_server_cursor_async.py @@ -4,9 +4,9 @@ import psycopg from psycopg import rows, errors as e from psycopg.pq import Format -pytestmark = [ - pytest.mark.crdb_skip("server-side cursor"), -] +from .utils import alist + +pytestmark = pytest.mark.crdb_skip("server-side cursor") async def test_init_row_factory(aconn): @@ -51,7 +51,7 @@ async def test_funny_name(aconn): async def test_repr(aconn): cur = aconn.cursor("my-name") - assert "psycopg.AsyncServerCursor" in str(cur) + assert "psycopg.%s" % psycopg.AsyncServerCursor.__name__ in str(cur) assert "my-name" in repr(cur) await cur.close() @@ -115,7 +115,7 @@ async def test_execute_binary(aconn): assert cur.pgresult.fformat(0) == 1 assert cur.pgresult.get_value(0, 0) == b"\x00\x00\x00\x02" - await cur.execute("select generate_series(1, 1)") + await cur.execute("select generate_series(1, 1)::int4") assert (await cur.fetchone()) == (1,) assert cur.pgresult.fformat(0) == 0 assert cur.pgresult.get_value(0, 0) == b"1" @@ -407,17 +407,13 @@ async def test_rownumber(aconn): async def test_iter(aconn): async with aconn.cursor("foo") as cur: await cur.execute("select generate_series(1, %s) as bar", (3,)) - recs = [] - async for rec in cur: - recs.append(rec) + recs = await alist(cur) assert recs == [(1,), (2,), (3,)] async with aconn.cursor("foo") as cur: await cur.execute("select generate_series(1, %s) as bar", (3,)) assert await cur.fetchone() == (1,) - recs = [] - async for rec in cur: - recs.append(rec) + recs = await alist(cur) assert recs == [(2,), (3,)] @@ -435,8 +431,7 @@ async def test_itersize(aconn, acommands): await cur.execute("select generate_series(1, %s) as bar", (3,)) acommands.popall() # flush begin and other noise - async for rec in cur: - pass + await alist(cur) cmds = acommands.popall() assert len(cmds) == 2 for cmd in cmds: @@ -516,9 +511,7 @@ async def test_hold(aconn): @pytest.mark.parametrize("row_factory", ["tuple_row", "namedtuple_row"]) async def test_steal_cursor(aconn, row_factory): cur1 = aconn.cursor() - await cur1.execute( - "declare test cursor without hold for select generate_series(1, 6) as s" - ) + await cur1.execute("declare test cursor for select generate_series(1, 6) as s") cur2 = aconn.cursor("test", row_factory=getattr(rows, row_factory)) # can call fetch without execute diff --git a/tools/async_to_sync.py b/tools/async_to_sync.py index 647bb4f75..fa6db75af 100755 --- a/tools/async_to_sync.py +++ b/tools/async_to_sync.py @@ -115,6 +115,7 @@ class RenameAsyncToSync(ast.NodeTransformer): "AsyncServerCursor": "ServerCursor", "aclose": "close", "aclosing": "closing", + "acommands": "commands", "aconn": "conn", "aconn_cls": "conn_cls", "aconn_set": "conn_set", diff --git a/tools/convert_async_to_sync.sh b/tools/convert_async_to_sync.sh index 9906b2042..f71354028 100755 --- a/tools/convert_async_to_sync.sh +++ b/tools/convert_async_to_sync.sh @@ -12,7 +12,10 @@ for async in \ tests/test_connection_async.py \ tests/test_copy_async.py \ tests/test_cursor_async.py \ - tests/test_pipeline_async.py + tests/test_default_cursor_async.py \ + tests/test_pipeline_async.py \ + tests/test_raw_cursor_async.py \ + tests/test_server_cursor_async.py do sync=${async/_async/} echo "converting '${async}' -> '${sync}'" >&2