]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
test: allow to run pytest -m pool with psycopg 3.1 imported
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 11 Nov 2023 16:47:35 +0000 (16:47 +0000)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 11 Nov 2023 19:08:11 +0000 (19:08 +0000)
This allows to test psycopg pool 3.2 tests with psycopg 3.1 installed.

psycopg/psycopg/_compat.py
tests/pool/test_pool.py
tests/pool/test_pool_async.py
tests/pool/test_pool_common.py
tests/pool/test_pool_common_async.py
tests/pool/test_pool_null.py
tests/pool/test_pool_null_async.py
tests/test_cursor_common.py
tests/test_cursor_common_async.py
tests/utils.py

index d496817ad009bf8d141cff6fcd40a30a1b6cfa22..c9e97e7609a00f5579520390c076c5e423d658e3 100644 (file)
@@ -23,9 +23,9 @@ else:
     from typing_extensions import TypeGuard
 
 if sys.version_info >= (3, 11):
-    from typing import LiteralString, assert_type
+    from typing import LiteralString
 else:
-    from typing_extensions import LiteralString, assert_type
+    from typing_extensions import LiteralString
 
 __all__ = [
     "Counter",
@@ -33,6 +33,5 @@ __all__ = [
     "LiteralString",
     "TypeGuard",
     "ZoneInfo",
-    "assert_type",
     "cache",
 ]
index f959203e349734b5df7a2480d8ebaa94323c50d3..6323420e2dd8b4d96832d162b70c9abc22029caa 100644 (file)
@@ -11,8 +11,8 @@ import pytest
 import psycopg
 from psycopg.pq import TransactionStatus
 from psycopg.rows import class_row, Row, TupleRow
-from psycopg._compat import assert_type, Counter
 
+from ..utils import assert_type, Counter, set_autocommit
 from ..acompat import Event, spawn, gather, sleep, skip_sync
 from .test_pool_common import delay_connection
 
@@ -46,8 +46,8 @@ class MyRow(Dict[str, Any]):
 
 
 def test_generic_connection_type(dsn):
-    def set_autocommit(conn: psycopg.Connection[Any]) -> None:
-        conn.set_autocommit(True)
+    def configure(conn: psycopg.Connection[Any]) -> None:
+        set_autocommit(conn, True)
 
     class MyConnection(psycopg.Connection[Row]):
         pass
@@ -56,7 +56,7 @@ def test_generic_connection_type(dsn):
         dsn,
         connection_class=MyConnection[MyRow],
         kwargs=dict(row_factory=class_row(MyRow)),
-        configure=set_autocommit,
+        configure=configure,
     ) as p1:
         with p1.connection() as conn1:
             cur1 = conn1.execute("select 1 as x")
@@ -78,8 +78,8 @@ def test_generic_connection_type(dsn):
 
 
 def test_non_generic_connection_type(dsn):
-    def set_autocommit(conn: psycopg.Connection[Any]) -> None:
-        conn.set_autocommit(True)
+    def configure(conn: psycopg.Connection[Any]) -> None:
+        set_autocommit(conn, True)
 
     class MyConnection(psycopg.Connection[MyRow]):
         def __init__(self, *args: Any, **kwargs: Any):
@@ -87,7 +87,7 @@ def test_non_generic_connection_type(dsn):
             super().__init__(*args, **kwargs)
 
     with pool.ConnectionPool(
-        dsn, connection_class=MyConnection, configure=set_autocommit
+        dsn, connection_class=MyConnection, configure=configure
     ) as p1:
         with p1.connection() as conn1:
             cur1 = conn1.execute("select 1 as x")
index 126abddb90ee4e935dc0ee594e3fc9fdd0e200ea..7319e00b444bc216f0e3de5d5e1c30b7991453e9 100644 (file)
@@ -8,8 +8,8 @@ import pytest
 import psycopg
 from psycopg.pq import TransactionStatus
 from psycopg.rows import class_row, Row, TupleRow
-from psycopg._compat import assert_type, Counter
 
+from ..utils import assert_type, Counter, set_autocommit
 from ..acompat import AEvent, spawn, gather, asleep, skip_sync
 from .test_pool_common_async import delay_connection
 
