From: Daniele Varrazzo Date: Sat, 11 Nov 2023 16:47:35 +0000 (+0000) Subject: test: allow to run pytest -m pool with psycopg 3.1 imported X-Git-Tag: pool-3.2.1~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a0f15dd73e0ed16380dc6aba9f7144285ac47d43;p=thirdparty%2Fpsycopg.git test: allow to run pytest -m pool with psycopg 3.1 imported This allows to test psycopg pool 3.2 tests with psycopg 3.1 installed. --- diff --git a/psycopg/psycopg/_compat.py b/psycopg/psycopg/_compat.py index d496817ad..c9e97e760 100644 --- a/psycopg/psycopg/_compat.py +++ b/psycopg/psycopg/_compat.py @@ -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", ] diff --git a/tests/pool/test_pool.py b/tests/pool/test_pool.py index f959203e3..6323420e2 100644 --- a/tests/pool/test_pool.py +++ b/tests/pool/test_pool.py @@ -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") diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index 126abddb9..7319e00b4 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -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") diff --git a/tests/pool/test_pool_common.py b/tests/pool/test_pool_common.py index 72274074e..ee8d03ffd 100644 --- a/tests/pool/test_pool_common.py +++ b/tests/pool/test_pool_common.py @@ -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 diff --git a/tests/pool/test_pool_common_async.py b/tests/pool/test_pool_common_async.py index 90a06fbf4..dbcfbbe46 100644 --- a/tests/pool/test_pool_common_async.py +++ b/tests/pool/test_pool_common_async.py @@ -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 diff --git a/tests/pool/test_pool_null.py b/tests/pool/test_pool_null.py index c9af4f8de..fbe698df6 100644 --- a/tests/pool/test_pool_null.py +++ b/tests/pool/test_pool_null.py @@ -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() diff --git a/tests/pool/test_pool_null_async.py b/tests/pool/test_pool_null_async.py index 0efd61bd7..09c0e2150 100644 --- a/tests/pool/test_pool_null_async.py +++ b/tests/pool/test_pool_null_async.py @@ -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() diff --git a/tests/test_cursor_common.py b/tests/test_cursor_common.py index 19cc31bbf..535aa0153 100644 --- a/tests/test_cursor_common.py +++ b/tests/test_cursor_common.py @@ -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 diff --git a/tests/test_cursor_common_async.py b/tests/test_cursor_common_async.py index cad01cfbf..4268fdd70 100644 --- a/tests/test_cursor_common_async.py +++ b/tests/test_cursor_common_async.py @@ -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 diff --git a/tests/utils.py b/tests/utils.py index c1dc86dd1..88a6bb9a0 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -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}")