def test_send_prepare_types(pgconn): pgconn.send_prepare(b"prep", b"select $1 + $2", [23, 23]) (res, ) = psycopg3.waiting.wait(execute(pgconn)) assert res.status == pq.ExecStatus.COMMAND_OK, res.error_message pgconn.send_query_prepared(b"prep", [b"3", b"5"]) (res, ) = psycopg3.waiting.wait(execute(pgconn)) assert res.get_value(0, 0) == b"8"
def test_send_prepared_binary_out(pgconn, fmt, out): val = b"foo\00bar" pgconn.send_prepare(b"", b"select $1::bytea") (res, ) = psycopg3.waiting.wait(execute(pgconn)) assert res.status == pq.ExecStatus.COMMAND_OK, res.error_message pgconn.send_query_prepared(b"", [val], param_formats=[1], result_format=fmt) (res, ) = psycopg3.waiting.wait(execute(pgconn)) assert res.status == pq.ExecStatus.TUPLES_OK assert res.get_value(0, 0) == out
def test_send_prepare(pgconn): pgconn.send_prepare(b"prep", b"select $1::int + $2::int") (res, ) = psycopg3.waiting.wait(execute(pgconn)) assert res.status == pq.ExecStatus.COMMAND_OK, res.error_message pgconn.send_query_prepared(b"prep", [b"3", b"5"]) (res, ) = psycopg3.waiting.wait(execute(pgconn)) assert res.get_value(0, 0) == b"8" pgconn.finish() with pytest.raises(psycopg3.OperationalError): pgconn.send_prepare(b"prep", b"select $1::int + $2::int") with pytest.raises(psycopg3.OperationalError): pgconn.send_query_prepared(b"prep", [b"3", b"5"])
def test_send_prepared_binary_in(pgconn): val = b"foo\00bar" pgconn.send_prepare(b"", b"select length($1::bytea), length($2::bytea)") (res, ) = psycopg3.waiting.wait(execute(pgconn)) assert res.status == pq.ExecStatus.COMMAND_OK, res.error_message pgconn.send_query_prepared(b"", [val, val], param_formats=[0, 1]) (res, ) = psycopg3.waiting.wait(execute(pgconn)) assert res.status == pq.ExecStatus.TUPLES_OK assert res.get_value(0, 0) == b"3" assert res.get_value(0, 1) == b"7" with pytest.raises(ValueError): pgconn.exec_params(b"select $1::bytea", [val], param_formats=[1, 1])
async def test_wait_async_bad(pgconn): pgconn.send_query(b"select 1") gen = generators.execute(pgconn) socket = pgconn.socket pgconn.finish() with pytest.raises(psycopg3.OperationalError): await waiting.wait_async(gen, socket)
def test_send_query_params(pgconn): pgconn.send_query_params(b"select $1::int + $2", [b"5", b"3"]) (res, ) = psycopg3.waiting.wait(execute(pgconn)) assert res.status == pq.ExecStatus.TUPLES_OK assert res.get_value(0, 0) == b"8" pgconn.finish() with pytest.raises(psycopg3.OperationalError): pgconn.send_query_params(b"select $1", [b"1"])
def test_send_query_compact_test(pgconn): # Like the above test but use psycopg3 facilities for compactness pgconn.send_query(b"/* %s */ select pg_sleep(0.01); select 1 as foo;" % (b"x" * 1_000_000)) results = psycopg3.waiting.wait(execute(pgconn)) assert len(results) == 2 assert results[0].nfields == 1 assert results[0].fname(0) == b"pg_sleep" assert results[0].get_value(0, 0) == b"" assert results[1].nfields == 1 assert results[1].fname(0) == b"foo" assert results[1].get_value(0, 0) == b"1" pgconn.finish() with pytest.raises(psycopg3.OperationalError): pgconn.send_query(b"select 1")
def execute_wait(pgconn): return psycopg3.waiting.wait(execute(pgconn), pgconn.socket)
async def test_wait_async(pgconn): pgconn.send_query(b"select 1") gen = generators.execute(pgconn) (res, ) = await waiting.wait_async(gen, pgconn.socket) assert res.status == ExecStatus.TUPLES_OK
def test_wait_epoll(pgconn, timeout): pgconn.send_query(b"select 1") gen = generators.execute(pgconn) (res, ) = waiting.wait_epoll(gen, pgconn.socket, **timeout) assert res.status == ExecStatus.TUPLES_OK
def test_wait_selector_bad(pgconn): pgconn.send_query(b"select 1") gen = generators.execute(pgconn) pgconn.finish() with pytest.raises(psycopg3.OperationalError): waiting.wait_selector(gen, pgconn.socket)