]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added tests for all numeric types arrays
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 7 Apr 2020 14:30:33 +0000 (02:30 +1200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 7 Apr 2020 14:30:33 +0000 (02:30 +1200)
tests/types/test_numeric.py

index 955c9382fdf50e885447ed022908b553a62a0228..331f906308fd15f486d910b7a9096b2db591bc56 100644 (file)
@@ -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):