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_arrays_one_element_sequences(self):
""" Tests the case of multiple common one element sequences inside an array """
# see https://github.com/stefankoegl/python-json-patch/issues/30#issuecomment-155070128
src = [1,2,3]
dst = [3,1,4,2]
patch = jsonpatch.make_patch(src, dst)
res = jsonpatch.apply_patch(src, patch)
self.assertEqual(res, dst)
def test_move_object_key(self):
obj = {'foo': {'bar': 'baz', 'waldo': 'fred'},
'qux': {'corge': 'grault'}}
res = jsonpatch.apply_patch(obj, [{'op': 'move', 'from': '/foo/waldo',
'path': '/qux/thud'}])
self.assertEqual(res, {'qux': {'thud': 'fred', 'corge': 'grault'},
'foo': {'bar': 'baz'}})
def test_add_nested(self):
# see http://tools.ietf.org/html/draft-ietf-appsawg-json-patch-03#appendix-A.10
src = {"foo": "bar"}
patch_obj = [ { "op": "add", "path": "/child", "value": { "grandchild": { } } } ]
res = jsonpatch.apply_patch(src, patch_obj)
expected = { "foo": "bar",
"child": { "grandchild": { } }
}
self.assertEqual(expected, res)
def test_issue103(self):
"""In JSON 1 is different from 1.0 even though in python 1 == 1.0"""
src = {'A': 1}
dst = {'A': 1.0}
patch = jsonpatch.make_patch(src, dst)
res = jsonpatch.apply_patch(src, patch)
self.assertEqual(res, dst)
self.assertIsInstance(res['A'], float)
def test_add_array_item(self):
obj = {'foo': ['bar', 'baz']}
res = jsonpatch.apply_patch(obj, [{'op': 'add', 'path': '/foo/1', 'value': 'qux'}])
self.assertEqual(res['foo'], ['bar', 'qux', 'baz'])
def apply_patch(doc, patch):
return jsonpatch.apply_patch(doc, patch)
def patch(self, patch):
"""Patch record metadata.
:params patch: Dictionary of record metadata.
:returns: A new :class:`Record` instance.
"""
data = apply_patch(dict(self), patch)
return self.__class__(data, model=self.model)
try:
rt = pecan.request.indexer.get_resource_type(self._name)
except indexer.NoSuchResourceType as e:
abort(404, e)
enforce("update resource type", rt)
# Ensure this is a valid jsonpatch dict
patch = deserialize_and_validate(
ResourceTypeJsonPatchSchema,
expected_content_types=["application/json-patch+json"])
# Add new attributes to the resource type
rt_json_current = rt.jsonify()
try:
rt_json_next = jsonpatch.apply_patch(rt_json_current, patch)
except jsonpatch.JsonPatchException as e:
abort(400, e)
del rt_json_next['state']
# Validate that the whole new resource_type is valid
schema = pecan.request.indexer.get_resource_type_schema()
try:
rt_json_next = voluptuous.Schema(schema.for_update, required=True)(
rt_json_next)
except voluptuous.Error as e:
abort(400, "Invalid input: %s" % e)
# Get only newly formatted and deleted attributes
add_attrs = {k: v for k, v in rt_json_next["attributes"].items()
if k not in rt_json_current["attributes"]}
del_attrs = [k for k in rt_json_current["attributes"]