@@ -46,8 +46,8 @@ class MyRow(Dict[str, Any]):
 
 
 async def test_generic_connection_type(dsn):
-    async def set_autocommit(conn: psycopg.AsyncConnection[Any]) -> None:
-        await conn.set_autocommit(True)
+    async def configure(conn: psycopg.AsyncConnection[Any]) -> None:
+        await set_autocommit(conn, True)
 
     class MyConnection(psycopg.AsyncConnection[Row]):
         pass
@@ -56,7 +56,7 @@ async def test_generic_connection_type(dsn):
         dsn,
         connection_class=MyConnection[MyRow],
         kwargs=dict(row_factory=class_row(MyRow)),
-        configure=set_autocommit,
+        configure=configure,
     ) as p1:
         async with p1.connection() as conn1:
             cur1 = await conn1.execute("select 1 as x")
@@ -80,8 +80,8 @@ async def test_generic_connection_type(dsn):
 
 
 async def test_non_generic_connection_type(dsn):
-    async def set_autocommit(conn: psycopg.AsyncConnection[Any]) -> None:
-        await conn.set_autocommit(True)
+    async def configure(conn: psycopg.AsyncConnection[Any]) -> None:
+        await set_autocommit(conn, True)
 
     class MyConnection(psycopg.AsyncConnection[MyRow]):
         def __init__(self, *args: Any, **kwargs: Any):
@@ -91,7 +91,7 @@ async def test_non_generic_connection_type(dsn):
     async with pool.AsyncConnectionPool(
         dsn,
         connection_class=MyConnection,
-        configure=set_autocommit,
+        configure=configure,
     ) as p1:
         async with p1.connection() as conn1:
             cur1 = await conn1.execute("select 1 as x")
index 72274074e408858b4994118f6558e8986df1d1f8..ee8d03ffdeb0d53d0f14852844738573fbc3f086 100644 (file)
@@ -9,6 +9,7 @@ import pytest
 
 import psycopg
 
+from ..utils import set_autocommit
 from ..acompat import Event, spawn, gather, sleep, is_alive, skip_async, skip_sync
 
 try:
@@ -581,7 +582,7 @@ def test_debug_deadlock(pool_cls, dsn):
 @pytest.mark.parametrize("autocommit", [True, False])
 def test_check_connection(pool_cls, conn_cls, dsn, autocommit):
     conn = conn_cls.connect(dsn)
-    conn.set_autocommit(autocommit)
+    set_autocommit(conn, autocommit)
     pool_cls.check_connection(conn)
     assert not conn.closed
     assert conn.info.transaction_status == psycopg.pq.TransactionStatus.IDLE
index 90a06fbf4a276c7a1d728ee4d58aca4e50c2875c..dbcfbbe46c078e5b535f62ef70e994a39c33da3f 100644 (file)
@@ -6,6 +6,7 @@ import pytest
 
 import psycopg
 
+from ..utils import set_autocommit
 from ..acompat import AEvent, spawn, gather, asleep, is_alive, skip_async, skip_sync
 
 try:
@@ -600,7 +601,7 @@ async def test_debug_deadlock(pool_cls, dsn):
 @pytest.mark.parametrize("autocommit", [True, False])
 async def test_check_connection(pool_cls, aconn_cls, dsn, autocommit):
     conn = await aconn_cls.connect(dsn)
-    await conn.set_autocommit(autocommit)
+    await set_autocommit(conn, autocommit)
     await pool_cls.check_connection(conn)
     assert not conn.closed
     assert conn.info.transaction_status == psycopg.pq.TransactionStatus.IDLE
index c9af4f8de8e83f262cd26ef5c4a9aa39d95cdbe4..fbe698df68658a6cf77e65e2fe0cce8dae3f12ff 100644 (file)
@@ -10,8 +10,8 @@ from packaging.version import parse as ver  # noqa: F401  # used in skipif
 import psycopg
 from psycopg.pq import TransactionStatus
 from psycopg.rows import class_row, Row, TupleRow
-from psycopg._compat import assert_type
 
+from ..utils import assert_type, set_autocommit
 from ..acompat import Event, sleep, spawn, gather, skip_sync
 from .test_pool_common import delay_connection, ensure_waiting
 
