def test_apply_patch_with_wrong_keys_raises_400(self): instance_data = {} patch_list = [ { 'wrong key': 'any value' } ] with self.assertRaises(HTTPError) as error: apply_patch(instance_data, patch_list) msg = str(error.exception) expected = "HTTP 400: Bad Request (Incorrect patch item. Every object in the list must contain the following keys: ['op', 'path'])" self.assertEqual(msg, expected)
def test_apply_patch_add_inexistent_data_succeeds(self): instance_data = {} patch_list = [{ u'path': 'http://on.to/children', u'op': u'add', u'value': u'Mary' }] computed = apply_patch(instance_data, patch_list) expected = { u'http://on.to/children': [u'Mary'], } self.assertEqual(computed, expected)
def test_apply_patch_remove_succeeds_for_expanded_uri(self): instance_data = { 'http://dbpedia.org/ontology/name': u'Francis' } patch_list = [ { u'path': 'dbpedia:name', u'op': u'remove' } ] computed = apply_patch(instance_data, patch_list) expected = {} self.assertEqual(computed, expected)
def test_apply_patch_remove_succeeds_for_inexistent_key(self): instance_data = { 'http://on.to/name': u'Flipper', 'http://on.to/weight': 200.0 } patch_list = [ { u'path': 'http://on.to/age', u'op': u'remove' } ] computed = apply_patch(instance_data, patch_list) self.assertEqual(computed, instance_data)
def test_apply_patch_replace_succeeds(self): instance_data = { u'http://on.to/name': u'Flipper', u'http://on.to/age': 4 } patch_list = [{ u'path': u'http://on.to/age', u'value': 5, u'op': u'replace' }] computed = apply_patch(instance_data, patch_list) expected = {u'http://on.to/name': u'Flipper', u'http://on.to/age': 5} self.assertEqual(computed, expected)
def test_apply_patch_add_inexistent_data_succeeds(self): instance_data = { } patch_list = [ { u'path': 'http://on.to/children', u'op': u'add', u'value': u'Mary' } ] computed = apply_patch(instance_data, patch_list) expected = { u'http://on.to/children': [u'Mary'], } self.assertEqual(computed, expected)
def test_apply_patch_remove_succeeds(self): instance_data = { 'http://on.to/name': u'Flipper', 'http://on.to/weight': 200.0 } patch_list = [ { u'path': 'http://on.to/weight', u'op': u'remove' } ] computed = apply_patch(instance_data, patch_list) expected = { u'http://on.to/name': u'Flipper', } self.assertEqual(computed, expected)
def test_apply_patch_add_list_data_succeeds(self): instance_data = { 'http://on.to/children': ['Dave', 'Eric'] } patch_list = [ { u'path': 'http://on.to/children', u'op': u'add', u'value': [u'Mary', u'John'] } ] computed = apply_patch(instance_data, patch_list) expected = { u'http://on.to/children': ['Dave', 'Eric', 'John', 'Mary'], } self.assertEqual(computed, expected)
def test_apply_patch_replace_succeeds(self): instance_data = { u'http://on.to/name': u'Flipper', u'http://on.to/age': 4 } patch_list = [ { u'path': u'http://on.to/age', u'value': 5, u'op': u'replace' } ] computed = apply_patch(instance_data, patch_list) expected = { u'http://on.to/name': u'Flipper', u'http://on.to/age': 5 } self.assertEqual(computed, expected)
def test_apply_patch_replace_with_add_semantics(self): instance_data = { u'http://on.to/name': u'Flipper', u'http://on.to/age': 4 } patch_list = [ { u'path': u'http://on.to/birthDate', u'value': '2014-08-01', u'op': u'replace' } ] computed = apply_patch(instance_data, patch_list) expected = { u'http://on.to/name': u'Flipper', u'http://on.to/age': 4, u'http://on.to/birthDate': '2014-08-01' } self.assertEqual(computed, expected)
def patch(self, context_name, class_name, instance_id): valid_params = INSTANCE_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, context_name=context_name, class_name=class_name, instance_id=instance_id, **valid_params) del context_name del class_name del instance_id try: patch_list = json.loads(self.request.body) except ValueError: raise HTTPError(400, log_message=_("No JSON object could be decoded")) # Retrieve original data instance_data = memoize(self.query_params, get_instance, key=build_instance_key(self.query_params), function_arguments=self.query_params) try: instance_data = instance_data['body'] except TypeError: raise HTTPError(404, log_message=_("Inexistent instance")) instance_data.pop('http://www.w3.org/1999/02/22-rdf-syntax-ns#type', None) # compute patch changed_data = apply_patch(instance_data, patch_list) # Try to put edit_instance(self.query_params, changed_data) status = 200 # Clear cache cache.purge_an_instance(self.query_params['instance_uri']) self.finalize(status)
def patch(self, context_name, class_name, instance_id): valid_params = INSTANCE_PARAMS with safe_params(valid_params): self.query_params = ParamDict(self, context_name=context_name, class_name=class_name, instance_id=instance_id, **valid_params) del context_name del class_name del instance_id try: patch_list = json.loads(self.request.body) except ValueError: raise HTTPError(400, log_message=_("No JSON object could be decoded")) # Retrieve original data instance_data = memoize(self.query_params, get_instance, key=build_instance_key(self.query_params), function_arguments=self.query_params) try: instance_data = instance_data['body'] except TypeError: raise HTTPError(404, log_message=_("Inexistent instance")) instance_data.pop('http://www.w3.org/1999/02/22-rdf-syntax-ns#type', None) # compute patch changed_data = apply_patch(instance_data, patch_list) # Try to put edit_instance(self.query_params, changed_data) status = 200 # Clear cache cache.purge_an_instance(self.query_params['instance_uri']) self.finalize(status)