How to use jsonpatch - 10 common examples

To help you get started, we’ve selected a few jsonpatch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stefankoegl / python-json-patch / ext_tests.py View on Github external
def _test(self, test):
        if not 'doc' in test or not 'patch' in test:
            # incomplete
            return

        if test.get('disabled', False):
            # test is disabled
            return

        if 'error' in test:
            self.assertRaises(
                (jsonpatch.JsonPatchException, jsonpatch.JsonPointerException),
                jsonpatch.apply_patch, test['doc'], test['patch']
                )

        else:
            try:
                res = jsonpatch.apply_patch(test['doc'], test['patch'])
            except jsonpatch.JsonPatchException as jpe:
                raise Exception(test.get('comment', '')) from jpe

            # if there is no 'expected' we only verify that applying the patch
            # does not raies an exception
            if 'expected' in test:
                self.assertEquals(res, test['expected'], test.get('comment', ''))
github EUDAT-B2SHARE / b2share / tests / b2share_unit_tests / schemas / test_schemas_api.py View on Github external
with app.app_context():
        created_community = Community.create_community(**community_metadata)
        block_schema = BlockSchema.create_block_schema(created_community.id, 'abc')
        block_schema_id = block_schema.id
        db.session.commit()

        retrieved = BlockSchema.get_block_schema(block_schema_id)
        retrieved.patch([{'op': 'replace', 'path': '/name', 'value': 'patched'}])
        db.session.commit()

    with app.app_context():
        patched = BlockSchema.get_block_schema(block_schema_id)
        assert block_schema_id == patched.id
        assert getattr(patched, 'name') == 'patched'

        with pytest.raises(JsonPatchConflict):
            patched.patch([{'op': 'replace', 'path': '/non_exist_name', 'value': None}])
        assert getattr(patched, 'name') == 'patched'
github EUDAT-B2SHARE / b2share / tests / b2share_unit_tests / communities / test_communities_api.py View on Github external
db.session.commit()

    with app.app_context():
        retrieved = Community.get(id=community_id)
        retrieved.patch(community_patch)
        db.session.commit()

    with app.app_context():
        patched = Community.get(id=community_id)
        assert community_id == patched.id
        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
github stefankoegl / python-json-patch / tests.py View on Github external
def test_arrays(self):
        src = {'numbers': [1, 2, 3], 'other': [1, 3, 4, 5]}
        dst = {'numbers': [1, 3, 4, 5], 'other': [1, 3, 4]}
        patch = jsonpatch.make_patch(src, dst)
        res = patch.apply(src)
        self.assertEqual(res, dst)
github stefankoegl / python-json-patch / tests.py View on Github external
def fn(_src, _dst):
            patch = list(jsonpatch.make_patch(_src, _dst))
            # Check if there are only 'move' operations
            for p in patch:
                self.assertEqual(p['op'], 'move')
            res = jsonpatch.apply_patch(_src, patch)
            self.assertEqual(res, _dst)
github stefankoegl / python-json-patch / tests.py View on Github external
def test_success_if_replace_inside_dict(self):
        src = [{'a': 1, 'foo': {'b': 2, 'd': 5}}]
        dst = [{'a': 1, 'foo': {'b': 3, 'd': 6}}]
        patch = jsonpatch.make_patch(src, dst)
        self.assertEqual(patch.apply(src), dst)
github stefankoegl / python-json-patch / tests.py View on Github external
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)
github stefankoegl / python-json-patch / tests.py View on Github external
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'}})
github stefankoegl / python-json-patch / tests.py View on Github external
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)
github stefankoegl / python-json-patch / tests.py View on Github external
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)