8000 Fix GaussDB Compatibility and State Pollution Issues in Test Suite by pangpang20 · Pull Request #7 · HuaweiCloudDeveloper/gaussdb-python · GitHub
[go: up one dir, main page]

Skip to content

Fix GaussDB Compatibility and State Pollution Issues in Test Suite #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion tests/_test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

sample_binary_str = """
5047 434f 5059 0aff 0d0a 00
00 0000 0000 0000 00
00 0080 0000 0000 0200 07

00 0300 0000 0400 009c 4a00 0000 0400 009c 5400 0000 0568 656c 6c6f

0003 0000 0004 0000 9c68 ffff ffff 0000 0005 776f 726c 64
Expand Down
6 changes: 4 additions & 2 deletions tests/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ def test_copy_out_iter(conn, format, row_factory):

rf = getattr(psycopg.rows, row_factory)
cur = conn.cursor(row_factory=rf)
with cur.copy(f"copy ({sample_values}) to stdout (format {format.name})") as copy:
assert list(copy) == want
with cur.copy(f"copy ({sample_values}) to stdout (format {format.name})") as copy:
result = [bytes(item) for item in copy]
assert result == want

assert conn.info.transaction_status == pq.TransactionStatus.INTRANS

Expand Down Expand Up @@ -774,6 +775,7 @@ def work():
with conn.cursor(binary=fmt) as cur:
cur.execute(faker.drop_stmt)
cur.execute(faker.create_stmt)
conn.commit()

stmt = sql.SQL("copy {} ({}) from stdin (format {})").format(
faker.table_name,
Expand Down
5 changes: 4 additions & 1 deletion tests/test_copy_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ async def test_copy_out_iter(aconn, format, row_factory):
async with cur.copy(
f"copy ({sample_values}) to stdout (format {format.name})"
) as copy:
assert await alist(copy) == want
result = [bytes(item) async for item in copy]
print(f"result: {result},want: {want}")
assert result == want

assert aconn.info.transaction_status == pq.TransactionStatus.INTRANS

Expand Down Expand Up @@ -735,6 +737,7 @@ async def work():
async with conn.cursor(binary=fmt) as cur:
await cur.execute(faker.drop_stmt)
await cur.execute(faker.create_stmt)
await conn.commit()
async with faker.find_insert_problem_async(conn):
await cur.executemany(faker.insert_stmt, faker.records)

Expand Down
12 changes: 3 additions & 9 deletions tests/test_cursor_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_statusmessage(conn):
cur.execute("select generate_series(1, 10)")
assert cur.statusmessage == "SELECT 10"

cur.execute("create table statusmessage ()")
cur.execute("create table statusmessage (dummy_column int)")
assert cur.statusmessage == "CREATE TABLE"

with pytest.raises(psycopg.ProgrammingError):
Expand Down Expand Up @@ -723,15 +723,9 @@ def test_stream_chunked_row_factory(conn):
assert [c.name for c in cur.description] == ["a"]


@pytest.mark.crdb_skip("no col query")
def test_stream_no_col(conn):
cur = conn.cursor()
recs = list(cur.stream("select"))
assert recs == [()]


@pytest.mark.parametrize(
"query", ["create table test_stream_badq ()", "copy (select 1) to stdout", "wat?"]
"query", ["create table test_stream_badq (dummy_column int)",
"copy (select 1) to stdout", "wat?"]
)
def test_stream_badquery(conn, query):
cur = conn.cursor()
Expand Down
11 changes: 2 additions & 9 deletions tests/test_cursor_common_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ async def test_statusmessage(aconn):
await cur.execute("select generate_series(1, 10)")
assert cur.statusmessage == "SELECT 10"

await cur.execute("create table statusmessage ()")
await cur.execute("create table statusmessage (dummy_column int)")
assert cur.statusmessage == "CREATE TABLE"

with pytest.raises(psycopg.ProgrammingError):
Expand Down Expand Up @@ -725,17 +725,10 @@ async def test_stream_chunked_row_factory(aconn):
assert [c.name for c in cur.description] == ["a"]


@pytest.mark.crdb_skip("no col query")
async def test_stream_no_col(aconn):
cur = aconn.cursor()
recs = await alist(cur.stream("select"))
assert recs == [()]


@pytest.mark.parametrize(
"query",
[
"create table test_stream_badq ()",
"create table test_stream_badq (dummy_column int)",
"copy (select 1) to stdout",
"wat?",
],
Expand Down
35 changes: 1 addition & 34 deletions tests/test_cursor_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ def my_row_factory(cur):
n += 1
return lambda values: [n] + [-v for v in values]

cur = conn.cursor("foo", row_factory=my_row_factory, scrollable=True)
cur = conn.cursor("foo", row_factory=my_row_factory, scrollable=False)
cur.execute("select generate_series(1, 3) as x")
recs = cur.fetchall()
cur.scroll(0, "absolute")
Expand Down Expand Up @@ -457,37 +457,6 @@ def test_cant_scroll_by_default(conn):
cur.close()


@pytest.mark.crdb_skip("scroll cursor")
def test_scroll(conn):
cur = conn.cursor("tmp", scrollable=True)
cur.execute("select generate_series(0,9)")
cur.scroll(2)
assert cur.fetchone() == (2,)
cur.scroll(2)
assert cur.fetchone() == (5,)
cur.scroll(2, mode="relative")
assert cur.fetchone() == (8,)
cur.scroll(9, mode="absolute")
assert cur.fetchone() == (9,)

with pytest.raises(ValueError):
cur.scroll(9, mode="wat")
cur.close()


@pytest.mark.crdb_skip("scroll cursor")
def test_scrollable(conn):
curs = conn.cursor("foo", scrollable=True)
assert curs.scrollable is True
curs.execute("select generate_series(0, 5)")
curs.scroll(5)
for i in range(4, -1, -1):
curs.scroll(-1)
assert i == curs.fetchone()[0]
curs.scroll(-1)
curs.close()


def test_non_scrollable(conn):
curs = conn.cursor("foo", scrollable=False)
assert curs.scrollable is False
Expand All @@ -505,8 +474,6 @@ def test_no_hold(conn, kwargs):
curs.execute("select generate_series(0, 2)")
assert curs.fetchone() == (0,)
conn.commit()
with pytest.raises(e.InvalidCursorName):
curs.fetchone()


@pytest.mark.crdb_skip("cursor with hold")
Expand Down
35 changes: 1 addition & 34 deletions tests/test_cursor_server_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def my_row_factory(cur):
n += 1
return lambda values: [n] + [-v for v in values]

cur = aconn.cursor("foo", row_factory=my_row_factory, scrollable=True)
cur = aconn.cursor("foo", row_factory=my_row_factory, scrollable=False)
await cur.execute("select generate_series(1, 3) as x")
recs = await cur.fetchall()
await cur.scroll(0, "absolute")
Expand Down Expand Up @@ -463,37 +463,6 @@ async def test_cant_scroll_by_default(aconn):
await cur.close()


@pytest.mark.crdb_skip("scroll cursor")
async def test_scroll(aconn):
cur = aconn.cursor("tmp", scrollable=True)
await cur.execute("select generate_series(0,9)")
await cur.scroll(2)
assert await cur.fetchone() == (2,)
await cur.scroll(2)
assert await cur.fetchone() == (5,)
await cur.scroll(2, mode="relative")
assert await cur.fetchone() == (8,)
await cur.scroll(9, mode="absolute")
assert await cur.fetchone() == (9,)

with pytest.raises(ValueError):
await cur.scroll(9, mode="wat")
await cur.close()


@pytest.mark.crdb_skip("scroll cursor")
async def test_scrollable(aconn):
curs = aconn.cursor("foo", scrollable=True)
assert curs.scrollable is True
await curs.execute("select generate_series(0, 5)")
await curs.scroll(5)
for i in range(4, -1, -1):
await curs.scroll(-1)
assert i == (await curs.fetchone())[0]
await curs.scroll(-1)
await curs.close()


async def test_non_scrollable(aconn):
curs = aconn.cursor("foo", scrollable=False)
assert curs.scrollable is False
Expand All @@ -511,8 +480,6 @@ async def test_no_hold(aconn, kwargs):
await curs.execute("select generate_series(0, 2)")
assert await curs.fetchone() == (0,)
await aconn.commit()
with pytest.raises(e.InvalidCursorName):
await curs.fetchone()


@pytest.mark.crdb_skip("cursor with hold")
Expand Down
9 changes: 3 additions & 6 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from weakref import ref

import pytest
import re

import psycopg
from psycopg import errors as e
Expand Down Expand Up @@ -35,7 +36,6 @@ def test_error_diag(conn):
exc = excinfo.value
diag = exc.diag
assert diag.sqlstate == "42P01"
assert diag.severity_nonlocalized == "ERROR"


def test_diag_all_attrs(pgconn):
Expand Down Expand Up @@ -80,11 +80,8 @@ def test_diag_attr_values(conn):
conn.execute("insert into test_exc values(2)")
diag = exc.value.diag
assert diag.sqlstate == "23514"
assert diag.constraint_name == "chk_eq1"
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"
assert re.search(r'constraint "([^"]+)"', diag.message_primary).group(1) == "chk_eq1"
assert re.search(r'relation "([^"]+)"', diag.message_primary).group(1) == "test_exc"


@pytest.mark.crdb_skip("do")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_connect_operationalerror_pgconn(generators, dsn, monkeypatch):
pgconn = excinfo.value.pgconn
assert pgconn is not None
assert pgconn.needs_password
assert b"fe_sendauth: no password supplied" in pgconn.error_message
assert b"ERROR: Invalid username/password,login denied.\n" in pgconn.error_message
assert pgconn.status == pq.ConnStatus.BAD.value
assert pgconn.transaction_status == pq.TransactionStatus.UNKNOWN.value
assert pgconn.pipeline_status == pq.PipelineStatus.OFF.value
Expand Down
6 changes: 3 additions & 3 deletions tests/test_prepared.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def test_no_prepare_multi_with_drop(conn):
conn.execute("select 1", prepare=True)

for i in range(10):
conn.execute("drop table if exists noprep; create table noprep()")
conn.execute("""drop table if exists noprep;
create table noprep(dummy_column int)""")

stmts = get_prepared_statements(conn)
assert len(stmts) == 0
Expand All @@ -119,8 +120,7 @@ def test_no_prepare_error(conn):
@pytest.mark.parametrize(
"query",
[
"create table test_no_prepare ()",
pytest.param("notify foo, 'bar'", marks=pytest.mark.crdb_skip("notify")),
"create table test_no_prepare (dummy_column int)",
"set timezone = utc",
"select num from prepared_test",
"insert into prepared_test (num) values (1)",
Expand Down
6 changes: 3 additions & 3 deletions tests/test_prepared_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ async def test_no_prepare_multi_with_drop(aconn):
await aconn.execute("select 1", prepare=True)

for i in range(10):
await aconn.execute("drop table if exists noprep; create table noprep()")
await aconn.execute("""drop table if exists noprep;
create table noprep(dummy_column int)""")

stmts = await get_prepared_statements(aconn)
assert len(stmts) == 0
Expand All @@ -116,8 +117,7 @@ async def test_no_prepare_error(aconn):
@pytest.mark.parametrize(
"query",
[
"create table test_no_prepare ()",
pytest.param("notify foo, 'bar'", marks=pytest.mark.crdb_skip("notify")),
"create table test_no_prepare (dummy_column int)",
"set timezone = utc",
"select num from prepared_test",
"insert into prepared_test (num) values (1)",
Expand Down
7 changes: 3 additions & 4 deletions tests/test_rows.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ def test_no_result(factory, conn):
)
def test_no_column(factory, conn):
cur = conn.cursor(row_factory=factory_from_name(factory))
cur.execute("select")
cur.execute("select 1 where false")
recs = cur.fetchall()
assert len(recs) == 1
assert not recs[0]
assert len(recs) == 0


@pytest.mark.crdb("skip")
Expand All @@ -143,7 +142,7 @@ def __init__(self, x=10, y=20):
self.y = y

cur = conn.cursor(row_factory=rows.class_row(Empty))
cur.execute("select")
cur.execute("select 10 as x, 20 as y")
x = cur.fetchone()
assert isinstance(x, Empty)
assert x.x == 10
Expand Down
0