Skip to content

Commit 9758934

Browse files
committed
Add property based test.
1 parent 18887df commit 9758934

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ build
44
dist
55
*.egg-info/
66
doc/_build
7+
.hypothesis/

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
wheel
22
pypandoc
3+
hypothesis >= 3

tests.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import jsonpatch
1010
import jsonpointer
1111
import sys
12+
import string
13+
from hypothesis import given, note, strategies as st
1214

1315

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

520522

523+
json_st = st.recursive(
524+
st.one_of([
525+
st.none(),
526+
st.booleans(),
527+
st.floats(),
528+
st.text(string.printable)]),
529+
lambda children:
530+
st.lists(children)
531+
| st.dictionaries(st.text(string.printable), children))
532+
533+
534+
class RoundtripTests(unittest.TestCase):
535+
@given(json_st, json_st)
536+
def test_roundtrip(self, src, dst):
537+
patch = jsonpatch.JsonPatch.from_diff(src, dst, False)
538+
note('Patch: %s' % (patch,))
539+
res = patch.apply(src)
540+
self.assertEqual(res, dst)
541+
542+
521543
if __name__ == '__main__':
522544
modules = ['jsonpatch']
523545

@@ -531,6 +553,7 @@ def get_suite():
531553
suite.addTest(unittest.makeSuite(InvalidInputTests))
532554
suite.addTest(unittest.makeSuite(ConflictTests))
533555
suite.addTest(unittest.makeSuite(OptimizationTests))
556+
suite.addTest(unittest.makeSuite(RoundtripTests))
534557
return suite
535558

536559

0 commit comments

Comments
 (0)