Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property-based tests with Hypothesis #79

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Next Next commit
Add property based test.
  • Loading branch information
mithrandi committed Jul 6, 2017
commit 975893443cc7e1c258c82e1272fbc3fd7268d1df
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -4,3 +4,4 @@ build
dist
*.egg-info/
doc/_build
.hypothesis/
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
wheel
pypandoc
hypothesis >= 3
23 changes: 23 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@
import jsonpatch
import jsonpointer
import sys
import string
from hypothesis import given, note, strategies as st


class ApplyPatchTestCase(unittest.TestCase):
@@ -518,6 +520,26 @@ def test_replace_missing(self):
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, src, patch_obj)


json_st = st.recursive(
st.one_of([
st.none(),
st.booleans(),
st.floats(),
st.text(string.printable)]),
lambda children:
st.lists(children)
| st.dictionaries(st.text(string.printable), children))


class RoundtripTests(unittest.TestCase):
@given(json_st, json_st)
def test_roundtrip(self, src, dst):
patch = jsonpatch.JsonPatch.from_diff(src, dst, False)
note('Patch: %s' % (patch,))
res = patch.apply(src)
self.assertEqual(res, dst)


if __name__ == '__main__':
modules = ['jsonpatch']

@@ -531,6 +553,7 @@ def get_suite():
suite.addTest(unittest.makeSuite(InvalidInputTests))
suite.addTest(unittest.makeSuite(ConflictTests))
suite.addTest(unittest.makeSuite(OptimizationTests))
suite.addTest(unittest.makeSuite(RoundtripTests))
return suite