]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
test: verify that dumpers returning None are used correctly in copy
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 19 Sep 2022 10:47:23 +0000 (11:47 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 1 Jun 2024 11:07:21 +0000 (13:07 +0200)
tests/test_copy.py
tests/test_copy_async.py

index 286e6e4408ab5ec29d6f5bcbb5f292eda0e23345..3abe9bfa1aec26bd774a186b29914de43377f923 100644 (file)
@@ -24,6 +24,7 @@ from .utils import eur
 from ._test_copy import sample_text, sample_binary, sample_binary_rows  # noqa
 from ._test_copy import sample_values, sample_records, sample_tabledef
 from ._test_copy import ensure_table, py_to_raw, special_chars, FileWriter
+from .test_adapt import StrNoneDumper, StrNoneBinaryDumper
 
 pytestmark = pytest.mark.crdb_skip("copy")
 
@@ -325,6 +326,29 @@ def test_subclass_adapter(conn, format):
     assert rec[0] == "hellohello"
 
 
+@pytest.mark.parametrize("format", Format)
+def test_subclass_nulling_dumper(conn, format):
+    Base: type = StrNoneDumper if format == Format.TEXT else StrNoneBinaryDumper
+
+    class MyStrDumper(Base):  # type: ignore
+
+        def dump(self, obj):
+            return super().dump(obj) if obj else None
+
+    conn.adapters.register_dumper(str, MyStrDumper)
+
+    cur = conn.cursor()
+    ensure_table(cur, sample_tabledef)
+
+    with cur.copy(f"copy copy_in (data) from stdin (format {format.name})") as copy:
+        copy.write_row(("hello",))
+        copy.write_row(("",))
+
+    cur.execute("select data from copy_in order by col1")
+    recs = cur.fetchall()
+    assert recs == [("hello",), (None,)]
+
+
 @pytest.mark.parametrize("format", Format)
 def test_copy_in_error_empty(conn, format):
     cur = conn.cursor()
index b06e880ae5ba771351716b3e28de6f02422fb053..b0f48458e8bbc2ba8f36effeef1b8cc157dd0262 100644 (file)
@@ -22,6 +22,7 @@ from .acompat import alist
 from ._test_copy import sample_text, sample_binary, sample_binary_rows  # noqa
 from ._test_copy import sample_values, sample_records, sample_tabledef
 from ._test_copy import ensure_table_async, py_to_raw, special_chars, AsyncFileWriter
+from .test_adapt import StrNoneDumper, StrNoneBinaryDumper
 
 pytestmark = pytest.mark.crdb_skip("copy")
 
@@ -336,6 +337,30 @@ async def test_subclass_adapter(aconn, format):
     assert rec[0] == "hellohello"
 
 
+@pytest.mark.parametrize("format", Format)
+async def test_subclass_nulling_dumper(aconn, format):
+    Base: type = StrNoneDumper if format == Format.TEXT else StrNoneBinaryDumper
+
+    class MyStrDumper(Base):  # type: ignore
+        def dump(self, obj):
+            return super().dump(obj) if obj else None
+
+    aconn.adapters.register_dumper(str, MyStrDumper)
+
+    cur = aconn.cursor()
+    await ensure_table_async(cur, sample_tabledef)
+
+    async with cur.copy(
+        f"copy copy_in (data) from stdin (format {format.name})"
+    ) as copy:
+        await copy.write_row(("hello",))
+        await copy.write_row(("",))
+
+    await cur.execute("select data from copy_in order by col1")
+    recs = await cur.fetchall()
+    assert recs == [("hello",), (None,)]
+
+
 @pytest.mark.parametrize("format", Format)
 async def test_copy_in_error_empty(aconn, format):
     cur = aconn.cursor()