@@ -44,8 +44,8 @@ class MyRow(Dict[str, Any]):
 
 
 def test_generic_connection_type(dsn):
-    def set_autocommit(conn: psycopg.Connection[Any]) -> None:
-        conn.set_autocommit(True)
+    def configure(conn: psycopg.Connection[Any]) -> None:
+        set_autocommit(conn, True)
 
     class MyConnection(psycopg.Connection[Row]):
         pass
@@ -54,7 +54,7 @@ def test_generic_connection_type(dsn):
         dsn,
         connection_class=MyConnection[MyRow],
         kwargs={"row_factory": class_row(MyRow)},
-        configure=set_autocommit,
+        configure=configure,
     ) as p1:
         with p1.connection() as conn1:
             cur1 = conn1.execute("select 1 as x")
@@ -76,8 +76,8 @@ def test_generic_connection_type(dsn):
 
 
 def test_non_generic_connection_type(dsn):
-    def set_autocommit(conn: psycopg.Connection[Any]) -> None:
-        conn.set_autocommit(True)
+    def configure(conn: psycopg.Connection[Any]) -> None:
+        set_autocommit(conn, True)
 
     class MyConnection(psycopg.Connection[MyRow]):
         def __init__(self, *args: Any, **kwargs: Any):
@@ -85,7 +85,7 @@ def test_non_generic_connection_type(dsn):
             super().__init__(*args, **kwargs)
 
     with pool.NullConnectionPool(
-        dsn, connection_class=MyConnection, configure=set_autocommit
+        dsn, connection_class=MyConnection, configure=configure
     ) as p1:
         with p1.connection() as conn1:
             (row1,) = conn1.execute("select 1 as x").fetchall()
index 0efd61bd7b5ee699725cf04bc76599a3d6e467d9..09c0e21509050d9bf71c3a6b7022d1bc75e8960f 100644 (file)
@@ -7,8 +7,8 @@ from packaging.version import parse as ver  # noqa: F401  # used in skipif
 import psycopg
 from psycopg.pq import TransactionStatus
 from psycopg.rows import class_row, Row, TupleRow
-from psycopg._compat import assert_type
 
+from ..utils import assert_type, set_autocommit
 from ..acompat import AEvent, asleep, spawn, gather, skip_sync
 from .test_pool_common_async import delay_connection, ensure_waiting
 
@@ -44,8 +44,8 @@ class MyRow(Dict[str, Any]):
 
 
 async def test_generic_connection_type(dsn):
-    async def set_autocommit(conn: psycopg.AsyncConnection[Any]) -> None:
-        await conn.set_autocommit(True)
+    async def configure(conn: psycopg.AsyncConnection[Any]) -> None:
+        await set_autocommit(conn, True)
 
     class MyConnection(psycopg.AsyncConnection[Row]):
         pass
@@ -54,7 +54,7 @@ async def test_generic_connection_type(dsn):
         dsn,
         connection_class=MyConnection[MyRow],
         kwargs={"row_factory": class_row(MyRow)},
-        configure=set_autocommit,
+        configure=configure,
     ) as p1:
         async with p1.connection() as conn1:
             cur1 = await conn1.execute("select 1 as x")
@@ -78,8 +78,8 @@ async def test_generic_connection_type(dsn):
 
 
 async def test_non_generic_connection_type(dsn):
-    async def set_autocommit(conn: psycopg.AsyncConnection[Any]) -> None:
-        await conn.set_autocommit(True)
+    async def configure(conn: psycopg.AsyncConnection[Any]) -> None:
+        await set_autocommit(conn, True)
 
     class MyConnection(psycopg.AsyncConnection[MyRow]):
         def __init__(self, *args: Any, **kwargs: Any):
@@ -87,7 +87,7 @@ async def test_non_generic_connection_type(dsn):
             super().__init__(*args, **kwargs)
 
     async with pool.AsyncNullConnectionPool(
-        dsn, connection_class=MyConnection, configure=set_autocommit
+        dsn, connection_class=MyConnection, configure=configure
     ) as p1:
         async with p1.connection() as conn1:
             (row1,) = await (await conn1.execute("select 1 as x")).fetchall()
