Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_path_with_null_value(self):
obj = {'bar': 'qux'}
self.assertRaises(jsonpatch.InvalidJsonPatch,
jsonpatch.apply_patch,
obj, '[{"op": "add", "path": null, "value": "bar"}]')
assert patched.updated
for field, value in patched_community_metadata.items():
assert getattr(patched, field) == value
# test invalid or conflicting patchs
with pytest.raises(JsonPatchConflict):
patched.patch([{
'op': 'replace',
'path': '/name',
'value': 'this should not be applied'
}, {
'op': 'replace',
'path': '/non-existing-field',
'value': 'random value'
}])
with pytest.raises(InvalidJsonPatch):
patched.patch({'whatever': 'key'})
with pytest.raises(InvalidCommunityError):
patched.patch([{
'op': 'replace',
'path': '/name',
'value': None
}])
# check that the community was not modified
for field, value in patched_community_metadata.items():
assert getattr(patched, field) == value
def _get_operation(self, operation):
if 'op' not in operation:
raise InvalidJsonPatch("Operation does not contain 'op' member")
op = operation['op']
if not isinstance(op, basestring):
raise InvalidJsonPatch("Operation must be a string")
if op not in self.operations:
raise InvalidJsonPatch("Unknown operation {0!r}".format(op))
cls = self.operations[op]
return cls(operation)
def _get_operation(self, operation):
if 'op' not in operation:
raise InvalidJsonPatch("Operation does not contain 'op' member")
op = operation['op']
if not isinstance(op, basestring):
raise InvalidJsonPatch("Operation must be a string")
if op not in self.operations:
raise InvalidJsonPatch("Unknown operation {0!r}".format(op))
cls = self.operations[op]
return cls(operation)
def _get_operation(self, operation):
if 'op' not in operation:
raise InvalidJsonPatch("Operation does not contain 'op' member")
op = operation['op']
if not isinstance(op, basestring):
raise InvalidJsonPatch("Operation must be a string")
if op not in self.operations:
raise InvalidJsonPatch("Unknown operation {0!r}".format(op))
cls = self.operations[op]
return cls(operation)
def apply(self, obj):
try:
value = self.operation["value"]
except KeyError as ex:
raise InvalidJsonPatch(
"The operation does not contain a 'value' member")
subobj, part = self.pointer.to_last(obj)
if part is None:
return value
if isinstance(subobj, MutableSequence):
if part > len(subobj) or part < 0:
raise JsonPatchConflict("can't replace outside of list")
elif isinstance(subobj, MutableMapping):
if part not in subobj:
msg = "can't replace non-existent object '{0}'".format(part)
raise JsonPatchConflict(msg)
else:
def apply(self, obj):
try:
from_ptr = JsonPointer(self.operation['from'])
except KeyError as ex:
raise InvalidJsonPatch(
"The operation does not contain a 'from' member")
subobj, part = from_ptr.to_last(obj)
try:
value = copy.deepcopy(subobj[part])
except (KeyError, IndexError) as ex:
raise JsonPatchConflict(str(ex))
obj = AddOperation({
'op': 'add',
'path': self.location,
'value': value
}).apply(obj)
return obj