]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
refactor(test): uniform the way sync/async-only tests are skipped
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 8 Oct 2023 13:55:31 +0000 (15:55 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 11 Oct 2023 21:45:39 +0000 (23:45 +0200)
Add @skip_sync, @skip_async markers to mark the functions to skip.

tests/acompat.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_connection.py
tests/test_connection_async.py

index 256e29f7c84cf06ac6c646c20d061eac7a6d4147..41c2f40b4712ea22dd6208031e97d427fdfef3e7 100644 (file)
@@ -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)"""
index 7d96d3f3d910999a29654d995f41784d865a80c6..9ab1b01d22226073fb2eb8792ab532d16e975c35 100644 (file)
@@ -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
 
index be018f2b6afa721b8773a10336f65ba655573eb2..27b55bbe1dd26f2a382a0bf8b0f67234ae606073 100644 (file)
@@ -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
 
index e80550c3233d7f19827fe1c206491bed8150de33..ce60ea795d4c5cc66e5dddeceed420da5684e2aa 100644 (file)
@@ -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
 
index 6d1b9fe1b836e41c3965e22d11c90de349711c9f..ff6dcc0bc738eaa9061aa47eb5a52d028a359092 100644 (file)
@@ -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
 
index 3a28beac3064aed5086b3450c3c99631899aebde..c9af4f8de8e83f262cd26ef5c4a9aa39d95cdbe4 100644 (file)
@@ -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
 
index 106437eb5a9f8ed5d250a42cfd958a6153d1a316..0efd61bd7b5ee699725cf04bc76599a3d6e467d9 100644 (file)
@@ -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
 
index dd27e6e396468aa22b59944bbbdd930a18da250c..dcc38a2bc7419885708a217f0282765780f509c5 100644 (file)
@@ -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):
index b68e36a262111e4be2f6bc4496c11ccafcc0b94c..36a6c9f9feb354985f9f0857bebf3a7ca6355389 100644 (file)
@@ -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):