]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor(tests): auto-generate sync version of cursor subclasses
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 15 Aug 2023 18:05:58 +0000 (19:05 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 11 Oct 2023 21:45:38 +0000 (23:45 +0200)
tests/test_client_cursor.py
tests/test_client_cursor_async.py
tests/test_default_cursor.py
tests/test_default_cursor_async.py
tests/test_raw_cursor.py
tests/test_raw_cursor_async.py
tests/test_server_cursor.py
tests/test_server_cursor_async.py
tools/async_to_sync.py
tools/convert_async_to_sync.sh

index ee6a69ee77e26bf5351dbeb8c9321037b01ab02d..5d56b3ebf5e5a2df2bb7d093927de4e3f1bef42d 100644 (file)
@@ -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()
index 43505cb4c15bc0aaff75a90fb68ef45024a3c879..d1abbe84915cfd407c5eec9690af866b020e1f03 100644 (file)
@@ -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
index 92be4461b7b3a673badef01b11b84804134890a5..9cd230659a8b09c3e41e51d95425c04eb1bd150d 100644 (file)
@@ -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]}"
index 64c5bfad5585004b324f4a324274ff2973cbf710..1ebc827cda249f956ae047159d0687299d198a36 100644 (file)
@@ -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):
index fd6fe9bc5854117c2444b563eda61bc61aca8937..887cdc1644f4415be16c29125b2689dd99119d96 100644 (file)
@@ -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()
index 189a208f18aeb145a307165fe958ed81e6e88cf1..b207b28c83c53924e611a94379b61b430f846b09 100644 (file)
@@ -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):
index 385dcd774839ac981c5df2ea9c397a66c20dc6cb..3a9e4f680e87f914078f9bcd1d49c897893146c7 100644 (file)
@@ -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
index 99ff9cc76686775f1949af1ab955f12557ae70f8..26db29ef36ecf2f8e8b6d37b4a00c7fa0f3676a4 100644 (file)
@@ -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
index 647bb4f75621a4ffcfeb3fbcf30e9b8b958c3781..fa6db75af46e609c534ba6b922f6d05bfe8c9df6 100755 (executable)
@@ -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",
index 9906b2042097aa3848bf741243eafa75fd3b78b5..f71354028645015edd15fd6802fe49cdbb5a8fd5 100755 (executable)
@@ -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