From d27ea8ca4595f6b7298b274aa8637fc17662c0e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20K=C3=B6gl?= Date: Mon, 17 Sep 2012 20:13:01 +0200 Subject: update "test" behaviour according to latest spec --- README | 2 +- jsonpatch.py | 19 +++++++++++++++---- tests.py | 30 +++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/README b/README index 1364eab..06af07e 100644 --- a/README +++ b/README @@ -3,7 +3,7 @@ python-json-patch: Applying JSON Patches ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Library to apply JSON Patches according to -http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-03 +http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-04 See Sourcecode for Examples diff --git a/jsonpatch.py b/jsonpatch.py index d102e7e..4dc73d7 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -31,7 +31,7 @@ # """Apply JSON-Patches according to -http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-03""" +http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-04""" # Will be parsed by setup.py to determine package metadata __author__ = 'Stefan Kögl ' @@ -63,6 +63,9 @@ class JsonPatchConflict(JsonPatchException): - etc. """ +class JsonPatchTestFailed(JsonPatchException, AssertionError): + """ A Test operation failed """ + def apply_patch(doc, patch, in_place=False): """Apply list of patches to specified json document. @@ -412,9 +415,17 @@ class TestOperation(PatchOperation): """Test value by specified location.""" def apply(self, obj): - value = self.operation['value'] - subobj, part = self.locate(obj, self.location) - assert subobj[part] == value + try: + subobj, part = self.locate(obj, self.location) + except JsonPatchConflict, c: + raise JsonPatchTestFailed(str(c)) + + val = subobj[part] + + if 'value' in self.operation: + value = self.operation['value'] + if val != value: + raise JsonPatchTestFailed('%s is not equal to tested value %s' % (val, value)) class CopyOperation(PatchOperation): diff --git a/tests.py b/tests.py index fd07f9d..340c10a 100755 --- a/tests.py +++ b/tests.py @@ -105,11 +105,39 @@ class ApplyPatchTestCase(unittest.TestCase): def test_test_error(self): obj = {'bar': 'qux'} - self.assertRaises(AssertionError, + self.assertRaises(jsonpatch.JsonPatchTestFailed, jsonpatch.apply_patch, obj, [{'test': '/bar', 'value': 'bar'}]) + def test_test_not_existing(self): + obj = {'bar': 'qux'} + self.assertRaises(jsonpatch.JsonPatchTestFailed, + jsonpatch.apply_patch, + obj, [{'test': '/baz', 'value': 'bar'}]) + + + def test_test_noval_existing(self): + obj = {'bar': 'qux'} + jsonpatch.apply_patch(obj, [{'test': '/bar'}]) + + + def test_test_noval_not_existing(self): + obj = {'bar': 'qux'} + self.assertRaises(jsonpatch.JsonPatchTestFailed, + jsonpatch.apply_patch, + obj, [{'test': '/baz'}]) + + + def test_test_noval_not_existing_nested(self): + obj = {'bar': {'qux': 2}} + self.assertRaises(jsonpatch.JsonPatchTestFailed, + jsonpatch.apply_patch, + obj, [{'test': '/baz/qx'}]) + + + + class MakePatchTestCase(unittest.TestCase): def test_apply_patch_to_copy(self): -- cgit v1.2.1