From: Daniele Varrazzo Date: Wed, 5 May 2021 17:47:14 +0000 (+0200) Subject: Add Decimal values to random tests X-Git-Tag: 3.0.dev0~48^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f39e1ca56443f014e58e3a406d4609b3a38f88c9;p=thirdparty%2Fpsycopg.git Add Decimal values to random tests --- diff --git a/tests/fix_faker.py b/tests/fix_faker.py index 0120a7cae..2555047b1 100644 --- a/tests/fix_faker.py +++ b/tests/fix_faker.py @@ -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