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")
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()
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")
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()