From 296f35a1160c7299eaa7d8af103c2b2bff1cee1a Mon Sep 17 00:00:00 2001 From: Kurt Rose <=> Date: Thu, 6 Dec 2018 06:15:40 -0800 Subject: [PATCH] first cut at a (somewhat) useful hypothesis test --- glom/test/test_auto.py | 51 ++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 2 files changed, 52 insertions(+) create mode 100644 glom/test/test_auto.py diff --git a/glom/test/test_auto.py b/glom/test/test_auto.py new file mode 100644 index 00000000..960f1421 --- /dev/null +++ b/glom/test/test_auto.py @@ -0,0 +1,51 @@ +''' +Use hypothesis and contracts to automatically search for flaws. +''' +from hypothesis import settings +from hypothesis.stateful import RuleBasedStateMachine, invariant, rule + +from glom import glom, Inspect, Coalesce, T + + +class SpecAndTarget(RuleBasedStateMachine): + ''' + auto generate a spec and target that should work together + need more sophisticated strategies to make this more useful + (probably not mindless recursion to an arbitrary depth) + ''' + def __init__(self): + super(SpecAndTarget, self).__init__() + self.spec = T + self.target = None + self.result = None + + @invariant() + def spec_works(self): + assert glom(self.target, self.spec) == self.result + + @rule() + def add_inspect(self): + self.spec = Inspect(self.spec) + + @rule() + def add_list(self): + self.spec = [self.spec] + self.target = [self.target, self.target] + self.result = [self.result, self.result] + + @rule() + def add_dict(self): + self.spec = {'key': self.spec} + self.result = {'key': self.result} + + @rule() + def add_coalesce(self): + self.spec = Coalesce(self.spec) + + @rule() + def add_tuple(self): + self.spec = (self.spec, T) + + +TestSpecAndTarget = SpecAndTarget.TestCase +TestSpecAndTarget.settings = settings(max_examples=20, stateful_step_count=50) diff --git a/requirements.txt b/requirements.txt index 8f80deaa..392a039d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,6 +3,7 @@ boltons==17.2.0 coverage==4.5.1 face>=0.1.0 funcsigs==1.0.2 +hypothesis==3.82.1 more-itertools==4.1.0 pluggy==0.6.0 py==1.5.2