]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Add regression test for the infinity date example
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 2 May 2021 00:23:16 +0000 (02:23 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 2 May 2021 00:29:58 +0000 (02:29 +0200)
docs/advanced/adapt.rst
tests/types/test_date.py

index 4d7296773ce08ef9fd8ccf90f987816739e0d03b..6f359e0e2de28099bfad7872e0e2b3594c4cd92a 100644 (file)
@@ -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
 
index 0042f475c6cd7df4a8283b7c4d7bfd65f4118e5e..0f1981f72d0810c65fd17f25a09a7f24aa3b135e 100644 (file)
@@ -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
 #