Skip to content

Add property based test. #63

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build
dist
*.egg-info/
doc/_build
.hypothesis/
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ python:

install:
- travis_retry pip install -r requirements.txt
- if [ "$TRAVIS_PYTHON_VERSION" != "2.6" -a "$TRAVIS_PYTHON_VERSION" != "3.2" -a "$TRAVIS_PYTHON_VERSION" != "3.3" ]; then travis_retry pip install hypothesis; fi
- if [ "$TRAVIS_PYTHON_VERSION" == "3.2" ]; then travis_retry pip install 'coverage<4'; fi
- travis_retry pip install coveralls

Expand Down
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
36 changes: 36 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
import jsonpatch
import jsonpointer
import sys
import string

try:
import hypothesis
from hypothesis import strategies as st
except ImportError:
hypothesis = None

try:
unicode
except NameError:
unicode = str


class ApplyPatchTestCase(unittest.TestCase):
Expand Down Expand Up @@ -518,6 +530,28 @@ def test_replace_missing(self):
self.assertRaises(jsonpatch.JsonPatchConflict, jsonpatch.apply_patch, src, patch_obj)


if hypothesis is not None:
json_st = st.recursive(
st.one_of([
st.none(),
st.booleans(),
st.floats(allow_nan=False),
st.text(string.printable)]),
lambda children:
st.lists(children)
| st.dictionaries(st.text(string.printable), children))

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


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

Expand All @@ -531,6 +565,8 @@ def get_suite():
suite.addTest(unittest.makeSuite(InvalidInputTests))
suite.addTest(unittest.makeSuite(ConflictTests))
suite.addTest(unittest.makeSuite(OptimizationTests))
if hypothesis is not None:
suite.addTest(unittest.makeSuite(RoundtripTests))
return suite


Expand Down