From: Daniele Varrazzo Date: Thu, 23 Sep 2021 01:15:52 +0000 (+0200) Subject: Improve range random testing X-Git-Tag: 3.0~66 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=11fd7e90b26925f3ec2eab4593af81fcc37a9cd8;p=thirdparty%2Fpsycopg.git Improve range random testing Make sure to create a numrange[] field instead of text, which would have happened if the random selection in Faker.types_names() had picked an array whose first object is an empty range. Also extend the tests to certain binary range types which previously couldn't be managed. --- diff --git a/tests/fix_faker.py b/tests/fix_faker.py index ea3f5d570..f36e18c40 100644 --- a/tests/fix_faker.py +++ b/tests/fix_faker.py @@ -210,7 +210,7 @@ class Faker: def make_record(self, nulls=0): if not nulls: - return tuple(self.make(spec) for spec in self.schema) + return tuple(self.example(spec) for spec in self.schema) else: return tuple( self.make(spec) if random() > nulls else None @@ -294,6 +294,15 @@ class Faker: # spec can be a type or a tuple (type, options) return self.get_maker(spec)(spec) + def example(self, spec): + # A good representative of the object - no degenerate case + cls = spec if isinstance(spec, type) else spec[0] + meth = self._get_method("example", cls) + if meth: + return meth(spec) + else: + return self.make(spec) + def match_any(self, spec, got, want): assert got == want @@ -497,29 +506,23 @@ class Faker: def schema_Range(self, cls): subtypes = [ Decimal, + Int4, + Int8, dt.date, (dt.datetime, True), (dt.datetime, False), ] - # TODO: learn to dump numeric ranges in binary - if self.format != PyFormat.BINARY: - subtypes.extend([Int4, Int8]) return (cls, choice(subtypes)) - def make_Range(self, spec): - # TODO: drop format check after fixing binary dumping of empty ranges - if ( - random() < 0.02 - and spec[0] is Range - and self.format == PyFormat.TEXT - ): + def make_Range(self, spec, empty_chance=0.02, no_bound_chance=0.05): + if random() < empty_chance and spec[0] is Range: return spec[0](empty=True) while True: bounds = [] while len(bounds) < 2: - if random() < 0.05: + if random() < no_bound_chance: bounds.append(None) continue @@ -545,6 +548,9 @@ class Faker: r = spec[0](bounds[0], bounds[1], choice("[(") + choice("])")) return r + def example_Range(self, spec): + return self.make_Range(spec, empty_chance=0, no_bound_chance=0) + def make_Int4Range(self, spec): return self.make_Range((spec, Int4))