summaryrefslogtreecommitdiff
path: root/jsonpatch.py
diff options
context:
space:
mode:
authorAlexander Shorin <kxepal@gmail.com>2011-12-25 16:28:19 +0400
committerAlexander Shorin <kxepal@gmail.com>2011-12-25 16:28:19 +0400
commit07b0af061f286b2f93a7e4848c19cdf91a8387b8 (patch)
treed7a2b82f79b0c4185a0b9b57903177b22a537888 /jsonpatch.py
parent602b550919264358d3eaa26d05f4d3c28eb3dced (diff)
downloadpython-json-patch-07b0af061f286b2f93a7e4848c19cdf91a8387b8.tar.gz
Add support for test operation
Diffstat (limited to 'jsonpatch.py')
-rw-r--r--jsonpatch.py31
1 files changed, 30 insertions, 1 deletions
diff --git a/jsonpatch.py b/jsonpatch.py
index d77f4e4..db4dd75 100644
--- a/jsonpatch.py
+++ b/jsonpatch.py
@@ -73,7 +73,8 @@ class JsonPatch(object):
'remove': RemoveOperation,
'add': AddOperation,
'replace': ReplaceOperation,
- 'move': MoveOperation
+ 'move': MoveOperation,
+ 'test': TestOperation
}
@@ -256,3 +257,31 @@ class MoveOperation(PatchOperation):
value = subobj[part]
RemoveOperation(self.location, self.operation).apply(obj)
AddOperation(self.operation['to'], {'value': value}).apply(obj)
+
+
+class TestOperation(PatchOperation):
+ """ Test value by specified location
+
+ >>> obj = {'baz': 'qux', 'foo': ['a', 2, 'c']}
+ >>> patch = JsonPatch([
+ ... {'test': '/baz', 'value': 'qux'},
+ ... {'test': '/foo/1', 'value': 2}
+ ... ])
+ >>> patch.apply(obj)
+ {'foo': ['a', 2, 'c'], 'baz': 'qux'}
+
+ >>> patch = JsonPatch([
+ ... {'test': '/foo/1', 'value': 'BOOM!'}
+ ... ])
+ >>> try:
+ ... patch.apply(obj)
+ ... except AssertionError:
+ ... pass
+ ... else:
+ ... assert False, 'test should fall'
+ """
+
+ def apply(self, obj):
+ value = self.operation['value']
+ subobj, part = self.locate(obj, self.location)
+ assert subobj[part] == value