]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
test(crdb): adapt cursor and error tests to CRDB
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 24 May 2022 23:38:30 +0000 (01:38 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 12 Jul 2022 11:58:34 +0000 (12:58 +0100)
tests/test_cursor.py
tests/test_cursor_async.py
tests/test_errors.py

index da0576ac3893f75b3462a57808c9322c3b6ece49..8ffa303011b31797811500d86e99edce40b3cb2e 100644 (file)
@@ -210,25 +210,28 @@ def test_fetchone(conn):
 
 
 def test_binary_cursor_execute(conn):
+    unk = "foo" if is_crdb(conn) else None
     cur = conn.cursor(binary=True)
-    cur.execute("select %s, %s", [1, None])
-    assert cur.fetchone() == (1, None)
+    cur.execute("select %s, %s", [1, unk])
+    assert cur.fetchone() == (1, unk)
     assert cur.pgresult.fformat(0) == 1
     assert cur.pgresult.get_value(0, 0) == b"\x00\x01"
 
 
 def test_execute_binary(conn):
     cur = conn.cursor()
-    cur.execute("select %s, %s", [1, None], binary=True)
-    assert cur.fetchone() == (1, None)
+    unk = "foo" if is_crdb(conn) else None
+    cur.execute("select %s, %s", [1, unk], binary=True)
+    assert cur.fetchone() == (1, unk)
     assert cur.pgresult.fformat(0) == 1
     assert cur.pgresult.get_value(0, 0) == b"\x00\x01"
 
 
 def test_binary_cursor_text_override(conn):
     cur = conn.cursor(binary=True)
-    cur.execute("select %s, %s", [1, None], binary=False)
-    assert cur.fetchone() == (1, None)
+    unk = "foo" if is_crdb(conn) else None
+    cur.execute("select %s, %s", [1, unk], binary=False)
+    assert cur.fetchone() == (1, unk)
     assert cur.pgresult.fformat(0) == 0
     assert cur.pgresult.get_value(0, 0) == b"1"
 
@@ -657,7 +660,7 @@ def test_stream_close(conn):
 def test_stream_binary_cursor(conn):
     cur = conn.cursor(binary=True)
     recs = []
-    for rec in cur.stream("select generate_series(1, 2)"):
+    for rec in cur.stream("select x::int4 from generate_series(1, 2) x"):
         recs.append(rec)
         assert cur.pgresult.fformat(0) == 1
         assert cur.pgresult.get_value(0, 0) == bytes([0, 0, 0, rec[0]])
@@ -668,7 +671,7 @@ def test_stream_binary_cursor(conn):
 def test_stream_execute_binary(conn):
     cur = conn.cursor()
     recs = []
-    for rec in cur.stream("select generate_series(1, 2)", binary=True):
+    for rec in cur.stream("select x::int4 from generate_series(1, 2) x", binary=True):
         recs.append(rec)
         assert cur.pgresult.fformat(0) == 1
         assert cur.pgresult.get_value(0, 0) == bytes([0, 0, 0, rec[0]])
index 6b3b5974e4f013bcc7c4c0d741db3779208cc77f..8b2b618f028dfadaa5f0553e5f5a4cb040691eda 100644 (file)
@@ -11,7 +11,7 @@ from psycopg.adapt import PyFormat
 from .utils import gc_collect
 from .test_cursor import my_row_factory
 from .test_cursor import execmany, _execmany  # noqa: F401
-from .fix_crdb import crdb_encoding
+from .fix_crdb import is_crdb, crdb_encoding
 
 execmany = execmany  # avoid F811 underneath
 pytestmark = pytest.mark.asyncio
@@ -212,25 +212,28 @@ async def test_fetchone(aconn):
 
 
 async def test_binary_cursor_execute(aconn):
+    unk = "foo" if is_crdb(aconn) else None
     cur = aconn.cursor(binary=True)
-    await cur.execute("select %s, %s", [1, None])
-    assert (await cur.fetchone()) == (1, None)
+    await cur.execute("select %s, %s", [1, unk])
+    assert (await cur.fetchone()) == (1, unk)
     assert cur.pgresult.fformat(0) == 1
     assert cur.pgresult.get_value(0, 0) == b"\x00\x01"
 
 
 async def test_execute_binary(aconn):
+    unk = "foo" if is_crdb(aconn) else None
     cur = aconn.cursor()
-    await cur.execute("select %s, %s", [1, None], binary=True)
-    assert (await cur.fetchone()) == (1, None)
+    await cur.execute("select %s, %s", [1, unk], binary=True)
+    assert (await cur.fetchone()) == (1, unk)
     assert cur.pgresult.fformat(0) == 1
     assert cur.pgresult.get_value(0, 0) == b"\x00\x01"
 
 
 async def test_binary_cursor_text_override(aconn):
+    unk = "foo" if is_crdb(aconn) else None
     cur = aconn.cursor(binary=True)
-    await cur.execute("select %s, %s", [1, None], binary=False)
-    assert (await cur.fetchone()) == (1, None)
+    await cur.execute("select %s, %s", [1, unk], binary=False)
+    assert (await cur.fetchone()) == (1, unk)
     assert cur.pgresult.fformat(0) == 0
     assert cur.pgresult.get_value(0, 0) == b"1"
 
@@ -595,6 +598,7 @@ async def test_stream_no_row(aconn):
     assert recs == []
 
 
+@pytest.mark.crdb("skip", reason="no col query")
 async def test_stream_no_col(aconn):
     cur = aconn.cursor()
     recs = [rec async for rec in cur.stream("select")]
@@ -649,7 +653,7 @@ async def test_stream_close(aconn):
 async def test_stream_binary_cursor(aconn):
     cur = aconn.cursor(binary=True)
     recs = []
-    async for rec in cur.stream("select generate_series(1, 2)"):
+    async for rec in cur.stream("select x::int4 from generate_series(1, 2) x"):
         recs.append(rec)
         assert cur.pgresult.fformat(0) == 1
         assert cur.pgresult.get_value(0, 0) == bytes([0, 0, 0, rec[0]])
@@ -660,7 +664,9 @@ async def test_stream_binary_cursor(aconn):
 async def test_stream_execute_binary(aconn):
     cur = aconn.cursor()
     recs = []
-    async for rec in cur.stream("select generate_series(1, 2)", binary=True):
+    async for rec in cur.stream(
+        "select x::int4 from generate_series(1, 2) x", binary=True
+    ):
         recs.append(rec)
         assert cur.pgresult.fformat(0) == 1
         assert cur.pgresult.get_value(0, 0) == bytes([0, 0, 0, rec[0]])
index 82a4dc8f065aa88f0116230ba766ab0a61685682..cb9ea145f661af469c5c903ee50ab74c26452669 100644 (file)
@@ -9,6 +9,7 @@ from psycopg import pq
 from psycopg import errors as e
 
 from .utils import eur, gc_collect
+from .fix_crdb import is_crdb
 
 
 def test_error_diag(conn):
@@ -19,7 +20,8 @@ def test_error_diag(conn):
     exc = excinfo.value
     diag = exc.diag
     assert diag.sqlstate == "42P01"
-    assert diag.severity_nonlocalized == "ERROR"
+    # https://github.com/cockroachdb/cockroach/issues/81794
+    assert (diag.severity_nonlocalized or diag.severity) == "ERROR"
 
 
 def test_diag_all_attrs(pgconn):
@@ -52,24 +54,26 @@ def test_diag_right_attr(pgconn, monkeypatch):
 
 
 def test_diag_attr_values(conn):
-    cur = conn.cursor()
-    cur.execute(
+    if is_crdb(conn):
+        conn.execute("set experimental_enable_temp_tables = 'on'")
+    conn.execute(
         """
         create temp table test_exc (
             data int constraint chk_eq1 check (data = 1)
         )"""
     )
     with pytest.raises(e.Error) as exc:
-        cur.execute("insert into test_exc values(2)")
+        conn.execute("insert into test_exc values(2)")
     diag = exc.value.diag
     assert diag.sqlstate == "23514"
-    assert diag.schema_name and diag.schema_name[:7] == "pg_temp"
-    assert diag.table_name == "test_exc"
     assert diag.constraint_name == "chk_eq1"
-    if conn.pgconn.server_version >= 90600:
+    if not is_crdb(conn):
+        assert diag.table_name == "test_exc"
+        assert diag.schema_name and diag.schema_name[:7] == "pg_temp"
         assert diag.severity_nonlocalized == "ERROR"
 
 
+@pytest.mark.crdb("skip", reason="do")
 @pytest.mark.parametrize("enc", ["utf8", "latin9"])
 def test_diag_encoding(conn, enc):
     msgs = []
@@ -81,6 +85,7 @@ def test_diag_encoding(conn, enc):
     assert msgs == [f"hello {eur}"]
 
 
+@pytest.mark.crdb("skip", reason="do")
 @pytest.mark.parametrize("enc", ["utf8", "latin9"])
 def test_error_encoding(conn, enc):
     with conn.transaction():
@@ -193,6 +198,7 @@ def test_diag_independent(conn):
     assert exc2.value.diag.sqlstate == "42P01"
 
 
+@pytest.mark.crdb("skip", reason="deferrable")
 def test_diag_from_commit(conn):
     cur = conn.cursor()
     cur.execute(
@@ -211,6 +217,7 @@ def test_diag_from_commit(conn):
 
 
 @pytest.mark.asyncio
+@pytest.mark.crdb("skip", reason="deferrable")
 async def test_diag_from_commit_async(aconn):
     cur = aconn.cursor()
     await cur.execute(
@@ -233,13 +240,15 @@ def test_query_context(conn):
         conn.execute("select * from wat")
 
     s = str(exc.value)
-    assert "from wat" in s, s
+    if not is_crdb(conn):
+        assert "from wat" in s, s
     assert exc.value.diag.message_primary
     assert exc.value.diag.message_primary in s
     assert "ERROR" not in s
     assert not s.endswith("\n")
 
 
+@pytest.mark.crdb("skip", reason="do")
 def test_unknown_sqlstate(conn):
     code = "PXX99"
     with pytest.raises(KeyError):