From: Daniele Varrazzo Date: Sun, 8 Oct 2023 13:55:31 +0000 (+0200) Subject: refactor(test): uniform the way sync/async-only tests are skipped X-Git-Tag: pool-3.2.0~12^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bd0e01afdf846a66a21f9c2d0249c046f0a0ac88;p=thirdparty%2Fpsycopg.git refactor(test): uniform the way sync/async-only tests are skipped Add @skip_sync, @skip_async markers to mark the functions to skip. --- diff --git a/tests/acompat.py b/tests/acompat.py index 256e29f7c..41c2f40b4 100644 --- a/tests/acompat.py +++ b/tests/acompat.py @@ -15,11 +15,17 @@ import threading import contextlib from typing import Any +import pytest + # Re-exports sleep = time.sleep Event = threading.Event closing = contextlib.closing +# Markers to decorate tests to run only in async or only in sync version. +skip_sync = pytest.mark.skipif("'async' not in __name__", reason="async test only") +skip_async = pytest.mark.skipif("'async' in __name__", reason="sync test only") + def is_async(obj): """Return true if obj is an async object (class, instance, module name)""" diff --git a/tests/pool/test_pool.py b/tests/pool/test_pool.py index 7d96d3f3d..9ab1b01d2 100644 --- a/tests/pool/test_pool.py +++ b/tests/pool/test_pool.py @@ -13,7 +13,7 @@ from psycopg.pq import TransactionStatus from psycopg.rows import class_row, Row, TupleRow from psycopg._compat import assert_type, Counter -from ..acompat import Event, spawn, gather, sleep, is_async +from ..acompat import Event, spawn, gather, sleep, skip_sync from .test_pool_common import delay_connection try: @@ -509,11 +509,8 @@ def test_reconnect(proxy, caplog, monkeypatch): @pytest.mark.slow @pytest.mark.timing -@pytest.mark.parametrize("async_cb", [True, False]) +@pytest.mark.parametrize("async_cb", [pytest.param(True, marks=skip_sync), False]) def test_reconnect_failure(proxy, async_cb): - if async_cb and (not is_async(__name__)): - pytest.skip("async test only") - proxy.start() t1 = None @@ -798,7 +795,7 @@ def test_debug_deadlock(dsn): logger.setLevel(old_level) -@pytest.mark.skipif(not is_async(__name__), reason="async test only") +@skip_sync def test_cancellation_in_queue(dsn): # https://github.com/psycopg/psycopg/issues/509 diff --git a/tests/pool/test_pool_async.py b/tests/pool/test_pool_async.py index be018f2b6..27b55bbe1 100644 --- a/tests/pool/test_pool_async.py +++ b/tests/pool/test_pool_async.py @@ -10,7 +10,7 @@ from psycopg.pq import TransactionStatus from psycopg.rows import class_row, Row, TupleRow from psycopg._compat import assert_type, Counter -from ..acompat import AEvent, spawn, gather, asleep, is_async +from ..acompat import AEvent, spawn, gather, asleep, skip_sync from .test_pool_common_async import delay_connection try: @@ -515,11 +515,8 @@ async def test_reconnect(proxy, caplog, monkeypatch): @pytest.mark.slow @pytest.mark.timing -@pytest.mark.parametrize("async_cb", [True, False]) +@pytest.mark.parametrize("async_cb", [pytest.param(True, marks=skip_sync), False]) async def test_reconnect_failure(proxy, async_cb): - if async_cb and not is_async(__name__): - pytest.skip("async test only") - proxy.start() t1 = None @@ -804,7 +801,7 @@ async def test_debug_deadlock(dsn): logger.setLevel(old_level) -@pytest.mark.skipif(not is_async(__name__), reason="async test only") +@skip_sync async def test_cancellation_in_queue(dsn): # https://github.com/psycopg/psycopg/issues/509 diff --git a/tests/pool/test_pool_common.py b/tests/pool/test_pool_common.py index e80550c32..ce60ea795 100644 --- a/tests/pool/test_pool_common.py +++ b/tests/pool/test_pool_common.py @@ -9,7 +9,7 @@ import pytest import psycopg -from ..acompat import Event, spawn, gather, sleep, is_alive, is_async +from ..acompat import Event, spawn, gather, sleep, is_alive, skip_async, skip_sync try: import psycopg_pool as pool @@ -299,8 +299,8 @@ def test_putconn_wrong_pool(pool_cls, dsn): p2.putconn(conn) +@skip_async @pytest.mark.slow -@pytest.mark.skipif(is_async(__name__), reason="sync test only") def test_del_stops_threads(pool_cls, dsn): p = pool_cls(dsn) assert p._sched_runner is not None @@ -529,7 +529,7 @@ def test_debug_deadlock(pool_cls, dsn): logger.setLevel(old_level) -@pytest.mark.skipif(not is_async(__name__), reason="async test only") +@skip_sync def test_cancellation_in_queue(pool_cls, dsn): # https://github.com/psycopg/psycopg/issues/509 diff --git a/tests/pool/test_pool_common_async.py b/tests/pool/test_pool_common_async.py index 6d1b9fe1b..ff6dcc0bc 100644 --- a/tests/pool/test_pool_common_async.py +++ b/tests/pool/test_pool_common_async.py @@ -6,7 +6,7 @@ import pytest import psycopg -from ..acompat import AEvent, spawn, gather, asleep, is_alive, is_async +from ..acompat import AEvent, spawn, gather, asleep, is_alive, skip_async, skip_sync try: import psycopg_pool as pool @@ -309,8 +309,8 @@ async def test_putconn_wrong_pool(pool_cls, dsn): await p2.putconn(conn) +@skip_async @pytest.mark.slow -@pytest.mark.skipif(is_async(__name__), reason="sync test only") async def test_del_stops_threads(pool_cls, dsn): p = pool_cls(dsn) assert p._sched_runner is not None @@ -541,7 +541,7 @@ async def test_debug_deadlock(pool_cls, dsn): logger.setLevel(old_level) -@pytest.mark.skipif(not is_async(__name__), reason="async test only") +@skip_sync async def test_cancellation_in_queue(pool_cls, dsn): # https://github.com/psycopg/psycopg/issues/509 diff --git a/tests/pool/test_pool_null.py b/tests/pool/test_pool_null.py index 3a28beac3..c9af4f8de 100644 --- a/tests/pool/test_pool_null.py +++ b/tests/pool/test_pool_null.py @@ -12,7 +12,7 @@ from psycopg.pq import TransactionStatus from psycopg.rows import class_row, Row, TupleRow from psycopg._compat import assert_type -from ..acompat import Event, sleep, spawn, gather, is_async +from ..acompat import Event, sleep, spawn, gather, skip_sync from .test_pool_common import delay_connection, ensure_waiting try: @@ -437,7 +437,7 @@ def test_stats_connect(dsn, proxy, monkeypatch): assert 200 <= stats["connections_ms"] < 300 -@pytest.mark.skipif(not is_async(__name__), reason="async test only") +@skip_sync def test_cancellation_in_queue(dsn): # https://github.com/psycopg/psycopg/issues/509 diff --git a/tests/pool/test_pool_null_async.py b/tests/pool/test_pool_null_async.py index 106437eb5..0efd61bd7 100644 --- a/tests/pool/test_pool_null_async.py +++ b/tests/pool/test_pool_null_async.py @@ -9,7 +9,7 @@ from psycopg.pq import TransactionStatus from psycopg.rows import class_row, Row, TupleRow from psycopg._compat import assert_type -from ..acompat import AEvent, asleep, spawn, gather, is_async +from ..acompat import AEvent, asleep, spawn, gather, skip_sync from .test_pool_common_async import delay_connection, ensure_waiting try: @@ -441,7 +441,7 @@ async def test_stats_connect(dsn, proxy, monkeypatch): assert 200 <= stats["connections_ms"] < 300 -@pytest.mark.skipif(not is_async(__name__), reason="async test only") +@skip_sync async def test_cancellation_in_queue(dsn): # https://github.com/psycopg/psycopg/issues/509 diff --git a/tests/test_connection.py b/tests/test_connection.py index dd27e6e39..dcc38a2bc 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -14,7 +14,7 @@ from psycopg.rows import tuple_row from psycopg.conninfo import conninfo_to_dict, make_conninfo from .utils import gc_collect -from .acompat import is_async +from .acompat import is_async, skip_sync, skip_async from ._test_cursor import my_row_factory from ._test_connection import tx_params, tx_params_isolation, tx_values_map from ._test_connection import conninfo_params_timeout @@ -312,7 +312,7 @@ def test_auto_transaction_fail(conn): assert conn.pgconn.transaction_status == conn.TransactionStatus.INTRANS -@pytest.mark.skipif(not is_async(__name__), reason="async test only") +@skip_sync def test_autocommit_readonly_property(conn): with pytest.raises(AttributeError): conn.autocommit = True @@ -337,7 +337,7 @@ def test_autocommit(conn): assert conn.autocommit is True -@pytest.mark.skipif(is_async(__name__), reason="sync test only") +@skip_async def test_autocommit_property(conn): assert conn.autocommit is False @@ -637,7 +637,7 @@ def test_transaction_param_default(conn, param): assert current == default -@pytest.mark.skipif(not is_async(__name__), reason="async test only") +@skip_sync @pytest.mark.parametrize("param", tx_params) def test_transaction_param_readonly_property(conn, param): with pytest.raises(AttributeError): @@ -723,7 +723,7 @@ def test_set_transaction_param_not_intrans_external(conn, param): getattr(conn, f"set_{param.name}")(value) -@pytest.mark.skipif(is_async(__name__), reason="sync test only") +@skip_async @pytest.mark.crdb("skip", reason="transaction isolation") def test_set_transaction_param_all_property(conn): params: List[Any] = tx_params[:] @@ -769,7 +769,7 @@ def test_set_transaction_param_strange(conn): assert conn.deferrable is False -@pytest.mark.skipif(is_async(__name__), reason="sync test only") +@skip_async def test_set_transaction_param_strange_property(conn): for val in ("asdf", 0, 5): with pytest.raises(ValueError): diff --git a/tests/test_connection_async.py b/tests/test_connection_async.py index b68e36a26..36a6c9f9f 100644 --- a/tests/test_connection_async.py +++ b/tests/test_connection_async.py @@ -11,7 +11,7 @@ from psycopg.rows import tuple_row from psycopg.conninfo import conninfo_to_dict, make_conninfo from .utils import gc_collect -from .acompat import is_async +from .acompat import is_async, skip_sync, skip_async from ._test_cursor import my_row_factory from ._test_connection import tx_params, tx_params_isolation, tx_values_map from ._test_connection import conninfo_params_timeout @@ -310,7 +310,7 @@ async def test_auto_transaction_fail(aconn): assert aconn.pgconn.transaction_status == aconn.TransactionStatus.INTRANS -@pytest.mark.skipif(not is_async(__name__), reason="async test only") +@skip_sync async def test_autocommit_readonly_property(aconn): with pytest.raises(AttributeError): aconn.autocommit = True @@ -335,7 +335,7 @@ async def test_autocommit(aconn): assert aconn.autocommit is True -@pytest.mark.skipif(is_async(__name__), reason="sync test only") +@skip_async def test_autocommit_property(conn): assert conn.autocommit is False @@ -639,7 +639,7 @@ async def test_transaction_param_default(aconn, param): assert current == default -@pytest.mark.skipif(not is_async(__name__), reason="async test only") +@skip_sync @pytest.mark.parametrize("param", tx_params) async def test_transaction_param_readonly_property(aconn, param): with pytest.raises(AttributeError): @@ -729,7 +729,7 @@ async def test_set_transaction_param_not_intrans_external(aconn, param): await getattr(aconn, f"set_{param.name}")(value) -@pytest.mark.skipif(is_async(__name__), reason="sync test only") +@skip_async @pytest.mark.crdb("skip", reason="transaction isolation") def test_set_transaction_param_all_property(conn): params: List[Any] = tx_params[:] @@ -777,7 +777,7 @@ async def test_set_transaction_param_strange(aconn): assert aconn.deferrable is False -@pytest.mark.skipif(is_async(__name__), reason="sync test only") +@skip_async def test_set_transaction_param_strange_property(conn): for val in ("asdf", 0, 5): with pytest.raises(ValueError):