]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Better tests of dumping after set_types
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 26 Sep 2021 19:41:08 +0000 (21:41 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 26 Sep 2021 19:51:39 +0000 (21:51 +0200)
These tests reveal that When there is a 1-n mapping between oid and
python classes, we might pick a dumper that cannot really deal with the
object to dump (for instance trying to dump an int to numeric with the
Decimal dumper).

tests/fix_faker.py
tests/test_copy.py
tests/test_copy_async.py

index f36e18c40881c7aaf609d09f2782728135ede943..547ac2ba875a2ab7d3cf7d6055b1321196891e41 100644 (file)
@@ -77,7 +77,7 @@ class Faker:
         return self._types
 
     @property
-    def types_names(self):
+    def types_names_sql(self):
         if self._types_names:
             return self._types_names
 
@@ -90,6 +90,14 @@ class Faker:
         self._types_names = types
         return types
 
+    @property
+    def types_names(self):
+        types = [
+            t.as_string(self.conn).replace('"', "")
+            for t in self.types_names_sql
+        ]
+        return types
+
     def _get_type_name(self, tx, schema, value):
         # Special case it as it is passed as unknown so is returned as text
         if schema == (list, str):
@@ -111,7 +119,7 @@ class Faker:
     @property
     def create_stmt(self):
         fields = []
-        for name, type in zip(self.fields_names, self.types_names):
+        for name, type in zip(self.fields_names, self.types_names_sql):
             fields.append(sql.SQL("{} {}").format(name, type))
 
         fields = sql.SQL(", ").join(fields)
index 20439e32cd4a9a85321355fc2ad90f7c08ec9b1b..043eb860f32d0c8220adc347c1f82070f0c3983d 100644 (file)
@@ -537,9 +537,12 @@ def test_worker_life(conn, format, buffer):
 
 
 @pytest.mark.slow
-@pytest.mark.parametrize("fmt", [Format.TEXT, Format.BINARY])
+@pytest.mark.parametrize(
+    "fmt, set_types",
+    [(Format.TEXT, True), (Format.TEXT, False), (Format.BINARY, True)],
+)
 @pytest.mark.parametrize("method", ["read", "iter", "row", "rows"])
-def test_copy_to_leaks(dsn, faker, fmt, method, retries):
+def test_copy_to_leaks(dsn, faker, fmt, set_types, method, retries):
     faker.format = PyFormat.from_pq(fmt)
     faker.choose_schema(ncols=20)
     faker.make_records(20)
@@ -561,11 +564,8 @@ def test_copy_to_leaks(dsn, faker, fmt, method, retries):
                 )
 
                 with cur.copy(stmt) as copy:
-                    types = [
-                        t.as_string(conn).replace('"', "")
-                        for t in faker.types_names
-                    ]
-                    copy.set_types(types)
+                    if set_types:
+                        copy.set_types(faker.types_names)
 
                     if method == "read":
                         while 1:
@@ -597,8 +597,11 @@ def test_copy_to_leaks(dsn, faker, fmt, method, retries):
 
 
 @pytest.mark.slow
-@pytest.mark.parametrize("fmt", [Format.TEXT, Format.BINARY])
-def test_copy_from_leaks(dsn, faker, fmt, retries):
+@pytest.mark.parametrize(
+    "fmt, set_types",
+    [(Format.TEXT, True), (Format.TEXT, False), (Format.BINARY, True)],
+)
+def test_copy_from_leaks(dsn, faker, fmt, set_types, retries):
     faker.format = PyFormat.from_pq(fmt)
     faker.choose_schema(ncols=20)
     faker.make_records(20)
@@ -615,6 +618,8 @@ def test_copy_from_leaks(dsn, faker, fmt, retries):
                     sql.SQL(fmt.name),
                 )
                 with cur.copy(stmt) as copy:
+                    if set_types:
+                        copy.set_types(faker.types_names)
                     for row in faker.records:
                         copy.write_row(row)
 
index 182fb7365ca52d35fd9a21d159cb006407d6a560..37ab5e1f8f5bc423cbb0aff65908b83efef8fb02 100644 (file)
@@ -530,9 +530,12 @@ async def test_worker_life(aconn, format, buffer):
 
 
 @pytest.mark.slow
-@pytest.mark.parametrize("fmt", [Format.TEXT, Format.BINARY])
+@pytest.mark.parametrize(
+    "fmt, set_types",
+    [(Format.TEXT, True), (Format.TEXT, False), (Format.BINARY, True)],
+)
 @pytest.mark.parametrize("method", ["read", "iter", "row", "rows"])
-async def test_copy_to_leaks(dsn, faker, fmt, method, retries):
+async def test_copy_to_leaks(dsn, faker, fmt, set_types, method, retries):
     faker.format = PyFormat.from_pq(fmt)
     faker.choose_schema(ncols=20)
     faker.make_records(20)
@@ -554,11 +557,8 @@ async def test_copy_to_leaks(dsn, faker, fmt, method, retries):
                 )
 
                 async with cur.copy(stmt) as copy:
-                    types = [
-                        t.as_string(conn).replace('"', "")
-                        for t in faker.types_names
-                    ]
-                    copy.set_types(types)
+                    if set_types:
+                        copy.set_types(faker.types_names)
 
                     if method == "read":
                         while 1:
@@ -590,8 +590,11 @@ async def test_copy_to_leaks(dsn, faker, fmt, method, retries):
 
 
 @pytest.mark.slow
-@pytest.mark.parametrize("fmt", [Format.TEXT, Format.BINARY])
-async def test_copy_from_leaks(dsn, faker, fmt, retries):
+@pytest.mark.parametrize(
+    "fmt, set_types",
+    [(Format.TEXT, True), (Format.TEXT, False), (Format.BINARY, True)],
+)
+async def test_copy_from_leaks(dsn, faker, fmt, set_types, retries):
     faker.format = PyFormat.from_pq(fmt)
     faker.choose_schema(ncols=20)
     faker.make_records(20)
@@ -608,6 +611,8 @@ async def test_copy_from_leaks(dsn, faker, fmt, retries):
                     sql.SQL(fmt.name),
                 )
                 async with cur.copy(stmt) as copy:
+                    if set_types:
+                        copy.set_types(faker.types_names)
                     for row in faker.records:
                         await copy.write_row(row)