From f39e1ca56443f014e58e3a406d4609b3a38f88c9 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 5 May 2021 19:47:14 +0200 Subject: [PATCH] Add Decimal values to random tests --- tests/fix_faker.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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 -- 2.47.2