From: Daniele Varrazzo Date: Sun, 15 May 2022 17:49:14 +0000 (+0200) Subject: test: Fix implicit f-string conversion to value X-Git-Tag: 3.1~93 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b4ac2ff6a4d3e80848f8913a48b44a37775e3f9f;p=thirdparty%2Fpsycopg.git test: Fix implicit f-string conversion to value Behaviour changed in Python 3.11. --- diff --git a/tests/test_adapt.py b/tests/test_adapt.py index 9656a8f25..e43038c9b 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -354,26 +354,26 @@ def test_return_untyped(conn, fmt_in): # Currently string are passed as unknown oid to libpq. This is because # unknown is more easily cast by postgres to different types (see jsonb # later). - cur.execute(f"select %{fmt_in}, %{fmt_in}", ["hello", 10]) + cur.execute(f"select %{fmt_in.value}, %{fmt_in.value}", ["hello", 10]) assert cur.fetchone() == ("hello", 10) cur.execute("create table testjson(data jsonb)") if fmt_in != PyFormat.BINARY: - cur.execute(f"insert into testjson (data) values (%{fmt_in})", ["{}"]) + cur.execute(f"insert into testjson (data) values (%{fmt_in.value})", ["{}"]) assert cur.execute("select data from testjson").fetchone() == ({},) else: # Binary types cannot be passed as unknown oids. with pytest.raises(e.DatatypeMismatch): - cur.execute(f"insert into testjson (data) values (%{fmt_in})", ["{}"]) + cur.execute(f"insert into testjson (data) values (%{fmt_in.value})", ["{}"]) -@pytest.mark.parametrize("fmt_in", PyFormat.AUTO) +@pytest.mark.parametrize("fmt_in", PyFormat) def test_no_cast_needed(conn, fmt_in): # Verify that there is no need of cast in certain common scenario - cur = conn.execute("select '2021-01-01'::date + %s", [3]) + cur = conn.execute(f"select '2021-01-01'::date + %{fmt_in.value}", [3]) assert cur.fetchone()[0] == dt.date(2021, 1, 4) - cur = conn.execute("select '[10, 20, 30]'::jsonb -> %s", [1]) + cur = conn.execute(f"select '[10, 20, 30]'::jsonb -> %{fmt_in.value}", [1]) assert cur.fetchone()[0] == 20 diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 29c726c33..3c7e4e443 100644 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -372,12 +372,12 @@ def test_executemany_null_first(conn, fmt_in): cur = conn.cursor() cur.execute("create table testmany (a bigint, b bigint)") cur.executemany( - f"insert into testmany values (%{fmt_in}, %{fmt_in})", + f"insert into testmany values (%{fmt_in.value}, %{fmt_in.value})", [[1, None], [3, 4]], ) with pytest.raises((psycopg.DataError, psycopg.ProgrammingError)): cur.executemany( - f"insert into testmany values (%{fmt_in}, %{fmt_in})", + f"insert into testmany values (%{fmt_in.value}, %{fmt_in.value})", [[1, ""], [3, 4]], ) diff --git a/tests/test_cursor_async.py b/tests/test_cursor_async.py index 6d63bed14..39d8e8327 100644 --- a/tests/test_cursor_async.py +++ b/tests/test_cursor_async.py @@ -362,12 +362,12 @@ async def test_executemany_null_first(aconn, fmt_in): cur = aconn.cursor() await cur.execute("create table testmany (a bigint, b bigint)") await cur.executemany( - f"insert into testmany values (%{fmt_in}, %{fmt_in})", + f"insert into testmany values (%{fmt_in.value}, %{fmt_in.value})", [[1, None], [3, 4]], ) with pytest.raises((psycopg.DataError, psycopg.ProgrammingError)): await cur.executemany( - f"insert into testmany values (%{fmt_in}, %{fmt_in})", + f"insert into testmany values (%{fmt_in.value}, %{fmt_in.value})", [[1, ""], [3, 4]], ) diff --git a/tests/test_sql.py b/tests/test_sql.py index 90c39da25..e7a84b10e 100644 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -563,18 +563,18 @@ class TestPlaceholder: @pytest.mark.parametrize("format", PyFormat) def test_as_string(self, conn, format): ph = sql.Placeholder(format=format) - assert ph.as_string(conn) == f"%{format}" + assert ph.as_string(conn) == f"%{format.value}" ph = sql.Placeholder(name="foo", format=format) - assert ph.as_string(conn) == f"%(foo){format}" + assert ph.as_string(conn) == f"%(foo){format.value}" @pytest.mark.parametrize("format", PyFormat) def test_as_bytes(self, conn, format): ph = sql.Placeholder(format=format) - assert ph.as_bytes(conn) == f"%{format}".encode("ascii") + assert ph.as_bytes(conn) == f"%{format.value}".encode("ascii") ph = sql.Placeholder(name="foo", format=format) - assert ph.as_bytes(conn) == f"%(foo){format}".encode("ascii") + assert ph.as_bytes(conn) == f"%(foo){format.value}".encode("ascii") class TestValues: diff --git a/tests/types/test_array.py b/tests/types/test_array.py index 6ea197101..07f99e146 100644 --- a/tests/types/test_array.py +++ b/tests/types/test_array.py @@ -32,7 +32,7 @@ tests_str = [ @pytest.mark.parametrize("obj, want", tests_str) def test_dump_list_str(conn, obj, want, fmt_in): cur = conn.cursor() - cur.execute(f"select %{fmt_in}::text[] = %s::text[]", (obj, want)) + cur.execute(f"select %{fmt_in.value}::text[] = %s::text[]", (obj, want)) assert cur.fetchone()[0] @@ -50,16 +50,16 @@ def test_all_chars(conn, fmt_in, fmt_out): cur = conn.cursor(binary=fmt_out) for i in range(1, 256): c = chr(i) - cur.execute(f"select %{fmt_in}::text[]", ([c],)) + cur.execute(f"select %{fmt_in.value}::text[]", ([c],)) assert cur.fetchone()[0] == [c] a = list(map(chr, range(1, 256))) a.append("\u20ac") - cur.execute(f"select %{fmt_in}::text[]", (a,)) + cur.execute(f"select %{fmt_in.value}::text[]", (a,)) assert cur.fetchone()[0] == a s = "".join(a) - cur.execute(f"select %{fmt_in}::text[]", ([s],)) + cur.execute(f"select %{fmt_in.value}::text[]", ([s],)) assert cur.fetchone()[0] == [s] @@ -176,7 +176,7 @@ def test_list_number_wrapper(conn, wrapper, fmt_in, fmt_out): obj = [wrapper(1), wrapper(0), wrapper(-1), None] cur = conn.cursor(binary=fmt_out) - got = cur.execute(f"select %{fmt_in}", [obj]).fetchone()[0] + got = cur.execute(f"select %{fmt_in.value}", [obj]).fetchone()[0] assert got == obj for i in got: if i is not None: @@ -197,7 +197,7 @@ def test_empty_list_mix(conn, fmt_in): conn.execute("create table testarrays (col1 bigint[], col2 bigint[])") # pro tip: don't get confused with the types f1, f2 = conn.execute( - f"insert into testarrays values (%{fmt_in}, %{fmt_in}) returning *", + f"insert into testarrays values (%{fmt_in.value}, %{fmt_in.value}) returning *", (objs, []), ).fetchone() assert f1 == objs @@ -209,14 +209,14 @@ def test_empty_list(conn, fmt_in): cur = conn.cursor() cur.execute("create table test (id serial primary key, data date[])") with conn.transaction(): - cur.execute(f"insert into test (data) values (%{fmt_in})", ([],)) + cur.execute(f"insert into test (data) values (%{fmt_in.value})", ([],)) cur.execute("select data from test") assert cur.fetchone() == ([],) # test untyped list in a filter - cur.execute(f"select data from test where id = any(%{fmt_in})", ([1],)) + cur.execute(f"select data from test where id = any(%{fmt_in.value})", ([1],)) assert cur.fetchone() - cur.execute(f"select data from test where id = any(%{fmt_in})", ([],)) + cur.execute(f"select data from test where id = any(%{fmt_in.value})", ([],)) assert not cur.fetchone() @@ -224,7 +224,9 @@ def test_empty_list(conn, fmt_in): def test_empty_list_after_choice(conn, fmt_in): cur = conn.cursor() cur.execute("create table test (id serial primary key, data float[])") - cur.executemany(f"insert into test (data) values (%{fmt_in})", [([1.0],), ([],)]) + cur.executemany( + f"insert into test (data) values (%{fmt_in.value})", [([1.0],), ([],)] + ) cur.execute("select data from test order by id") assert cur.fetchall() == [([1.0],), ([],)] diff --git a/tests/types/test_bool.py b/tests/types/test_bool.py index 9a6a8e8f5..edd4dad7a 100644 --- a/tests/types/test_bool.py +++ b/tests/types/test_bool.py @@ -11,13 +11,13 @@ from psycopg.postgres import types as builtins @pytest.mark.parametrize("b", [True, False]) def test_roundtrip_bool(conn, b, fmt_in, fmt_out): cur = conn.cursor(binary=fmt_out) - result = cur.execute(f"select %{fmt_in}", (b,)).fetchone()[0] + result = cur.execute(f"select %{fmt_in.value}", (b,)).fetchone()[0] assert cur.pgresult.fformat(0) == fmt_out if b is not None: assert cur.pgresult.ftype(0) == builtins["bool"].oid assert result is b - result = cur.execute(f"select %{fmt_in}", ([b],)).fetchone()[0] + result = cur.execute(f"select %{fmt_in.value}", ([b],)).fetchone()[0] assert cur.pgresult.fformat(0) == fmt_out if b is not None: assert cur.pgresult.ftype(0) == builtins["bool"].array_oid diff --git a/tests/types/test_composite.py b/tests/types/test_composite.py index c31822f0b..2c5b6703b 100644 --- a/tests/types/test_composite.py +++ b/tests/types/test_composite.py @@ -75,7 +75,7 @@ def test_dump_builtin_empty_range(conn, fmt_in): register_composite(info, conn) cur = conn.execute( - f"select pg_typeof(%{fmt_in})", + f"select pg_typeof(%{fmt_in.value})", [info.python_type(10, Range(empty=True), [])], ) assert cur.fetchone()[0] == "tmptype" @@ -179,7 +179,7 @@ def test_dump_tuple_all_chars(conn, fmt_in, testcomp): cur = conn.cursor() for i in range(1, 256): (res,) = cur.execute( - f"select row(chr(%s::int), 1, 1.0)::testcomp = %{fmt_in}::testcomp", + f"select row(chr(%s::int), 1, 1.0)::testcomp = %{fmt_in.value}::testcomp", (i, (chr(i), 1, 1.0)), ).fetchone() assert res is True @@ -193,7 +193,7 @@ def test_dump_composite_all_chars(conn, fmt_in, testcomp): for i in range(1, 256): obj = factory(chr(i), 1, 1.0) (res,) = cur.execute( - f"select row(chr(%s::int), 1, 1.0)::testcomp = %{fmt_in}", (i, obj) + f"select row(chr(%s::int), 1, 1.0)::testcomp = %{fmt_in.value}", (i, obj) ).fetchone() assert res is True @@ -207,7 +207,8 @@ def test_dump_composite_null(conn, fmt_in, testcomp): obj = factory("foo", 1, None) rec = cur.execute( f""" - select row('foo', 1, NULL)::testcomp = %(obj){fmt_in}, %(obj){fmt_in}::text + select row('foo', 1, NULL)::testcomp = %(obj){fmt_in.value}, + %(obj){fmt_in.value}::text """, {"obj": obj}, ).fetchone() diff --git a/tests/types/test_datetime.py b/tests/types/test_datetime.py index 05416d743..02d5a670a 100644 --- a/tests/types/test_datetime.py +++ b/tests/types/test_datetime.py @@ -22,7 +22,7 @@ class TestDate: def test_dump_date(self, conn, val, expr, fmt_in): val = as_date(val) cur = conn.cursor() - cur.execute(f"select '{expr}'::date = %{fmt_in}", (val,)) + cur.execute(f"select '{expr}'::date = %{fmt_in.value}", (val,)) assert cur.fetchone()[0] is True cur.execute( @@ -104,12 +104,12 @@ class TestDatetime: def test_dump_datetime(self, conn, val, expr, fmt_in): cur = conn.cursor() cur.execute("set timezone to '+02:00'") - cur.execute(f"select %{fmt_in}", (as_dt(val),)) - cur.execute(f"select '{expr}'::timestamp = %{fmt_in}", (as_dt(val),)) + cur.execute(f"select %{fmt_in.value}", (as_dt(val),)) + cur.execute(f"select '{expr}'::timestamp = %{fmt_in.value}", (as_dt(val),)) cur.execute( f""" - select '{expr}'::timestamp = %(val){fmt_in}, - '{expr}', %(val){fmt_in}::text + select '{expr}'::timestamp = %(val){fmt_in.value}, + '{expr}', %(val){fmt_in.value}::text """, {"val": as_dt(val)}, ) @@ -219,8 +219,8 @@ class TestDateTimeTz: cur.execute("set timezone to '-02:00'") cur.execute( f""" - select '{expr}'::timestamptz = %(val){fmt_in}, - '{expr}', %(val){fmt_in}::text + select '{expr}'::timestamptz = %(val){fmt_in.value}, + '{expr}', %(val){fmt_in.value}::text """, {"val": as_dt(val)}, ) @@ -308,7 +308,7 @@ class TestDateTimeTz: val = as_dt(val) cur = conn.cursor() cur.execute( - f"select pg_typeof(%{fmt_in}) = %s::regtype, %{fmt_in}", + f"select pg_typeof(%{fmt_in.value}) = %s::regtype, %{fmt_in.value}", [val, type, val], ) rec = cur.fetchone() @@ -379,8 +379,8 @@ class TestTime: cur = conn.cursor() cur.execute( f""" - select '{expr}'::time = %(val){fmt_in}, - '{expr}'::time::text, %(val){fmt_in}::text + select '{expr}'::time = %(val){fmt_in.value}, + '{expr}'::time::text, %(val){fmt_in.value}::text """, {"val": as_time(val)}, ) @@ -429,7 +429,7 @@ class TestTimeTz: def test_dump_timetz(self, conn, val, expr, fmt_in): cur = conn.cursor() cur.execute("set timezone to '-02:00'") - cur.execute(f"select '{expr}'::timetz = %{fmt_in}", (as_time(val),)) + cur.execute(f"select '{expr}'::timetz = %{fmt_in.value}", (as_time(val),)) assert cur.fetchone()[0] is True @pytest.mark.parametrize( @@ -470,7 +470,7 @@ class TestTimeTz: val = as_time(val) cur = conn.cursor() cur.execute( - f"select pg_typeof(%{fmt_in}) = %s::regtype, %{fmt_in}", + f"select pg_typeof(%{fmt_in.value}) = %s::regtype, %{fmt_in.value}", [val, type, val], ) rec = cur.fetchone() diff --git a/tests/types/test_json.py b/tests/types/test_json.py index 790a37c07..482bb3e3d 100644 --- a/tests/types/test_json.py +++ b/tests/types/test_json.py @@ -26,9 +26,9 @@ samples = [ def test_json_dump(conn, val, fmt_in): obj = json.loads(val) cur = conn.cursor() - cur.execute(f"select pg_typeof(%{fmt_in}) = 'json'::regtype", (Json(obj),)) + cur.execute(f"select pg_typeof(%{fmt_in.value}) = 'json'::regtype", (Json(obj),)) assert cur.fetchone()[0] is True - cur.execute(f"select %{fmt_in}::text = %s::json::text", (Json(obj), val)) + cur.execute(f"select %{fmt_in.value}::text = %s::json::text", (Json(obj), val)) assert cur.fetchone()[0] is True @@ -37,7 +37,7 @@ def test_json_dump(conn, val, fmt_in): def test_jsonb_dump(conn, val, fmt_in): obj = json.loads(val) cur = conn.cursor() - cur.execute(f"select %{fmt_in} = %s::jsonb", (Jsonb(obj), val)) + cur.execute(f"select %{fmt_in.value} = %s::jsonb", (Jsonb(obj), val)) assert cur.fetchone()[0] is True @@ -74,7 +74,7 @@ def test_json_dump_customise(conn, wrapper, fmt_in): set_json_dumps(my_dumps) try: - cur.execute(f"select %{fmt_in}->>'baz' = 'qux'", (wrapper(obj),)) + cur.execute(f"select %{fmt_in.value}->>'baz' = 'qux'", (wrapper(obj),)) assert cur.fetchone()[0] is True finally: set_json_dumps(json.dumps) @@ -89,9 +89,9 @@ def test_json_dump_customise_context(conn, wrapper, fmt_in): cur2 = conn.cursor() set_json_dumps(my_dumps, cur2) - cur1.execute(f"select %{fmt_in}->>'baz'", (wrapper(obj),)) + cur1.execute(f"select %{fmt_in.value}->>'baz'", (wrapper(obj),)) assert cur1.fetchone()[0] is None - cur2.execute(f"select %{fmt_in}->>'baz'", (wrapper(obj),)) + cur2.execute(f"select %{fmt_in.value}->>'baz'", (wrapper(obj),)) assert cur2.fetchone()[0] == "qux" @@ -101,7 +101,7 @@ def test_json_dump_customise_wrapper(conn, wrapper, fmt_in): wrapper = getattr(psycopg.types.json, wrapper) obj = {"foo": "bar"} cur = conn.cursor() - cur.execute(f"select %{fmt_in}->>'baz' = 'qux'", (wrapper(obj, my_dumps),)) + cur.execute(f"select %{fmt_in.value}->>'baz' = 'qux'", (wrapper(obj, my_dumps),)) assert cur.fetchone()[0] is True diff --git a/tests/types/test_multirange.py b/tests/types/test_multirange.py index 7d6bd3a9a..bbc389011 100644 --- a/tests/types/test_multirange.py +++ b/tests/types/test_multirange.py @@ -169,7 +169,7 @@ mr_classes = """ @pytest.mark.parametrize("fmt_in", PyFormat) def test_dump_builtin_empty(conn, pgtype, fmt_in): mr = Multirange() # type: ignore[var-annotated] - cur = conn.execute(f"select '{{}}'::{pgtype} = %{fmt_in}", (mr,)) + cur = conn.execute(f"select '{{}}'::{pgtype} = %{fmt_in.value}", (mr,)) assert cur.fetchone()[0] is True @@ -181,9 +181,9 @@ def test_dump_builtin_empty_wrapper(conn, wrapper, fmt_in): mr = wrapper() rec = conn.execute( f""" - select '{{}}' = %(mr){fmt_in}, - %(mr){fmt_in}::text, - pg_typeof(%(mr){fmt_in})::oid + select '{{}}' = %(mr){fmt_in.value}, + %(mr){fmt_in.value}::text, + pg_typeof(%(mr){fmt_in.value})::oid """, {"mr": mr}, ).fetchone() @@ -211,7 +211,7 @@ def test_dump_builtin_array(conn, pgtype, fmt_in): mr1 = Multirange() # type: ignore[var-annotated] mr2 = Multirange([Range(bounds="()")]) # type: ignore[var-annotated] cur = conn.execute( - f"select array['{{}}'::{pgtype}, '{{(,)}}'::{pgtype}] = %{fmt_in}", + f"select array['{{}}'::{pgtype}, '{{(,)}}'::{pgtype}] = %{fmt_in.value}", ([mr1, mr2],), ) assert cur.fetchone()[0] is True @@ -224,7 +224,8 @@ def test_dump_builtin_array_with_cast(conn, pgtype, fmt_in): mr2 = Multirange([Range(bounds="()")]) # type: ignore[var-annotated] cur = conn.execute( f""" - select array['{{}}'::{pgtype}, '{{(,)}}'::{pgtype}] = %{fmt_in}::{pgtype}[] + select array['{{}}'::{pgtype}, + '{{(,)}}'::{pgtype}] = %{fmt_in.value}::{pgtype}[] """, ([mr1, mr2],), ) @@ -237,7 +238,9 @@ def test_dump_builtin_array_wrapper(conn, wrapper, fmt_in): wrapper = getattr(multirange, wrapper) mr1 = Multirange() # type: ignore[var-annotated] mr2 = Multirange([Range(bounds="()")]) # type: ignore[var-annotated] - cur = conn.execute(f"""select '{{"{{}}","{{(,)}}"}}' = %{fmt_in}""", ([mr1, mr2],)) + cur = conn.execute( + f"""select '{{"{{}}","{{(,)}}"}}' = %{fmt_in.value}""", ([mr1, mr2],) + ) assert cur.fetchone()[0] is True @@ -247,7 +250,7 @@ def test_dump_builtin_multirange(conn, pgtype, ranges, fmt_in): mr = Multirange(ranges) rname = pgtype.replace("multi", "") phs = ", ".join([f"%s::{rname}"] * len(ranges)) - cur = conn.execute(f"select {pgtype}({phs}) = %{fmt_in}", ranges + [mr]) + cur = conn.execute(f"select {pgtype}({phs}) = %{fmt_in.value}", ranges + [mr]) assert cur.fetchone()[0] is True diff --git a/tests/types/test_net.py b/tests/types/test_net.py index 6a6fbee1f..41104e99f 100644 --- a/tests/types/test_net.py +++ b/tests/types/test_net.py @@ -13,10 +13,10 @@ from psycopg.adapt import PyFormat @pytest.mark.parametrize("val", ["192.168.0.1", "2001:db8::"]) def test_address_dump(conn, fmt_in, val): cur = conn.cursor() - cur.execute(f"select %{fmt_in} = %s::inet", (ipaddress.ip_address(val), val)) + cur.execute(f"select %{fmt_in.value} = %s::inet", (ipaddress.ip_address(val), val)) assert cur.fetchone()[0] is True cur.execute( - f"select %{fmt_in} = array[null, %s]::inet[]", + f"select %{fmt_in.value} = array[null, %s]::inet[]", ([None, ipaddress.ip_interface(val)], val), ) assert cur.fetchone()[0] is True @@ -27,12 +27,13 @@ def test_address_dump(conn, fmt_in, val): def test_interface_dump(conn, fmt_in, val): cur = conn.cursor() rec = cur.execute( - f"select %(val){fmt_in} = %(repr)s::inet, %(val){fmt_in}, %(repr)s::inet", + f"select %(val){fmt_in.value} = %(repr)s::inet," + f" %(val){fmt_in.value}, %(repr)s::inet", {"val": ipaddress.ip_interface(val), "repr": val}, ).fetchone() assert rec[0] is True, f"{rec[1]} != {rec[2]}" cur.execute( - f"select %{fmt_in} = array[null, %s]::inet[]", + f"select %{fmt_in.value} = array[null, %s]::inet[]", ([None, ipaddress.ip_interface(val)], val), ) assert cur.fetchone()[0] is True @@ -42,10 +43,10 @@ def test_interface_dump(conn, fmt_in, val): @pytest.mark.parametrize("val", ["127.0.0.0/24", "::ffff:102:300/128"]) def test_network_dump(conn, fmt_in, val): cur = conn.cursor() - cur.execute(f"select %{fmt_in} = %s::cidr", (ipaddress.ip_network(val), val)) + cur.execute(f"select %{fmt_in.value} = %s::cidr", (ipaddress.ip_network(val), val)) assert cur.fetchone()[0] is True cur.execute( - f"select %{fmt_in} = array[NULL, %s]::cidr[]", + f"select %{fmt_in.value} = array[NULL, %s]::cidr[]", ([None, ipaddress.ip_network(val)], val), ) assert cur.fetchone()[0] is True @@ -58,7 +59,7 @@ def test_network_mixed_size_array(conn, fmt_in): ipaddress.IPv6Network("::1/128"), ] cur = conn.cursor() - cur.execute(f"select %{fmt_in}", (val,)) + cur.execute(f"select %{fmt_in.value}", (val,)) got = cur.fetchone()[0] assert val == got diff --git a/tests/types/test_numeric.py b/tests/types/test_numeric.py index a6d34e8b1..e75a43dae 100644 --- a/tests/types/test_numeric.py +++ b/tests/types/test_numeric.py @@ -32,7 +32,7 @@ from psycopg.types.numeric import FloatLoader def test_dump_int(conn, val, expr, fmt_in): assert isinstance(val, int) cur = conn.cursor() - cur.execute(f"select {expr} = %{fmt_in}", (val,)) + cur.execute(f"select {expr} = %{fmt_in.value}", (val,)) assert cur.fetchone()[0] is True @@ -61,10 +61,10 @@ def test_dump_int(conn, val, expr, fmt_in): @pytest.mark.parametrize("fmt_in", PyFormat) def test_dump_int_subtypes(conn, val, expr, fmt_in): cur = conn.cursor() - cur.execute(f"select pg_typeof({expr}) = pg_typeof(%{fmt_in})", (val,)) + cur.execute(f"select pg_typeof({expr}) = pg_typeof(%{fmt_in.value})", (val,)) assert cur.fetchone()[0] is True cur.execute( - f"select {expr} = %(v){fmt_in}, {expr}::text, %(v){fmt_in}::text", + f"select {expr} = %(v){fmt_in.value}, {expr}::text, %(v){fmt_in.value}::text", {"v": val}, ) ok, want, got = cur.fetchone() @@ -84,7 +84,7 @@ class MyMixinEnum(enum.IntEnum): @pytest.mark.parametrize("enum", [MyEnum, MyMixinEnum]) def test_dump_enum(conn, fmt_in, enum): cur = conn.cursor() - cur.execute(f"select %{fmt_in}", (enum.foo,)) + cur.execute(f"select %{fmt_in.value}", (enum.foo,)) (res,) = cur.fetchone() assert res == enum.foo.value @@ -174,7 +174,7 @@ def test_load_int(conn, val, pgtype, want, fmt_out): def test_dump_float(conn, val, expr, fmt_in): assert isinstance(val, float) cur = conn.cursor() - cur.execute(f"select %{fmt_in} = {expr}::float8", (val,)) + cur.execute(f"select %{fmt_in.value} = {expr}::float8", (val,)) assert cur.fetchone()[0] is True @@ -330,7 +330,7 @@ def test_load_float_copy(conn): def test_roundtrip_numeric(conn, val, fmt_in, fmt_out): cur = conn.cursor(binary=fmt_out) val = Decimal(val) - cur.execute(f"select %{fmt_in}", (val,)) + cur.execute(f"select %{fmt_in.value}", (val,)) result = cur.fetchone()[0] assert isinstance(result, Decimal) if val.is_nan(): @@ -419,7 +419,7 @@ def test_dump_numeric_exhaustive(conn, fmt_in): for f in funcs: expr = f(i) val = Decimal(expr) - cur.execute(f"select %{fmt_in}::text, %s::decimal::text", [val, expr]) + cur.execute(f"select %{fmt_in.value}::text, %s::decimal::text", [val, expr]) want, got = cur.fetchone() assert got == want @@ -583,6 +583,6 @@ def test_dump_wrapper_oid(wrapper): @pytest.mark.parametrize("fmt_in", PyFormat) def test_repr_wrapper(conn, wrapper, fmt_in): wrapper = getattr(psycopg.types.numeric, wrapper) - cur = conn.execute(f"select pg_typeof(%{fmt_in})::oid", [wrapper(0)]) + cur = conn.execute(f"select pg_typeof(%{fmt_in.value})::oid", [wrapper(0)]) oid = cur.fetchone()[0] assert oid == psycopg.postgres.types[wrapper.__name__.lower()].oid diff --git a/tests/types/test_range.py b/tests/types/test_range.py index c00124f2b..163b1ef02 100644 --- a/tests/types/test_range.py +++ b/tests/types/test_range.py @@ -61,7 +61,7 @@ range_classes = """ @pytest.mark.parametrize("fmt_in", PyFormat) def test_dump_builtin_empty(conn, pgtype, fmt_in): r = Range(empty=True) # type: ignore[var-annotated] - cur = conn.execute(f"select 'empty'::{pgtype} = %{fmt_in}", (r,)) + cur = conn.execute(f"select 'empty'::{pgtype} = %{fmt_in.value}", (r,)) assert cur.fetchone()[0] is True @@ -70,7 +70,7 @@ def test_dump_builtin_empty(conn, pgtype, fmt_in): def test_dump_builtin_empty_wrapper(conn, wrapper, fmt_in): wrapper = getattr(range_module, wrapper) r = wrapper(empty=True) - cur = conn.execute(f"select 'empty' = %{fmt_in}", (r,)) + cur = conn.execute(f"select 'empty' = %{fmt_in.value}", (r,)) assert cur.fetchone()[0] is True @@ -94,7 +94,7 @@ def test_dump_builtin_array(conn, pgtype, fmt_in): r1 = Range(empty=True) # type: ignore[var-annotated] r2 = Range(bounds="()") # type: ignore[var-annotated] cur = conn.execute( - f"select array['empty'::{pgtype}, '(,)'::{pgtype}] = %{fmt_in}", + f"select array['empty'::{pgtype}, '(,)'::{pgtype}] = %{fmt_in.value}", ([r1, r2],), ) assert cur.fetchone()[0] is True @@ -106,7 +106,8 @@ def test_dump_builtin_array_with_cast(conn, pgtype, fmt_in): r1 = Range(empty=True) # type: ignore[var-annotated] r2 = Range(bounds="()") # type: ignore[var-annotated] cur = conn.execute( - f"select array['empty'::{pgtype}, '(,)'::{pgtype}] = %{fmt_in}::{pgtype}[]", + f"select array['empty'::{pgtype}, '(,)'::{pgtype}] " + f"= %{fmt_in.value}::{pgtype}[]", ([r1, r2],), ) assert cur.fetchone()[0] is True @@ -118,7 +119,7 @@ def test_dump_builtin_array_wrapper(conn, wrapper, fmt_in): wrapper = getattr(range_module, wrapper) r1 = wrapper(empty=True) r2 = wrapper(bounds="()") - cur = conn.execute(f"""select '{{empty,"(,)"}}' = %{fmt_in}""", ([r1, r2],)) + cur = conn.execute(f"""select '{{empty,"(,)"}}' = %{fmt_in.value}""", ([r1, r2],)) assert cur.fetchone()[0] is True @@ -128,7 +129,7 @@ def test_dump_builtin_range(conn, pgtype, min, max, bounds, fmt_in): r = Range(min, max, bounds) # type: ignore[var-annotated] sub = type2sub[pgtype] cur = conn.execute( - f"select {pgtype}(%s::{sub}, %s::{sub}, %s) = %{fmt_in}", + f"select {pgtype}(%s::{sub}, %s::{sub}, %s) = %{fmt_in.value}", (min, max, bounds, r), ) assert cur.fetchone()[0] is True diff --git a/tests/types/test_string.py b/tests/types/test_string.py index ec04b0900..a5f58900a 100644 --- a/tests/types/test_string.py +++ b/tests/types/test_string.py @@ -18,7 +18,7 @@ eur = "\u20ac" def test_dump_1char(conn, fmt_in): cur = conn.cursor() for i in range(1, 256): - cur.execute(f"select %{fmt_in} = chr(%s)", (chr(i), i)) + cur.execute(f"select %{fmt_in.value} = chr(%s)", (chr(i), i)) assert cur.fetchone()[0] is True, chr(i) @@ -46,7 +46,7 @@ def test_dump_zero(conn, fmt_in): cur = conn.cursor() s = "foo\x00bar" with pytest.raises(psycopg.DataError): - cur.execute(f"select %{fmt_in}::text", (s,)) + cur.execute(f"select %{fmt_in.value}::text", (s,)) def test_quote_zero(conn): @@ -94,7 +94,7 @@ def test_dump_enc(conn, fmt_in, encoding): cur = conn.cursor() conn.execute(f"set client_encoding to {encoding}") - (res,) = cur.execute(f"select ascii(%{fmt_in})", (eur,)).fetchone() + (res,) = cur.execute(f"select ascii(%{fmt_in.value})", (eur,)).fetchone() assert res == ord(eur) @@ -104,7 +104,7 @@ def test_dump_badenc(conn, fmt_in): conn.execute("set client_encoding to latin1") with pytest.raises(UnicodeEncodeError): - cur.execute(f"select %{fmt_in}::bytea", (eur,)) + cur.execute(f"select %{fmt_in.value}::bytea", (eur,)) @pytest.mark.parametrize("fmt_in", PyFormat) @@ -113,7 +113,7 @@ def test_dump_utf8_badenc(conn, fmt_in): conn.execute("set client_encoding to utf8") with pytest.raises(UnicodeEncodeError): - cur.execute(f"select %{fmt_in}", ("\uddf8",)) + cur.execute(f"select %{fmt_in.value}", ("\uddf8",)) @pytest.mark.parametrize("fmt_in", [PyFormat.AUTO, PyFormat.TEXT]) @@ -127,7 +127,7 @@ def test_dump_enum(conn, fmt_in): cur = conn.cursor() cur.execute("create type myenum as enum ('foo', 'bar')") cur.execute("create table with_enum (e myenum)") - cur.execute(f"insert into with_enum (e) values (%{fmt_in})", (MyEnum.foo,)) + cur.execute(f"insert into with_enum (e) values (%{fmt_in.value})", (MyEnum.foo,)) (res,) = cur.execute("select e from with_enum").fetchone() assert res == "foo" @@ -137,9 +137,11 @@ def test_dump_text_oid(conn, fmt_in): conn.autocommit = True with pytest.raises(e.IndeterminateDatatype): - conn.execute(f"select concat(%{fmt_in}, %{fmt_in})", ["foo", "bar"]) + conn.execute(f"select concat(%{fmt_in.value}, %{fmt_in.value})", ["foo", "bar"]) conn.adapters.register_dumper(str, psycopg.types.string.StrDumper) - cur = conn.execute(f"select concat(%{fmt_in}, %{fmt_in})", ["foo", "bar"]) + cur = conn.execute( + f"select concat(%{fmt_in.value}, %{fmt_in.value})", ["foo", "bar"] + ) assert cur.fetchone()[0] == "foobar" @@ -208,7 +210,7 @@ def test_text_array(conn, typename, fmt_in, fmt_out): cur = conn.cursor(binary=fmt_out) a = list(map(chr, range(1, 256))) + [eur] - (res,) = cur.execute(f"select %{fmt_in}::{typename}[]", (a,)).fetchone() + (res,) = cur.execute(f"select %{fmt_in.value}::{typename}[]", (a,)).fetchone() assert res == a @@ -219,7 +221,7 @@ def test_text_array_ascii(conn, fmt_in, fmt_out): cur = conn.cursor(binary=fmt_out) a = list(map(chr, range(1, 256))) + [eur] exp = [s.encode() for s in a] - (res,) = cur.execute(f"select %{fmt_in}::text[]", (a,)).fetchone() + (res,) = cur.execute(f"select %{fmt_in.value}::text[]", (a,)).fetchone() assert res == exp @@ -234,10 +236,10 @@ def test_dump_1byte(conn, fmt_in, pytype): cur = conn.cursor() for i in range(0, 256): obj = pytype(bytes([i])) - cur.execute(f"select %{fmt_in} = set_byte('x', 0, %s)", (obj, i)) + cur.execute(f"select %{fmt_in.value} = set_byte('x', 0, %s)", (obj, i)) assert cur.fetchone()[0] is True, i - cur.execute(f"select %{fmt_in} = array[set_byte('x', 0, %s)]", ([obj], i)) + cur.execute(f"select %{fmt_in.value} = array[set_byte('x', 0, %s)]", ([obj], i)) assert cur.fetchone()[0] is True @@ -275,5 +277,5 @@ def test_load_1byte(conn, fmt_out): def test_bytea_array(conn, fmt_in, fmt_out): cur = conn.cursor(binary=fmt_out) a = [bytes(range(0, 256))] - (res,) = cur.execute(f"select %{fmt_in}::bytea[]", (a,)).fetchone() + (res,) = cur.execute(f"select %{fmt_in.value}::bytea[]", (a,)).fetchone() assert res == a diff --git a/tests/types/test_uuid.py b/tests/types/test_uuid.py index 2a7aaf21c..f79b0060d 100644 --- a/tests/types/test_uuid.py +++ b/tests/types/test_uuid.py @@ -13,7 +13,7 @@ from psycopg.adapt import PyFormat def test_uuid_dump(conn, fmt_in): val = "12345678123456781234567812345679" cur = conn.cursor() - cur.execute(f"select %{fmt_in} = %s::uuid", (UUID(val), val)) + cur.execute(f"select %{fmt_in.value} = %s::uuid", (UUID(val), val)) assert cur.fetchone()[0] is True