From: Daniele Varrazzo Date: Sun, 26 Sep 2021 19:41:08 +0000 (+0200) Subject: Better tests of dumping after set_types X-Git-Tag: 3.0~57^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=77123e1a81381532fa51af9511af2d08e46a2396;p=thirdparty%2Fpsycopg.git Better tests of dumping after set_types 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). --- diff --git a/tests/fix_faker.py b/tests/fix_faker.py index f36e18c40..547ac2ba8 100644 --- a/tests/fix_faker.py +++ b/tests/fix_faker.py @@ -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) diff --git a/tests/test_copy.py b/tests/test_copy.py index 20439e32c..043eb860f 100644 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -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) diff --git a/tests/test_copy_async.py b/tests/test_copy_async.py index 182fb7365..37ab5e1f8 100644 --- a/tests/test_copy_async.py +++ b/tests/test_copy_async.py @@ -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)