]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Add Decimal values to random tests
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Wed, 5 May 2021 17:47:14 +0000 (19:47 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Thu, 6 May 2021 17:09:56 +0000 (19:09 +0200)
tests/fix_faker.py

index 0120a7caefdfbd5c81e3e983c2d0482729fcc958..2555047b1ac1dd1d967b2268654f9f17358efbf5 100644 (file)
@@ -2,6 +2,7 @@ import importlib
 from math import isnan
 from uuid import UUID
 from random import choice, random, randrange
+from decimal import Decimal
 from collections import deque
 
 import pytest
@@ -225,6 +226,34 @@ class Faker:
         length = randrange(self.str_max_length)
         return spec(bytes([randrange(256) for i in range(length)]))
 
+    def make_Decimal(self, spec):
+        if random() >= 0.99:
+            if self.conn.info.server_version >= 140000:
+                return Decimal(choice(["NaN", "Inf", "-Inf"]))
+            else:
+                return Decimal("NaN")
+
+        sign = choice("+-")
+        num = choice(["0.zd", "d", "d.d"])
+        while "z" in num:
+            ndigits = randrange(1, 20)
+            num = num.replace("z", "0" * ndigits, 1)
+        while "d" in num:
+            ndigits = randrange(1, 20)
+            num = num.replace(
+                "d", "".join([str(randrange(10)) for i in range(ndigits)]), 1
+            )
+        expsign = choice(["e+", "e-", ""])
+        exp = randrange(20) if expsign else ""
+        rv = Decimal(f"{sign}{num}{expsign}{exp}")
+        return rv
+
+    def match_Decimal(self, spec, got, want):
+        if got is not None and got.is_nan():
+            assert want.is_nan()
+        else:
+            assert got == want
+
     def make_float(self, spec):
         if random() <= 0.99:
             # this exponent should generate no inf