From: Daniele Varrazzo Date: Sun, 2 May 2021 00:23:16 +0000 (+0200) Subject: Add regression test for the infinity date example X-Git-Tag: 3.0.dev0~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1bbe20efa54378e977935ddf57008e03255f8dd8;p=thirdparty%2Fpsycopg.git Add regression test for the infinity date example --- diff --git a/docs/advanced/adapt.rst b/docs/advanced/adapt.rst index 4d7296773..6f359e0e2 100644 --- a/docs/advanced/adapt.rst +++ b/docs/advanced/adapt.rst @@ -57,8 +57,12 @@ configuration. subclass `!Dumper` or `!Loader` you should call the ``.register()`` on the class you created. -For example, suppose you want to work with the "infinity" date which is -available in PostgreSQL but not handled by Python: + +Example: handling inifity date +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Suppose you want to work with the "infinity" date which is available in +PostgreSQL but not handled by Python: .. code:: python diff --git a/tests/types/test_date.py b/tests/types/test_date.py index 0042f475c..0f1981f72 100644 --- a/tests/types/test_date.py +++ b/tests/types/test_date.py @@ -535,6 +535,38 @@ def test_load_interval_overflow(conn, val): cur.fetchone()[0] +def test_infinity_date_example(conn): + # NOTE: this is an example in the docs. Make sure it doesn't regress when + # adding binary datetime adapters + from psycopg3.oids import postgres_types as builtins + from psycopg3.types import DateLoader, DateDumper + + class InfDateDumper(DateDumper): + def dump(self, obj): + if obj == dt.date.max: + return b"infinity" + else: + return super().dump(obj) + + class InfDateLoader(DateLoader): + def load(self, data): + if data == b"infinity": + return dt.date.max + else: + return super().load(data) + + cur = conn.cursor() + InfDateDumper.register(dt.date, cur) + InfDateLoader.register(builtins["date"].oid, cur) + + rec = cur.execute( + "SELECT %s::text, %s::text", [dt.date(2020, 12, 31), dt.date.max] + ).fetchone() + assert rec == ("2020-12-31", "infinity") + rec = cur.execute("select '2020-12-31'::date, 'infinity'::date").fetchone() + assert rec == (dt.date(2020, 12, 31), dt.date(9999, 12, 31)) + + # # Support #