From: Daniele Varrazzo Date: Mon, 19 Sep 2022 10:47:23 +0000 (+0100) Subject: test: verify that dumpers returning None are used correctly in copy X-Git-Tag: 3.2.0~20^2~3 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=f001d3d40f4d13f8beb13964c9888fadb3b9ae4c;p=thirdparty%2Fpsycopg.git test: verify that dumpers returning None are used correctly in copy --- diff --git a/tests/test_copy.py b/tests/test_copy.py index 286e6e440..3abe9bfa1 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -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() diff --git a/tests/test_copy_async.py b/tests/test_copy_async.py index b06e880ae..b0f48458e 100644 --- a/tests/test_copy_async.py +++ b/tests/test_copy_async.py @@ -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()