diff options
author | Stefan Kögl <stefan@skoegl.net> | 2012-09-28 19:01:27 +0200 |
---|---|---|
committer | Stefan Kögl <stefan@skoegl.net> | 2012-09-28 19:06:08 +0200 |
commit | 33ad0dc629b7802fa084afa2a23a9599c224fc40 (patch) | |
tree | b703f489fbb76f297d82ed50edf0e248dbf0fccd | |
parent | d935044158c3bdcd2ca1014166d7ca323cd57c27 (diff) | |
download | python-json-patch-33ad0dc629b7802fa084afa2a23a9599c224fc40.tar.gz |
implement __has__, __eq__ for JsonPatch, PatchOperation
-rw-r--r-- | jsonpatch.py | 23 | ||||
-rwxr-xr-x | tests.py | 16 |
2 files changed, 39 insertions, 0 deletions
diff --git a/jsonpatch.py b/jsonpatch.py index 6aa4878..1244d60 100644 --- a/jsonpatch.py +++ b/jsonpatch.py @@ -191,6 +191,18 @@ class JsonPatch(object): def __iter__(self): return iter(self.patch) + + def __hash__(self): + return hash(frozenset(self.operations)) + + + def __eq__(self, other): + if not isinstance(other, JsonPatch): + return False + + return self.patch == other.patch + + @classmethod def from_string(cls, patch_str): """Creates JsonPatch instance from string source. @@ -354,6 +366,17 @@ class PatchOperation(object): return obj, part_variants[0] + def __hash__(self): + return hash(frozenset(self.operation.items())) + + + def __eq__(self, other): + if not isinstance(other, PatchOperation): + return False + + return self.operation == other.operation + + class RemoveOperation(PatchOperation): """Removes an object property or an array element.""" @@ -137,6 +137,21 @@ class ApplyPatchTestCase(unittest.TestCase): +class EqualityTestCase(unittest.TestCase): + + def test_patch_equality(self): + patch1 = jsonpatch.JsonPatch([{ "op": "add", "path": "/a/b/c", "value": "foo" }]) + patch2 = jsonpatch.JsonPatch([{ "path": "/a/b/c", "op": "add", "value": "foo" }]) + self.assertEqual(patch1, patch2) + + + def test_patch_unequal(self): + patch1 = jsonpatch.JsonPatch([{'op': 'test', 'path': '/test'}]) + patch2 = jsonpatch.JsonPatch([{'op': 'test', 'path': '/test1'}]) + self.assertNotEqual(patch1, patch2) + + + class MakePatchTestCase(unittest.TestCase): @@ -201,6 +216,7 @@ def suite(): suite = unittest.TestSuite() suite.addTest(doctest.DocTestSuite(jsonpatch)) suite.addTest(unittest.makeSuite(ApplyPatchTestCase)) + suite.addTest(unittest.makeSuite(EqualityTestCase)) suite.addTest(unittest.makeSuite(MakePatchTestCase)) return suite |