diff options
author | Stefan Kögl <stefan@skoegl.net> | 2013-07-12 16:04:39 +0200 |
---|---|---|
committer | Stefan Kögl <stefan@skoegl.net> | 2013-07-12 16:04:39 +0200 |
commit | 13c888e6bc215482a8be7236db6c2ac7c979e038 (patch) | |
tree | 9bd3a2b084b1d8e5d2b43946f13b68f2277921b2 | |
parent | 2572aa4d2f5f19b7943a6ea81828559afafc9eed (diff) | |
parent | b004e26349cc015742a1f3d9570e2b92c574da2b (diff) | |
download | python-json-patch-13c888e6bc215482a8be7236db6c2ac7c979e038.tar.gz |
Merge branch 'master' of github.com:stefankoegl/python-json-patch
-rw-r--r-- | .coveragerc | 19 | ||||
-rw-r--r-- | .travis.yml | 14 | ||||
-rw-r--r-- | jsonpatch.py | 2 | ||||
-rw-r--r-- | makefile | 17 | ||||
-rwxr-xr-x | tests.py | 54 |
5 files changed, 78 insertions, 28 deletions
diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..2a98e09 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,19 @@ +# .coveragerc to control coverage.py +[run] +branch = True + +[report] +# Regexes for lines to exclude from consideration +exclude_lines = + # Have to re-enable the standard pragma + pragma: no cover + + # No need to test __repr__ + def __repr__ + + # Python 2/3 compatibility + except ImportError + sys.version_info + + # don't include not implemented methods + NotImplementedError diff --git a/.travis.yml b/.travis.yml index 15b2a4a..9473cc9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,13 @@ python: - "3.2" - "3.3" - "pypy" -# command to install dependencies -install: pip install -r requirements.txt -# command to run tests -script: nosetests tests.py + +install: + - pip install -r requirements.txt + - pip install coveralls --use-mirrors + +script: + - coverage run --source=jsonpatch tests.py + +after_script: + - coveralls diff --git a/jsonpatch.py b/jsonpatch.py index d4aade3..c6ee302 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -359,7 +359,7 @@ class AddOperation(PatchOperation): def apply(self, obj): value = self.operation["value"] - subobj, part = self.pointer.to_last(obj, None) + subobj, part = self.pointer.to_last(obj) if isinstance(subobj, list): diff --git a/makefile b/makefile new file mode 100644 index 0000000..01cef8a --- /dev/null +++ b/makefile @@ -0,0 +1,17 @@ + +help: + @echo "jsonpatch" + @echo "Makefile targets" + @echo " - test: run tests" + @echo " - coverage: run tests with coverage" + @echo + @echo "To install jsonpatch, type" + @echo " python setup.py install" + @echo + +test: + python tests.py + +coverage: + coverage run --source=jsonpatch tests.py + coverage report -m @@ -3,6 +3,7 @@ from __future__ import unicode_literals +import json import doctest import unittest import jsonpatch @@ -185,6 +186,19 @@ class EqualityTestCase(unittest.TestCase): self.assertNotEqual(hash(patch1), hash(patch2)) + def test_patch_neq_other_objs(self): + p = [{'op': 'test', 'path': '/test'}] + patch = jsonpatch.JsonPatch(p) + # a patch will always compare not-equal to objects of other types + self.assertFalse(patch == p) + self.assertFalse(patch == None) + + def test_str(self): + patch_obj = [ { "op": "add", "path": "/child", "value": { "grandchild": { } } } ] + patch = jsonpatch.JsonPatch(patch_obj) + + self.assertEqual(json.dumps(patch_obj), str(patch)) + self.assertEqual(json.dumps(patch_obj), patch.to_string()) @@ -248,8 +262,23 @@ class MakePatchTestCase(unittest.TestCase): self.assertEqual(expected, res) +class InvalidInputTests(unittest.TestCase): + + def test_missing_op(self): + # an "op" member is required + src = {"foo": "bar"} + patch_obj = [ { "path": "/child", "value": { "grandchild": { } } } ] + self.assertRaises(jsonpatch.JsonPatchException, jsonpatch.apply_patch, src, patch_obj) + + + def test_invalid_op(self): + # "invalid" is not a valid operation + src = {"foo": "bar"} + patch_obj = [ { "op": "invalid", "path": "/child", "value": { "grandchild": { } } } ] + self.assertRaises(jsonpatch.JsonPatchException, jsonpatch.apply_patch, src, patch_obj) + + modules = ['jsonpatch'] -coverage_modules = [] def get_suite(): @@ -258,6 +287,7 @@ def get_suite(): suite.addTest(unittest.makeSuite(ApplyPatchTestCase)) suite.addTest(unittest.makeSuite(EqualityTestCase)) suite.addTest(unittest.makeSuite(MakePatchTestCase)) + suite.addTest(unittest.makeSuite(InvalidInputTests)) return suite @@ -265,33 +295,11 @@ suite = get_suite() for module in modules: m = __import__(module, fromlist=[module]) - coverage_modules.append(m) suite.addTest(doctest.DocTestSuite(m)) runner = unittest.TextTestRunner(verbosity=1) -try: - import coverage -except ImportError: - coverage = None - -if coverage is not None: - coverage.erase() - coverage.start() - result = runner.run(suite) if not result.wasSuccessful(): sys.exit(1) - -if coverage is not None: - coverage.stop() - coverage.report(coverage_modules) - coverage.erase() - -if coverage is None: - sys.stderr.write(""" -No coverage reporting done (Python module "coverage" is missing) -Please install the python-coverage package to get coverage reporting. -""") - sys.stderr.flush() |