From 1f8980f0535c1edb0c9c3edd46a052886670d175 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 8 Apr 2020 02:30:33 +1200 Subject: [PATCH] Added tests for all numeric types arrays --- tests/types/test_numeric.py | 58 ++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/tests/types/test_numeric.py b/tests/types/test_numeric.py index 955c9382f..331f90630 100644 --- a/tests/types/test_numeric.py +++ b/tests/types/test_numeric.py @@ -63,13 +63,21 @@ def test_adapt_int_binary(): @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 @@ -149,17 +157,29 @@ def test_adapt_float_binary(): @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( @@ -249,6 +269,16 @@ def test_numeric_as_float(conn, val): 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 @@ -263,8 +293,16 @@ def test_roundtrip_bool(conn, b, fmt_in, fmt_out): 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): -- 2.47.3