Use jsonpatch to provide a JSON patch document

The attribute "patch" of a model now returns a string
representing all of the operations that have occurred
on the model since it was created in JSON patch form.
This commit is contained in:
Brian Waldon 2012-11-26 11:41:14 -05:00
parent ec39437bfd
commit c265073862
3 changed files with 52 additions and 0 deletions

View File

@ -1 +1,2 @@
jsonschema>=0.7,<1
jsonpatch==0.10

View File

@ -142,3 +142,47 @@ class TestCore(unittest.TestCase):
self.assertEqual(sweden.changes, {'name': 'Finland'})
sweden['name'] = 'Norway'
self.assertEqual(sweden.changes, {'name': 'Norway'})
def test_patch_no_changes(self):
Country = warlock.model_factory(fixture)
sweden = Country(name='Sweden', population=9379116)
self.assertEqual(sweden.patch, '[]')
def test_patch_alter_value(self):
Country = warlock.model_factory(fixture)
sweden = Country(name='Sweden', population=9379116)
sweden['name'] = 'Finland'
self.assertEqual(
sweden.patch,
'[{"path": "/name", "value": "Finland", "op": "replace"}]')
def test_patch_drop_attribute(self):
Country = warlock.model_factory(fixture)
sweden = Country(name='Sweden', population=9379116)
del sweden['name']
self.assertEqual(sweden.patch, '[{"path": "/name", "op": "remove"}]')
def test_patch_reduce_operations(self):
Country = warlock.model_factory(fixture)
sweden = Country(name='Sweden', population=9379116)
sweden['name'] = 'Finland'
self.assertEqual(
sweden.patch,
'[{"path": "/name", "value": "Finland", "op": "replace"}]')
sweden['name'] = 'Norway'
self.assertEqual(
sweden.patch,
'[{"path": "/name", "value": "Norway", "op": "replace"}]')
def test_patch_multiple_operations(self):
Country = warlock.model_factory(fixture)
sweden = Country(name='Sweden', population=9379116)
sweden['name'] = 'Finland'
sweden['population'] = 5387000
self.assertEqual(
sweden.patch,
'[{"path": "/name", "value": "Finland", "op": "replace"}, '
'{"path": "/population", "value": 5387000, "op": "replace"}]')

View File

@ -3,6 +3,7 @@
import copy
import jsonschema
import jsonpatch
class InvalidOperation(RuntimeError):
@ -43,6 +44,7 @@ def model_factory(schema):
dict.__init__(self, d)
self.__dict__['changes'] = {}
self.__dict__['__original__'] = copy.deepcopy(d)
def __getattr__(self, key):
try:
@ -118,5 +120,10 @@ def model_factory(schema):
def changes(self):
return copy.deepcopy(self.__dict__['changes'])
@property
def patch(self):
original = self.__dict__['__original__']
return jsonpatch.make_patch(original, dict(self)).to_string()
Model.__name__ = str(schema['name'])
return Model