Skip to content

Commit fce420c

Browse files
committed
Merge branch 'property-tests' of https://github.com/mithrandi/python-json-patch into mithrandi-property-tests
2 parents 4ff7398 + 6f10715 commit fce420c

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-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/

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ python:
1313
install:
1414
- travis_retry pip install -r requirements.txt
1515
- travis_retry pip install coveralls
16+
- if [ "$TRAVIS_PYTHON_VERSION" != "3.3" ]; then travis_retry pip install hypothesis; fi
1617
script:
1718
- coverage run --source=jsonpointer tests.py
1819
after_script:

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: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
import jsonpatch
1010
import jsonpointer
1111
import sys
12+
import string
13+
14+
try:
15+
import hypothesis
16+
from hypothesis import strategies as st
17+
except ImportError:
18+
hypothesis = None
19+
20+
try:
21+
unicode
22+
except NameError:
23+
unicode = str
1224

1325

1426
class ApplyPatchTestCase(unittest.TestCase):
@@ -608,6 +620,28 @@ def test_replace_missing(self):
608620
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, src, patch_obj)
609621

610622

623+
if hypothesis is not None:
624+
json_st = st.recursive(
625+
st.one_of([
626+
st.none(),
627+
st.booleans(),
628+
st.floats(allow_nan=False),
629+
st.text(string.printable)]),
630+
lambda children:
631+
st.lists(children)
632+
| st.dictionaries(st.text(string.printable), children))
633+
634+
class RoundtripTests(unittest.TestCase):
635+
@hypothesis.example({}, {unicode('%20'): None})
636+
@hypothesis.example({unicode('%20'): None}, {})
637+
@hypothesis.given(json_st, json_st)
638+
def test_roundtrip(self, src, dst):
639+
patch = jsonpatch.JsonPatch.from_diff(src, dst, False)
640+
hypothesis.note('Patch: %s' % (patch,))
641+
res = patch.apply(src)
642+
self.assertEqual(res, dst)
643+
644+
611645
if __name__ == '__main__':
612646
modules = ['jsonpatch']
613647

@@ -622,6 +656,8 @@ def get_suite():
622656
suite.addTest(unittest.makeSuite(InvalidInputTests))
623657
suite.addTest(unittest.makeSuite(ConflictTests))
624658
suite.addTest(unittest.makeSuite(OptimizationTests))
659+
if hypothesis is not None:
660+
suite.addTest(unittest.makeSuite(RoundtripTests))
625661
return suite
626662

627663

0 commit comments

Comments
 (0)