From: Daniele Varrazzo Date: Mon, 14 Aug 2023 18:34:19 +0000 (+0100) Subject: tests: fix cursor tests to run on raw cursors too X-Git-Tag: pool-3.2.0~66^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1513077e2db92695609b16bb48c196850bbe9bad;p=thirdparty%2Fpsycopg.git tests: fix cursor tests to run on raw cursors too --- diff --git a/tests/fix_faker.py b/tests/fix_faker.py index 41376a7fa..e302d22e5 100644 --- a/tests/fix_faker.py +++ b/tests/fix_faker.py @@ -146,7 +146,7 @@ class Faker: with conn.transaction(): yield except psycopg.DatabaseError: - cur = conn.cursor() + cur = psycopg.Cursor(conn) # Repeat insert one field at time, until finding the wrong one cur.execute(self.drop_stmt) cur.execute(self.create_stmt) @@ -171,7 +171,7 @@ class Faker: async with aconn.transaction(): yield except psycopg.DatabaseError: - acur = aconn.cursor() + acur = psycopg.AsyncCursor(aconn) # Repeat insert one field at time, until finding the wrong one await acur.execute(self.drop_stmt) await acur.execute(self.create_stmt) diff --git a/tests/test_cursor.py b/tests/test_cursor.py index f2dcb6ea3..3fb15fc25 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -2,10 +2,11 @@ Tests common to psycopg.Cursor and its subclasses. """ +import re import pickle import weakref import datetime as dt -from typing import List, Union +from typing import Any, List, Match, Union from contextlib import closing import pytest @@ -27,6 +28,25 @@ def conn(conn, request): return conn +def ph(cur: Any, query: str) -> str: + """Change placeholders in a query from %s to $n if testing a raw cursor""" + if not isinstance(cur, (psycopg.RawCursor, psycopg.AsyncRawCursor)): + return query + + if "%(" in query: + raise pytest.skip("RawCursor only supports positional placeholders") + + n = 1 + + def s(m: Match[str]) -> str: + nonlocal n + rv = f"${n}" + n += 1 + return rv + + return re.sub(r"(?