1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
|
# WARNING: this file is auto-generated by 'async_to_sync.py'
# from the original file 'test_cursor_common_async.py'
# DO NOT CHANGE! Change the original file instead.
"""
Tests common to psycopg.Cursor and its subclasses.
"""
import weakref
import datetime as dt
from typing import Any
from contextlib import closing
import pytest
from packaging.version import parse as ver
import psycopg
from psycopg import pq, rows, sql
from psycopg.adapt import PyFormat
from psycopg.types import TypeInfo
from . import _test_cursor
from .utils import raiseif
from .acompat import gather, spawn
from .fix_crdb import crdb_encoding
from .test_adapt import make_loader
from ._test_cursor import my_row_factory, ph
execmany = _test_cursor.execmany # avoid F811 underneath
_execmany = _test_cursor._execmany # needed by the execmany fixture
cursor_classes = [psycopg.Cursor, psycopg.ClientCursor]
# Allow to import (not necessarily to run) the module with psycopg 3.1.
# Needed to test psycopg_pool 3.2 tests with psycopg 3.1 imported, i.e. to run
# `pytest -m pool`. (which might happen when releasing pool packages).
if ver(psycopg.__version__) >= ver("3.2.0.dev0"):
cursor_classes.append(psycopg.RawCursor)
@pytest.fixture(params=cursor_classes)
def conn(conn, request, anyio_backend):
conn.cursor_factory = request.param
return conn
def test_init(conn):
cur = conn.cursor_factory(conn)
cur.execute("select 1")
assert cur.fetchone() == (1,)
conn.row_factory = rows.dict_row
cur = conn.cursor_factory(conn)
cur.execute("select 1 as a")
assert cur.fetchone() == {"a": 1}
def test_init_factory(conn):
cur = conn.cursor_factory(conn, row_factory=rows.dict_row)
cur.execute("select 1 as a")
assert cur.fetchone() == {"a": 1}
def test_close(conn):
cur = conn.cursor()
assert not cur.closed
cur.close()
assert cur.closed
with pytest.raises(psycopg.InterfaceError):
cur.execute("select 'foo'")
cur.close()
assert cur.closed
def test_cursor_close_fetchone(conn):
cur = conn.cursor()
assert not cur.closed
query = "select * from generate_series(1, 10)"
cur.execute(query)
for _ in range(5):
cur.fetchone()
cur.close()
assert cur.closed
with pytest.raises(psycopg.InterfaceError):
cur.fetchone()
def test_cursor_close_fetchmany(conn):
cur = conn.cursor()
assert not cur.closed
query = "select * from generate_series(1, 10)"
cur.execute(query)
assert len(cur.fetchmany(2)) == 2
cur.close()
assert cur.closed
with pytest.raises(psycopg.InterfaceError):
cur.fetchmany(2)
def test_cursor_close_fetchall(conn):
cur = conn.cursor()
assert not cur.closed
query = "select * from generate_series(1, 10)"
cur.execute(query)
assert len(cur.fetchall()) == 10
cur.close()
assert cur.closed
with pytest.raises(psycopg.InterfaceError):
cur.fetchall()
def test_context(conn):
with conn.cursor() as cur:
assert not cur.closed
assert cur.closed
@pytest.mark.slow
def test_weakref(conn, gc_collect):
cur = conn.cursor()
w = weakref.ref(cur)
cur.close()
del cur
gc_collect()
assert w() is None
def test_pgresult(conn):
cur = conn.cursor()
cur.execute("select 1")
assert cur.pgresult
cur.close()
assert not cur.pgresult
def test_statusmessage(conn):
cur = conn.cursor()
assert cur.statusmessage is None
cur.execute("select generate_series(1, 10)")
assert cur.statusmessage == "SELECT 10"
cur.execute("create table statusmessage ()")
assert cur.statusmessage == "CREATE TABLE"
with pytest.raises(psycopg.ProgrammingError):
cur.execute("wat")
assert cur.statusmessage is None
def test_execute_sql(conn):
cur = conn.cursor()
cur.execute(sql.SQL("select {value}").format(value="hello"))
assert cur.fetchone() == ("hello",)
def test_next(conn):
cur = conn.cursor()
cur.execute("select 1")
assert next(cur) == (1,)
with pytest.raises(StopIteration):
next(cur)
def test_query_parse_cache_size(conn):
cur = conn.cursor()
cls = type(cur)
# Warning: testing internal structures. Test might need refactoring with the code.
cache: Any
if cls is psycopg.Cursor:
cache = psycopg._queries._query2pg
elif cls is psycopg.ClientCursor:
cache = psycopg._queries._query2pg_client
elif cls is psycopg.RawCursor:
pytest.skip("RawCursor has no query parse cache")
else:
assert False, cls
cache.cache_clear()
ci = cache.cache_info()
h0, m0 = (ci.hits, ci.misses)
tests = [
(f"select 1 -- {'x' * 3500}", (), h0, m0 + 1),
(f"select 1 -- {'x' * 3500}", (), h0 + 1, m0 + 1),
(f"select 1 -- {'x' * 4500}", (), h0 + 1, m0 + 1),
(f"select 1 -- {'x' * 4500}", (), h0 + 1, m0 + 1),
(f"select 1 -- {'%s' * 40}", ("x",) * 40, h0 + 1, m0 + 2),
(f"select 1 -- {'%s' * 40}", ("x",) * 40, h0 + 2, m0 + 2),
(f"select 1 -- {'%s' * 60}", ("x",) * 60, h0 + 2, m0 + 2),
(f"select 1 -- {'%s' * 60}", ("x",) * 60, h0 + 2, m0 + 2),
]
for i, (query, params, hits, misses) in enumerate(tests):
pq = cur._query_cls(psycopg.adapt.Transformer())
pq.convert(query, params)
ci = cache.cache_info()
assert ci.hits == hits, f"at {i}"
assert ci.misses == misses, f"at {i}"
def test_execute_many_results(conn):
cur = conn.cursor()
assert cur.nextset() is None
rv = cur.execute("select 'foo'; select generate_series(1,3)")
assert rv is cur
assert cur.fetchall() == [("foo",)]
assert cur.rowcount == 1
assert cur.nextset()
assert cur.fetchall() == [(1,), (2,), (3,)]
assert cur.rowcount == 3
assert cur.nextset() is None
cur.close()
assert cur.nextset() is None
def test_set_results(conn):
cur = conn.cursor()
with pytest.raises(IndexError):
cur.set_result(0)
cur.execute("select 'foo'; select generate_series(1,3)")
assert cur.set_result(0) is cur
assert cur.fetchall() == [("foo",)]
assert cur.rowcount == 1
assert cur.set_result(-1) is cur
assert cur.fetchall() == [(1,), (2,), (3,)]
assert cur.rowcount == 3
with pytest.raises(IndexError):
cur.set_result(2)
with pytest.raises(IndexError):
cur.set_result(-3)
def test_execute_sequence(conn):
cur = conn.cursor()
rv = cur.execute(ph(cur, "select %s::int, %s::text, %s::text"), [1, "foo", None])
assert rv is cur
assert len(cur._results) == 1
assert cur.pgresult.get_value(0, 0) == b"1"
assert cur.pgresult.get_value(0, 1) == b"foo"
assert cur.pgresult.get_value(0, 2) is None
assert cur.nextset() is None
@pytest.mark.parametrize("query", ["", " ", ";"])
def test_execute_empty_query(conn, query):
cur = conn.cursor()
cur.execute(query)
assert cur.pgresult.status == pq.ExecStatus.EMPTY_QUERY
with pytest.raises(psycopg.ProgrammingError):
cur.fetchone()
def test_execute_type_change(conn):
# issue #112
conn.execute("create table bug_112 (num integer)")
cur = conn.cursor()
sql = ph(cur, "insert into bug_112 (num) values (%s)")
cur.execute(sql, (1,))
cur.execute(sql, (100000,))
cur.execute("select num from bug_112 order by num")
assert cur.fetchall() == [(1,), (100000,)]
def test_executemany_type_change(conn):
conn.execute("create table bug_112 (num integer)")
cur = conn.cursor()
sql = ph(cur, "insert into bug_112 (num) values (%s)")
cur.executemany(sql, [(1,), (100000,)])
cur.execute("select num from bug_112 order by num")
assert cur.fetchall() == [(1,), (100000,)]
@pytest.mark.parametrize(
"query", ["copy testcopy from stdin", "copy testcopy to stdout"]
)
def test_execute_copy(conn, query):
cur = conn.cursor()
cur.execute("create table testcopy (id int)")
with pytest.raises(psycopg.ProgrammingError):
cur.execute(query)
def test_fetchone(conn):
cur = conn.cursor()
cur.execute(ph(cur, "select %s::int, %s::text, %s::text"), [1, "foo", None])
assert cur.pgresult.fformat(0) == 0
row = cur.fetchone()
assert row == (1, "foo", None)
row = cur.fetchone()
assert row is None
def test_binary_cursor_execute(conn):
with raiseif(
conn.cursor_factory is psycopg.ClientCursor, psycopg.NotSupportedError
) as ex:
cur = conn.cursor(binary=True)
cur.execute(ph(cur, "select %s, %s"), [1, None])
if ex:
return
assert cur.fetchone() == (1, None)
assert cur.pgresult.fformat(0) == 1
assert cur.pgresult.get_value(0, 0) == b"\x00\x01"
def test_execute_binary(conn):
cur = conn.cursor()
with raiseif(
conn.cursor_factory is psycopg.ClientCursor, psycopg.NotSupportedError
) as ex:
cur.execute(ph(cur, "select %s, %s"), [1, None], binary=True)
if ex:
return
assert cur.fetchone() == (1, None)
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(ph(cur, "select %s, %s"), [1, None], binary=False)
assert cur.fetchone() == (1, None)
assert cur.pgresult.fformat(0) == 0
assert cur.pgresult.get_value(0, 0) == b"1"
@pytest.mark.parametrize("encoding", ["utf8", crdb_encoding("latin9")])
def test_query_encode(conn, encoding):
conn.execute(f"set client_encoding to {encoding}")
cur = conn.cursor()
cur.execute("select '€'")
(res,) = cur.fetchone()
assert res == "€"
@pytest.mark.parametrize("encoding", [crdb_encoding("latin1")])
def test_query_badenc(conn, encoding):
conn.execute(f"set client_encoding to {encoding}")
cur = conn.cursor()
with pytest.raises(UnicodeEncodeError):
cur.execute("select '€'")
def test_executemany(conn, execmany):
cur = conn.cursor()
cur.executemany(
ph(cur, "insert into execmany(num, data) values (%s, %s)"),
[(10, "hello"), (20, "world")],
)
cur.execute("select num, data from execmany order by 1")
rv = cur.fetchall()
assert rv == [(10, "hello"), (20, "world")]
def test_executemany_name(conn, execmany):
cur = conn.cursor()
cur.executemany(
ph(cur, "insert into execmany(num, data) values (%(num)s, %(data)s)"),
[{"num": 11, "data": "hello", "x": 1}, {"num": 21, "data": "world"}],
)
cur.execute("select num, data from execmany order by 1")
rv = cur.fetchall()
assert rv == [(11, "hello"), (21, "world")]
def test_executemany_no_data(conn, execmany):
cur = conn.cursor()
cur.executemany(ph(cur, "insert into execmany(num, data) values (%s, %s)"), [])
assert cur.rowcount == 0
def test_executemany_rowcount(conn, execmany):
cur = conn.cursor()
cur.executemany(
ph(cur, "insert into execmany(num, data) values (%s, %s)"),
[(10, "hello"), (20, "world")],
)
assert cur.rowcount == 2
def test_executemany_returning(conn, execmany):
cur = conn.cursor()
cur.executemany(
ph(cur, "insert into execmany(num, data) values (%s, %s) returning num"),
[(10, "hello"), (20, "world")],
returning=True,
)
assert cur.rowcount == 1
assert cur.fetchone() == (10,)
assert cur.nextset()
assert cur.rowcount == 1
assert cur.fetchone() == (20,)
assert cur.nextset() is None
def test_executemany_returning_discard(conn, execmany):
cur = conn.cursor()
cur.executemany(
ph(cur, "insert into execmany(num, data) values (%s, %s) returning num"),
[(10, "hello"), (20, "world")],
)
assert cur.rowcount == 2
with pytest.raises(psycopg.ProgrammingError):
cur.fetchone()
assert cur.nextset() is None
def test_executemany_no_result(conn, execmany):
cur = conn.cursor()
cur.executemany(
ph(cur, "insert into execmany(num, data) values (%s, %s)"),
[(10, "hello"), (20, "world")],
returning=True,
)
assert cur.rowcount == 1
assert cur.statusmessage.startswith("INSERT")
with pytest.raises(psycopg.ProgrammingError):
cur.fetchone()
pgresult = cur.pgresult
assert cur.nextset()
assert cur.rowcount == 1
assert cur.statusmessage.startswith("INSERT")
assert pgresult is not cur.pgresult
assert cur.nextset() is None
def test_executemany_rowcount_no_hit(conn, execmany):
cur = conn.cursor()
cur.executemany(ph(cur, "delete from execmany where id = %s"), [(-1,), (-2,)])
assert cur.rowcount == 0
cur.executemany(ph(cur, "delete from execmany where id = %s"), [])
assert cur.rowcount == 0
cur.executemany(
ph(cur, "delete from execmany where id = %s returning num"), [(-1,), (-2,)]
)
assert cur.rowcount == 0
@pytest.mark.parametrize(
"query",
[
"insert into nosuchtable values (%s, %s)",
"copy (select %s, %s) to stdout",
"wat (%s, %s)",
],
)
def test_executemany_badquery(conn, query):
cur = conn.cursor()
with pytest.raises(psycopg.DatabaseError):
cur.executemany(ph(cur, query), [(10, "hello"), (20, "world")])
@pytest.mark.parametrize("fmt_in", PyFormat)
def test_executemany_null_first(conn, fmt_in):
cur = conn.cursor()
cur.execute("create table testmany (a bigint, b bigint)")
cur.executemany(
ph(cur, f"insert into testmany values (%{fmt_in.value}, %{fmt_in.value})"),
[[1, None], [3, 4]],
)
with pytest.raises((psycopg.DataError, psycopg.ProgrammingError)):
cur.executemany(
ph(cur, f"insert into testmany values (%{fmt_in.value}, %{fmt_in.value})"),
[[1, ""], [3, 4]],
)
@pytest.mark.slow
def test_executemany_lock(conn):
def do_execmany():
with conn.cursor() as cur:
cur.executemany(ph(cur, "select pg_sleep(%s)"), [(0.1,) for _ in range(10)])
def do_exec():
with conn.cursor() as cur:
for i in range(100):
cur.execute("select 1")
gather(spawn(do_execmany), spawn(do_exec))
def test_rowcount(conn):
cur = conn.cursor()
cur.execute("select 1 from generate_series(1, 0)")
assert cur.rowcount == 0
cur.execute("select 1 from generate_series(1, 42)")
assert cur.rowcount == 42
cur.execute("show timezone")
assert cur.rowcount == 1
cur.execute("create table test_rowcount_notuples (id int primary key)")
assert cur.rowcount == -1
cur.execute("insert into test_rowcount_notuples select generate_series(1, 42)")
assert cur.rowcount == 42
def test_rownumber(conn):
cur = conn.cursor()
assert cur.rownumber is None
cur.execute("select 1 from generate_series(1, 42)")
assert cur.rownumber == 0
cur.fetchone()
assert cur.rownumber == 1
cur.fetchone()
assert cur.rownumber == 2
cur.fetchmany(10)
assert cur.rownumber == 12
rns: list[int] = []
for i in cur:
assert cur.rownumber
rns.append(cur.rownumber)
if len(rns) >= 3:
break
assert rns == [13, 14, 15]
assert len(cur.fetchall()) == 42 - rns[-1]
assert cur.rownumber == 42
@pytest.mark.parametrize("query", ["", "set timezone to utc"])
def test_rownumber_none(conn, query):
cur = conn.cursor()
cur.execute(query)
assert cur.rownumber is None
def test_rownumber_mixed(conn):
cur = conn.cursor()
cur.execute(
"""
select x from generate_series(1, 3) x;
set timezone to utc;
select x from generate_series(4, 6) x;
"""
)
assert cur.rownumber == 0
assert cur.fetchone() == (1,)
assert cur.rownumber == 1
assert cur.fetchone() == (2,)
assert cur.rownumber == 2
cur.nextset()
assert cur.rownumber is None
cur.nextset()
assert cur.rownumber == 0
assert cur.fetchone() == (4,)
assert cur.rownumber == 1
def test_iter(conn):
cur = conn.cursor()
cur.execute("select generate_series(1, 3)")
assert list(cur) == [(1,), (2,), (3,)]
def test_iter_stop(conn):
cur = conn.cursor()
cur.execute("select generate_series(1, 3)")
for rec in cur:
assert rec == (1,)
break
for rec in cur:
assert rec == (2,)
break
assert cur.fetchone() == (3,)
assert list(cur) == []
def test_row_factory(conn):
cur = conn.cursor(row_factory=my_row_factory)
cur.execute("reset search_path")
with pytest.raises(psycopg.ProgrammingError):
cur.fetchone()
cur.execute("select 'foo' as bar")
(r,) = cur.fetchone()
assert r == "FOObar"
cur.execute("select 'x' as x; select 'y' as y, 'z' as z")
assert cur.fetchall() == [["Xx"]]
assert cur.nextset()
assert cur.fetchall() == [["Yy", "Zz"]]
cur.scroll(-1)
cur.row_factory = rows.dict_row
assert cur.fetchone() == {"y": "y", "z": "z"}
def test_row_factory_none(conn):
cur = conn.cursor(row_factory=None)
assert cur.row_factory is rows.tuple_row
cur.execute("select 1 as a, 2 as b")
r = cur.fetchone()
assert type(r) is tuple
assert r == (1, 2)
def test_bad_row_factory(conn):
def broken_factory(cur):
1 / 0
cur = conn.cursor(row_factory=broken_factory)
with pytest.raises(ZeroDivisionError):
cur.execute("select 1")
def broken_maker(cur):
def make_row(seq):
1 / 0
return make_row
cur = conn.cursor(row_factory=broken_maker)
cur.execute("select 1")
with pytest.raises(ZeroDivisionError):
cur.fetchone()
def test_scroll(conn):
cur = conn.cursor()
with pytest.raises(psycopg.ProgrammingError):
cur.scroll(0)
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(-1)
assert cur.fetchone() == (8,)
cur.scroll(-2)
assert cur.fetchone() == (7,)
cur.scroll(2, mode="absolute")
assert cur.fetchone() == (2,)
# on the boundary
cur.scroll(0, mode="absolute")
assert cur.fetchone() == (0,)
with pytest.raises(IndexError):
cur.scroll(-1, mode="absolute")
cur.scroll(0, mode="absolute")
with pytest.raises(IndexError):
cur.scroll(-1)
cur.scroll(9, mode="absolute")
assert cur.fetchone() == (9,)
with pytest.raises(IndexError):
cur.scroll(10, mode="absolute")
cur.scroll(9, mode="absolute")
with pytest.raises(IndexError):
cur.scroll(1)
with pytest.raises(ValueError):
cur.scroll(1, "wat")
@pytest.mark.parametrize(
"query, params, want",
[
("select %(x)s", {"x": 1}, (1,)),
("select %(x)s, %(y)s", {"x": 1, "y": 2}, (1, 2)),
("select %(x)s, %(x)s", {"x": 1}, (1, 1)),
],
)
def test_execute_params_named(conn, query, params, want):
cur = conn.cursor()
cur.execute(ph(cur, query), params)
rec = cur.fetchone()
assert rec == want
def test_stream(conn):
cur = conn.cursor()
recs = []
for rec in cur.stream(
ph(cur, "select i, '2021-01-01'::date + i from generate_series(1, %s) as i"),
[2],
):
recs.append(rec)
assert recs == [(1, dt.date(2021, 1, 2)), (2, dt.date(2021, 1, 3))]
def test_stream_sql(conn):
cur = conn.cursor()
recs = list(
cur.stream(
sql.SQL(
"select i, '2021-01-01'::date + i from generate_series(1, {}) as i"
).format(2)
)
)
assert recs == [(1, dt.date(2021, 1, 2)), (2, dt.date(2021, 1, 3))]
def test_stream_row_factory(conn):
cur = conn.cursor(row_factory=rows.dict_row)
it = cur.stream("select generate_series(1,2) as a")
assert next(it)["a"] == 1
cur.row_factory = rows.namedtuple_row
assert next(it).a == 2
def test_stream_no_row(conn):
cur = conn.cursor()
recs = list(cur.stream("select generate_series(2,1) as a"))
assert recs == []
def test_stream_chunked_invalid_size(conn):
cur = conn.cursor()
with pytest.raises(ValueError, match="size must be >= 1"):
next(cur.stream("select 1", size=0))
@pytest.mark.libpq("< 17")
def test_stream_chunked_not_supported(conn):
cur = conn.cursor()
with pytest.raises(psycopg.NotSupportedError):
next(cur.stream("select generate_series(1, 4)", size=2))
@pytest.mark.libpq(">= 17")
def test_stream_chunked(conn):
cur = conn.cursor()
recs = list(cur.stream("select generate_series(1, 5) as a", size=2))
assert recs == [(1,), (2,), (3,), (4,), (5,)]
@pytest.mark.libpq(">= 17")
def test_stream_chunked_row_factory(conn):
cur = conn.cursor(row_factory=rows.scalar_row)
it = cur.stream("select generate_series(1, 5) as a", size=2)
for i in range(1, 6):
assert next(it) == i
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?"]
)
def test_stream_badquery(conn, query):
cur = conn.cursor()
with pytest.raises(psycopg.ProgrammingError):
for rec in cur.stream(query):
pass
def test_stream_error_tx(conn):
cur = conn.cursor()
with pytest.raises(psycopg.ProgrammingError):
for rec in cur.stream("wat"):
pass
assert conn.info.transaction_status == pq.TransactionStatus.INERROR
def test_stream_error_notx(conn):
conn.set_autocommit(True)
cur = conn.cursor()
with pytest.raises(psycopg.ProgrammingError):
for rec in cur.stream("wat"):
pass
assert conn.info.transaction_status == pq.TransactionStatus.IDLE
def test_stream_error_python_to_consume(conn):
cur = conn.cursor()
with pytest.raises(ZeroDivisionError):
with closing(cur.stream("select generate_series(1, 10000)")) as gen:
for rec in gen:
1 / 0
assert conn.info.transaction_status in (
pq.TransactionStatus.INTRANS,
pq.TransactionStatus.INERROR,
)
def test_stream_error_python_consumed(conn):
cur = conn.cursor()
with pytest.raises(ZeroDivisionError):
gen = cur.stream("select 1")
for rec in gen:
1 / 0
gen.close()
assert conn.info.transaction_status == pq.TransactionStatus.INTRANS
@pytest.mark.parametrize("autocommit", [False, True])
def test_stream_close(conn, autocommit):
conn.set_autocommit(autocommit)
cur = conn.cursor()
with pytest.raises(psycopg.OperationalError):
for rec in cur.stream("select generate_series(1, 3)"):
if rec[0] == 1:
conn.close()
else:
assert False
assert conn.closed
def test_stream_binary_cursor(conn):
with raiseif(
conn.cursor_factory is psycopg.ClientCursor, psycopg.NotSupportedError
):
cur = conn.cursor(binary=True)
recs = []
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]])
assert recs == [(1,), (2,)]
def test_stream_execute_binary(conn):
cur = conn.cursor()
recs = []
with raiseif(
conn.cursor_factory is psycopg.ClientCursor, psycopg.NotSupportedError
):
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]])
assert recs == [(1,), (2,)]
def test_stream_binary_cursor_text_override(conn):
cur = conn.cursor(binary=True)
recs = []
for rec in cur.stream("select generate_series(1, 2)", binary=False):
recs.append(rec)
assert cur.pgresult.fformat(0) == 0
assert cur.pgresult.get_value(0, 0) == str(rec[0]).encode()
assert recs == [(1,), (2,)]
def test_str(conn):
cur = conn.cursor()
assert "[IDLE]" in str(cur)
assert "[closed]" not in str(cur)
assert "[no result]" in str(cur)
cur.execute("select 1")
assert "[INTRANS]" in str(cur)
assert "[TUPLES_OK]" in str(cur)
assert "[closed]" not in str(cur)
assert "[no result]" not in str(cur)
cur.close()
assert "[closed]" in str(cur)
assert "[INTRANS]" in str(cur)
@pytest.mark.pipeline
def test_message_0x33(conn):
# https://github.com/psycopg/psycopg/issues/314
notices = []
conn.add_notice_handler(lambda diag: notices.append(diag.message_primary))
conn.set_autocommit(True)
with conn.pipeline():
cur = conn.execute("select 'test'")
assert cur.fetchone() == ("test",)
assert not notices
def test_typeinfo(conn):
info = TypeInfo.fetch(conn, "jsonb")
assert info is not None
def test_error_no_result(conn):
cur = conn.cursor()
with pytest.raises(psycopg.ProgrammingError, match="no result available"):
cur.fetchone()
cur.execute("set timezone to utc")
with pytest.raises(
psycopg.ProgrammingError, match="last operation.*command status: SET"
):
cur.fetchone()
cur.execute("")
with pytest.raises(
psycopg.ProgrammingError, match="last operation.*result status: EMPTY_QUERY"
):
cur.fetchone()
def test_row_maker_returns_none(conn):
cur = conn.cursor(row_factory=rows.scalar_row)
query = "values (null), (0)"
recs = [None, 0]
cur.execute(query)
assert [cur.fetchone() for _ in range(len(recs))] == recs
cur.execute(query)
assert cur.fetchmany(len(recs)) == recs
cur.execute(query)
assert cur.fetchall() == recs
cur.execute(query)
assert list(cur) == recs
stream = cur.stream(query)
assert list(stream) == recs
@pytest.mark.parametrize("count", [1, 3])
def test_results_after_execute(conn, count):
with conn.cursor() as cur:
cur.execute(
";".join((f"select * from generate_series(1, {i})" for i in range(count)))
)
ress = list((res.fetchall() for res in cur.results()))
assert ress == [[(j + 1,) for j in range(i)] for i in range(count)]
@pytest.mark.parametrize("count", [0, 1, 3])
@pytest.mark.parametrize("returning", [False, True])
def test_results_after_executemany(conn, count, returning):
with conn.cursor() as cur:
cur.executemany(
ph(cur, "select * from generate_series(1, %s)"),
[(i,) for i in range(count)],
returning=returning,
)
ress = list((res.fetchall() for res in cur.results()))
if returning:
assert ress == [[(j + 1,) for j in range(i)] for i in range(count)]
else:
assert ress == []
def test_change_loader_results(conn):
cur = conn.cursor()
# With no result
cur.adapters.register_loader("text", make_loader("1"))
cur.execute(
"""
values ('foo'::text);
values ('bar'::text), ('baz');
values ('qux'::text);
"""
)
assert cur.fetchall() == [("foo1",)]
cur.nextset()
assert cur.fetchone() == ("bar1",)
cur.adapters.register_loader("text", make_loader("2"))
assert cur.fetchone() == ("baz2",)
cur.scroll(-2)
assert cur.fetchall() == [("bar2",), ("baz2",)]
cur.nextset()
assert cur.fetchall() == [("qux2",)]
# After the final result
assert not cur.nextset()
cur.adapters.register_loader("text", make_loader("3"))
assert cur.fetchone() is None
cur.set_result(0)
assert cur.fetchall() == [("foo3",)]
|