index 19cc31bbf1b802469a1c600c902ee20fb521b67f..535aa01535c2ff242ea43e12f666a12b214e54a0 100644 (file)
@@ -8,6 +8,7 @@ Tests common to psycopg.Cursor and its subclasses.
 import weakref
 import datetime as dt
 from typing import Any, List
+from packaging.version import parse as ver
 
 import pytest
 
@@ -24,8 +25,15 @@ from ._test_cursor import execmany, _execmany  # noqa: F401
 
 execmany = execmany  # avoid F811 underneath
 
+cursor_classes = [psycopg.Cursor, psycopg.ClientCursor]
+# Allow to import (not necessarily to run) the module with psycopg 3.1.
+# Needed to test psycopg_pool 3.2 tests with psycopg 3.1 imported, i.e. to run
+# `pytest -m pool`. (which might happen when releasing pool packages).
+if ver(psycopg.__version__) >= ver("3.2.0.dev0"):
+    cursor_classes.append(psycopg.RawCursor)
 
-@pytest.fixture(params=[psycopg.Cursor, psycopg.ClientCursor, psycopg.RawCursor])
+
+@pytest.fixture(params=cursor_classes)
 def conn(conn, request, anyio_backend):
     conn.cursor_factory = request.param
     return conn
index cad01cfbf7e5adc4d26b6fecfaefcb3eb34aec8b..4268fdd70606442a479e815e719180e84adb9a74 100644 (file)
@@ -5,6 +5,7 @@ Tests common to psycopg.AsyncCursor and its subclasses.
 import weakref
 import datetime as dt
 from typing import Any, List
+from packaging.version import parse as ver
 
 import pytest
 
@@ -22,9 +23,15 @@ from ._test_cursor import execmany, _execmany  # noqa: F401
 execmany = execmany  # avoid F811 underneath
 
 
-@pytest.fixture(
-    params=[psycopg.AsyncCursor, psycopg.AsyncClientCursor, psycopg.AsyncRawCursor]
-)
+cursor_classes = [psycopg.AsyncCursor, psycopg.AsyncClientCursor]
+# Allow to import (not necessarily to run) the module with psycopg 3.1.
+# Needed to test psycopg_pool 3.2 tests with psycopg 3.1 imported, i.e. to run
+# `pytest -m pool`. (which might happen when releasing pool packages).
+if ver(psycopg.__version__) >= ver("3.2.0.dev0"):
+    cursor_classes.append(psycopg.AsyncRawCursor)
+
+
+@pytest.fixture(params=cursor_classes)
 async def aconn(aconn, request, anyio_backend):
     aconn.cursor_factory = request.param
     return aconn
index c1dc86dd14c62d6198aae826bb6fda7d0b338501..88a6bb9a03479963f2dfbce792ced4c1663ab9c7 100644 (file)
@@ -5,6 +5,26 @@ import operator
 from typing import Callable, Optional, Tuple
 from contextlib import contextmanager
 
+
+if sys.version_info >= (3, 9):
+    import collections
+
+    Counter = collections.Counter
+else:
+    import typing
+
+    Counter = typing.Counter
+
+if sys.version_info >= (3, 11):
+    import typing
+
+    assert_type = typing.assert_type
+else:
+    import typing_extensions
+
+    assert_type = typing_extensions.assert_type
+
+
 import pytest
 
 eur = "\u20ac"
@@ -192,3 +212,20 @@ def raiseif(cond, *args, **kwargs):
         with pytest.raises(*args, **kwargs) as ex:
             yield ex
         return
+
+
+def set_autocommit(conn, value):
+    """
+    Set autocommit on a connection.
+
+    Give an uniform interface to both sync and async connection for psycopg
+    < 3.2, in order to run psycopg_pool 3.2 tests using psycopg 3.1.
+    """
+    import psycopg
+
+    if isinstance(conn, psycopg.Connection):
+        conn.autocommit = value
+    elif isinstance(conn, psycopg.AsyncConnection):
+        return conn.set_autocommit(value)
+    else:
+        raise TypeError(f"not a connection: {conn}")