From: Daniele Varrazzo Date: Mon, 6 Jun 2022 23:14:59 +0000 (+0200) Subject: fix(crdb): base context transaction tests on DML rather than DDL X-Git-Tag: 3.1~49^2~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6f075ae54dd4c4637c8ffce9c3e236624934f491;p=thirdparty%2Fpsycopg.git fix(crdb): base context transaction tests on DML rather than DDL On CRDB we may incur in problems caused by the table being dropped. --- diff --git a/tests/test_connection.py b/tests/test_connection.py index e83740028..42a32bbfe 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -115,30 +115,32 @@ def test_connection_warn_close(dsn, recwarn): assert not recwarn, [str(w.message) for w in recwarn.list] -def test_context_commit(conn, dsn): +@pytest.fixture +def testctx(svcconn): + svcconn.execute("create table if not exists testctx (id int primary key)") + svcconn.execute("delete from testctx") + return None + + +def test_context_commit(testctx, conn, dsn): with conn: with conn.cursor() as cur: - cur.execute("drop table if exists textctx") - cur.execute("create table textctx ()") + cur.execute("insert into testctx values (42)") assert conn.closed assert not conn.broken with psycopg.connect(dsn) as conn: with conn.cursor() as cur: - cur.execute("select * from textctx") - assert cur.fetchall() == [] + cur.execute("select * from testctx") + assert cur.fetchall() == [(42,)] -def test_context_rollback(conn, dsn): - with conn.cursor() as cur: - cur.execute("drop table if exists textctx") - conn.commit() - +def test_context_rollback(testctx, conn, dsn): with pytest.raises(ZeroDivisionError): with conn: with conn.cursor() as cur: - cur.execute("create table textctx ()") + cur.execute("insert into testctx values (42)") 1 / 0 assert conn.closed @@ -146,8 +148,8 @@ def test_context_rollback(conn, dsn): with psycopg.connect(dsn) as conn: with conn.cursor() as cur: - with pytest.raises(e.UndefinedTable): - cur.execute("select * from textctx") + cur.execute("select * from testctx") + assert cur.fetchall() == [] def test_context_close(conn): diff --git a/tests/test_connection_async.py b/tests/test_connection_async.py index 2539aecb7..bee4fb417 100644 --- a/tests/test_connection_async.py +++ b/tests/test_connection_async.py @@ -13,6 +13,7 @@ from .utils import gc_collect 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 +from .test_connection import testctx # noqa: F401 # fixture from .test_adapt import make_bin_dumper, make_dumper from .test_conninfo import fake_resolve # noqa: F401 @@ -124,30 +125,27 @@ async def test_connection_warn_close(dsn, recwarn): assert not recwarn, [str(w.message) for w in recwarn.list] +@pytest.mark.usefixtures("testctx") async def test_context_commit(aconn, dsn): async with aconn: async with aconn.cursor() as cur: - await cur.execute("drop table if exists textctx") - await cur.execute("create table textctx ()") + await cur.execute("insert into testctx values (42)") assert aconn.closed assert not aconn.broken async with await psycopg.AsyncConnection.connect(dsn) as aconn: async with aconn.cursor() as cur: - await cur.execute("select * from textctx") - assert await cur.fetchall() == [] + await cur.execute("select * from testctx") + assert await cur.fetchall() == [(42,)] +@pytest.mark.usefixtures("testctx") async def test_context_rollback(aconn, dsn): - async with aconn.cursor() as cur: - await cur.execute("drop table if exists textctx") - await aconn.commit() - with pytest.raises(ZeroDivisionError): async with aconn: async with aconn.cursor() as cur: - await cur.execute("create table textctx ()") + await cur.execute("insert into testctx values (42)") 1 / 0 assert aconn.closed @@ -155,8 +153,8 @@ async def test_context_rollback(aconn, dsn): async with await psycopg.AsyncConnection.connect(dsn) as aconn: async with aconn.cursor() as cur: - with pytest.raises(e.UndefinedTable): - await cur.execute("select * from textctx") + await cur.execute("select * from testctx") + assert await cur.fetchall() == [] async def test_context_close(aconn):