]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Added test to verify queries failing if int is dumped as int8 or numeric
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 20 Jan 2021 01:56:52 +0000 (02:56 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 20 Jan 2021 01:59:34 +0000 (02:59 +0100)
I wanted these so badly.

tests/test_adapt.py

index a32ea2e4f2da75f2ab1780f6bf99e395d68d9c31..1782e939e102af985b4742c9a0f3685d51a4c5d5 100644 (file)
@@ -1,3 +1,5 @@
+import datetime as dt
+
 import pytest
 
 import psycopg3
@@ -228,7 +230,7 @@ def test_none_type_argument(conn, fmt_in):
     assert cur.fetchone()[0]
 
 
-@pytest.mark.parametrize("fmt_in", [Format.TEXT, Format.BINARY])
+@pytest.mark.parametrize("fmt_in", [Format.AUTO, Format.TEXT, Format.BINARY])
 def test_return_untyped(conn, fmt_in):
     # Analyze and check for changes using strings in untyped/typed contexts
     cur = conn.cursor()
@@ -243,6 +245,16 @@ def test_return_untyped(conn, fmt_in):
     assert cur.execute("select data from testjson").fetchone() == ({},)
 
 
+@pytest.mark.parametrize("fmt_in", [Format.AUTO, Format.TEXT, Format.BINARY])
+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])
+    assert cur.fetchone()[0] == dt.date(2021, 1, 4)
+
+    cur = conn.execute("select '[10, 20, 30]'::jsonb -> %s", [1])
+    assert cur.fetchone()[0] == 20
+
+
 def test_optimised_adapters():
     if psycopg3.pq.__impl__ == "python":
         pytest.skip("test C module only")