]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
test: more exhaustive test of difficult names/attrs in composites
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Tue, 27 Sep 2022 08:51:11 +0000 (09:51 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 28 Sep 2022 00:45:59 +0000 (01:45 +0100)
tests/test_rows.py
tests/types/test_composite.py

index 600a7e678976fa75897c064d5137a9b406cd4404..5165b80074b0f8f1b658e840a2ebeba5e4a48e60 100644 (file)
@@ -3,6 +3,8 @@ import pytest
 import psycopg
 from psycopg import rows
 
+from .utils import eur
+
 
 def test_tuple_row(conn):
     conn.row_factory = rows.dict_row
@@ -56,11 +58,14 @@ def test_namedtuple_row(conn):
     assert r2.number == 1
     assert not cur.nextset()
     assert type(r1) is not type(r2)
-    cur.execute('select 1 as üåäö, 1 as _, 1 as "123"')
+
+    cur.execute(f'select 1 as üåäö, 2 as _, 3 as "123", 4 as "a-b", 5 as "{eur}eur"')
     (r3,) = cur.fetchall()
-    assert "üåäö" in r3._fields
-    assert "f_" in r3._fields
-    assert "f123" in r3._fields
+    assert r3.üåäö == 1
+    assert r3.f_ == 2
+    assert r3.f123 == 3
+    assert r3.a_b == 4
+    assert r3.f_eur == 5
 
 
 def test_class_row(conn):
index 5a0f437e75d9a147908de91b3a6d19a8db95dce0..47beecf352158b973ce72589bd8c093c84a9efa5 100644 (file)
@@ -353,11 +353,13 @@ def test_invalid_fields_names(conn):
     assert obj == got
 
 
-@pytest.mark.parametrize("name", ["a-b", f"{eur}", "order"])
+@pytest.mark.parametrize("name", ["a-b", f"{eur}", "order", "1", "'"])
 def test_literal_invalid_name(conn, name):
     conn.execute("set client_encoding to utf8")
-    conn.execute(f'create type "{name}" as (foo text)')
-    info = CompositeInfo.fetch(conn, f'"{name}"')
+    conn.execute(
+        sql.SQL("create type {name} as (foo text)").format(name=sql.Identifier(name))
+    )
+    info = CompositeInfo.fetch(conn, sql.Identifier(name).as_string(conn))
     register_composite(info, conn)
     obj = info.python_type("hello")
     assert sql.Literal(obj).as_string(conn) == f"'(hello)'::\"{name}\""
@@ -365,3 +367,30 @@ def test_literal_invalid_name(conn, name):
     got = cur.fetchone()[0]
     assert got == obj
     assert type(got) is type(obj)
+
+
+@pytest.mark.parametrize(
+    "name, attr",
+    [
+        ("a-b", "a_b"),
+        (f"{eur}", "f_"),
+        ("üåäö", "üåäö"),
+        ("order", "order"),
+        ("1", "f1"),
+    ],
+)
+def test_literal_invalid_attr(conn, name, attr):
+    conn.execute("set client_encoding to utf8")
+    conn.execute(
+        sql.SQL("create type test_attr as ({name} text)").format(
+            name=sql.Identifier(name)
+        )
+    )
+    info = CompositeInfo.fetch(conn, "test_attr")
+    register_composite(info, conn)
+    obj = info.python_type("hello")
+    assert getattr(obj, attr) == "hello"
+    cur = conn.execute(sql.SQL("select {}").format(obj))
+    got = cur.fetchone()[0]
+    assert got == obj
+    assert type(got) is type(obj)