From: Daniele Varrazzo Date: Fri, 8 Jan 2021 01:57:35 +0000 (+0100) Subject: Added test to verify that the C adapters are registered correctly X-Git-Tag: 3.0.dev0~195 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e89323abe17404755d8e7b919b93ae116c6718b8;p=thirdparty%2Fpsycopg.git Added test to verify that the C adapters are registered correctly --- diff --git a/tests/test_adapt.py b/tests/test_adapt.py index e6e365221..f549c3c5e 100644 --- a/tests/test_adapt.py +++ b/tests/test_adapt.py @@ -1,5 +1,6 @@ import pytest +import psycopg3 from psycopg3.adapt import Transformer, Format, Dumper, Loader from psycopg3.oids import builtins, TEXT_OID @@ -201,6 +202,54 @@ def test_return_untyped(conn, fmt_in): assert cur.execute("select data from testjson").fetchone() == ({},) +def test_optimised_adapters(): + if psycopg3.pq.__impl__ == "python": + pytest.skip("test C module only") + + from psycopg3_c import _psycopg3 + + # All the optimised adapters available + c_adapters = {} + for n in dir(_psycopg3): + if n.startswith("_") or n in ("CDumper", "CLoader"): + continue + obj = getattr(_psycopg3, n) + if not isinstance(obj, type): + continue + if not issubclass(obj, (_psycopg3.CDumper, _psycopg3.CLoader)): + continue + c_adapters[n] = obj + + # All the registered adapters + reg_adapters = set() + adapters = ( + psycopg3.global_adapters._dumpers + psycopg3.global_adapters._loaders + ) + assert len(adapters) == 4 + for m in adapters: + reg_adapters |= set(m.values()) + + # Check that the registered adapters are the optimised one + n = 0 + for cls in reg_adapters: + if cls.__name__ in c_adapters: + assert cls is c_adapters[cls.__name__] + n += 1 + + assert n >= 10 + + # Check that every optimised adapter is the optimised version of a Py one + for n in dir(psycopg3.types): + obj = getattr(psycopg3.types, n) + if not isinstance(obj, type): + continue + if not issubclass(obj, (Dumper, Loader)): + continue + c_adapters.pop(obj.__name__, None) + + assert not c_adapters + + def make_dumper(suffix): """Create a test dumper appending a suffix to the bytes representation."""