From: Daniele Varrazzo Date: Tue, 15 Aug 2023 18:29:34 +0000 (+0100) Subject: refactor(tests): make transaction sync/async tests more uniform X-Git-Tag: pool-3.2.0~12^2~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0a73c9fca5a2096ac2036dabd1fad1614996cd79;p=thirdparty%2Fpsycopg.git refactor(tests): make transaction sync/async tests more uniform --- diff --git a/tests/_test_transaction.py b/tests/_test_transaction.py new file mode 100644 index 000000000..005e48cca --- /dev/null +++ b/tests/_test_transaction.py @@ -0,0 +1,72 @@ +import sys +import pytest +import psycopg + + +# TODOCRDB: is this the expected behaviour? +crdb_skip_external_observer = pytest.mark.crdb( + "skip", reason="deadlock on observer connection" +) + + +@pytest.fixture(autouse=True) +def create_test_table(svcconn): + """Creates a table called 'test_table' for use in tests.""" + cur = svcconn.cursor() + cur.execute("drop table if exists test_table") + cur.execute("create table test_table (id text primary key)") + yield + cur.execute("drop table test_table") + + +def insert_row(conn, value): + sql = "INSERT INTO test_table VALUES (%s)" + if isinstance(conn, psycopg.Connection): + conn.cursor().execute(sql, (value,)) + else: + + async def f(): + cur = conn.cursor() + await cur.execute(sql, (value,)) + + return f() + + +def inserted(conn): + """Return the values inserted in the test table.""" + sql = "SELECT * FROM test_table" + if isinstance(conn, psycopg.Connection): + rows = conn.cursor().execute(sql).fetchall() + return set(v for (v,) in rows) + else: + + async def f(): + cur = conn.cursor() + await cur.execute(sql) + rows = await cur.fetchall() + return set(v for (v,) in rows) + + return f() + + +def in_transaction(conn): + if conn.pgconn.transaction_status == conn.TransactionStatus.IDLE: + return False + elif conn.pgconn.transaction_status == conn.TransactionStatus.INTRANS: + return True + else: + assert False, conn.pgconn.transaction_status + + +def get_exc_info(exc): + """Return the exc info for an exception or a success if exc is None""" + if not exc: + return (None,) * 3 + try: + raise exc + except exc: + return sys.exc_info() + + +class ExpectedException(Exception): + pass diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 9391e00cf..d1fdcae61 100644 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -1,17 +1,14 @@ -import sys import logging from threading import Thread, Event import pytest -import psycopg from psycopg import Rollback from psycopg import errors as e -# TODOCRDB: is this the expected behaviour? -crdb_skip_external_observer = pytest.mark.crdb( - "skip", reason="deadlock on observer connection" -) +from ._test_transaction import in_transaction, insert_row, inserted, get_exc_info +from ._test_transaction import ExpectedException, crdb_skip_external_observer +from ._test_transaction import create_test_table # noqa # autouse fixture @pytest.fixture @@ -19,69 +16,6 @@ def conn(conn, pipeline): return conn -@pytest.fixture(autouse=True) -def create_test_table(svcconn): - """Creates a table called 'test_table' for use in tests.""" - cur = svcconn.cursor() - cur.execute("drop table if exists test_table") - cur.execute("create table test_table (id text primary key)") - yield - cur.execute("drop table test_table") - - -def insert_row(conn, value): - sql = "INSERT INTO test_table VALUES (%s)" - if isinstance(conn, psycopg.Connection): - conn.cursor().execute(sql, (value,)) - else: - - async def f(): - cur = conn.cursor() - await cur.execute(sql, (value,)) - - return f() - - -def inserted(conn): - """Return the values inserted in the test table.""" - sql = "SELECT * FROM test_table" - if isinstance(conn, psycopg.Connection): - rows = conn.cursor().execute(sql).fetchall() - return set(v for (v,) in rows) - else: - - async def f(): - cur = conn.cursor() - await cur.execute(sql) - rows = await cur.fetchall() - return set(v for (v,) in rows) - - return f() - - -def in_transaction(conn): - if conn.pgconn.transaction_status == conn.TransactionStatus.IDLE: - return False - elif conn.pgconn.transaction_status == conn.TransactionStatus.INTRANS: - return True - else: - assert False, conn.pgconn.transaction_status - - -def get_exc_info(exc): - """Return the exc info for an exception or a success if exc is None""" - if not exc: - return (None,) * 3 - try: - raise exc - except exc: - return sys.exc_info() - - -class ExpectedException(Exception): - pass - - def test_basic(conn, pipeline): """Basic use of transaction() to BEGIN and COMMIT a transaction.""" assert not in_transaction(conn) diff --git a/tests/test_transaction_async.py b/tests/test_transaction_async.py index abe5bf063..c72aa8cfe 100644 --- a/tests/test_transaction_async.py +++ b/tests/test_transaction_async.py @@ -6,9 +6,9 @@ import pytest from psycopg import Rollback from psycopg import errors as e -from .test_transaction import in_transaction, insert_row, inserted, get_exc_info -from .test_transaction import ExpectedException, crdb_skip_external_observer -from .test_transaction import create_test_table # noqa # autouse fixture +from ._test_transaction import in_transaction, insert_row, inserted, get_exc_info +from ._test_transaction import ExpectedException, crdb_skip_external_observer +from ._test_transaction import create_test_table # noqa # autouse fixture @pytest.fixture