@pytest.mark.parametrize("fmt_out", [Format.TEXT, Format.BINARY])
def test_cast_int(conn, val, pgtype, want, fmt_out):
cur = conn.cursor(binary=fmt_out == Format.BINARY)
- cur.execute("select %%s::%s" % pgtype, (val,))
+ cur.execute(f"select %s::{pgtype}", (val,))
assert cur.pgresult.fformat(0) == fmt_out
assert cur.pgresult.ftype(0) == builtins[pgtype].oid
result = cur.fetchone()[0]
assert result == want
assert type(result) is type(want)
+ # arrays work too
+ cur.execute(f"select array[%s::{pgtype}]", (val,))
+ assert cur.pgresult.fformat(0) == fmt_out
+ assert cur.pgresult.ftype(0) == builtins[pgtype].array_oid
+ result = cur.fetchone()[0]
+ assert result == [want]
+ assert type(result[0]) is type(want)
+
#
# Tests with float
@pytest.mark.parametrize("fmt_out", [Format.TEXT, Format.BINARY])
def test_cast_float(conn, val, pgtype, want, fmt_out):
cur = conn.cursor(binary=fmt_out == Format.BINARY)
- cur.execute("select %%s::%s" % pgtype, (val,))
+ cur.execute(f"select %s::{pgtype}", (val,))
assert cur.pgresult.fformat(0) == fmt_out
+ assert cur.pgresult.ftype(0) == builtins[pgtype].oid
result = cur.fetchone()[0]
- assert type(result) is type(want)
- if isnan(want):
- assert isnan(result)
- elif isinf(want):
- assert isinf(result)
- assert (result < 0) is (want < 0)
- else:
- assert result == want
+
+ def check(result, want):
+ assert type(result) is type(want)
+ if isnan(want):
+ assert isnan(result)
+ elif isinf(want):
+ assert isinf(result)
+ assert (result < 0) is (want < 0)
+ else:
+ assert result == want
+
+ check(result, want)
+
+ cur.execute(f"select array[%s::{pgtype}]", (val,))
+ assert cur.pgresult.fformat(0) == fmt_out
+ assert cur.pgresult.ftype(0) == builtins[pgtype].array_oid
+ result = cur.fetchone()[0]
+ assert isinstance(result, list)
+ check(result[0], want)
@pytest.mark.parametrize(
else:
assert result == pytest.approx(float(val))
+ # the customization works with arrays too
+ cur.execute("select %s", ([val],))
+ result = cur.fetchone()[0]
+ assert isinstance(result, list)
+ assert isinstance(result[0], float)
+ if val.is_nan():
+ assert isnan(result[0])
+ else:
+ assert result[0] == pytest.approx(float(val))
+
#
# Mixed tests
ph = "%s" if fmt_in == Format.TEXT else "%b"
result = cur.execute(f"select {ph}", (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 {ph}", ([b],)).fetchone()[0]
+ assert cur.pgresult.fformat(0) == fmt_out
+ if b is not None:
+ assert cur.pgresult.ftype(0) == builtins["bool"].array_oid
+ assert result[0] is b
+
@pytest.mark.parametrize("pgtype", [None, "float8", "int8", "numeric"])
def test_minus_minus(conn, pgtype):