From 47821540b2cbfc9150ebbccd8cf959cc41de2c2d Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 12 Feb 2021 03:35:04 +0100 Subject: [PATCH] Add tests to verify broken queries or other statements in server-side cursors Enforce passing a single statement by making sure to use the advanced query protocol. --- psycopg3/psycopg3/server_cursor.py | 2 +- tests/test_server_cursor.py | 9 +++++++++ tests/test_server_cursor_async.py | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/psycopg3/psycopg3/server_cursor.py b/psycopg3/psycopg3/server_cursor.py index 058a03921..cadd2ca7d 100644 --- a/psycopg3/psycopg3/server_cursor.py +++ b/psycopg3/psycopg3/server_cursor.py @@ -61,7 +61,7 @@ class ServerCursorHelper(Generic[ConnectionType]): yield from cur._start_query(query) pgq = cur._convert_query(query, params) - cur._execute_send(pgq) + cur._execute_send(pgq, no_pqexec=True) results = yield from execute(conn.pgconn) cur._execute_results(results) diff --git a/tests/test_server_cursor.py b/tests/test_server_cursor.py index 551579e95..29cb2921c 100644 --- a/tests/test_server_cursor.py +++ b/tests/test_server_cursor.py @@ -108,6 +108,15 @@ def test_execute_reuse(conn): assert cur.description[1].name == "baz" +@pytest.mark.parametrize( + "stmt", ["", "wat", "create table ssc ()", "select 1; select 2"] +) +def test_execute_error(conn, stmt): + cur = conn.cursor("foo") + with pytest.raises(e.ProgrammingError): + cur.execute(stmt) + + def test_executemany(conn): cur = conn.cursor("foo") with pytest.raises(e.NotSupportedError): diff --git a/tests/test_server_cursor_async.py b/tests/test_server_cursor_async.py index fa2d9425c..d7d4805ac 100644 --- a/tests/test_server_cursor_async.py +++ b/tests/test_server_cursor_async.py @@ -110,6 +110,15 @@ async def test_execute_reuse(aconn): assert cur.description[1].name == "baz" +@pytest.mark.parametrize( + "stmt", ["", "wat", "create table ssc ()", "select 1; select 2"] +) +async def test_execute_error(aconn, stmt): + cur = aconn.cursor("foo") + with pytest.raises(e.ProgrammingError): + await cur.execute(stmt) + + async def test_executemany(aconn): cur = aconn.cursor("foo") with pytest.raises(e.NotSupportedError): -- 2.47.2