summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Shawley <daveshawley@gmail.com>2020-11-17 08:17:07 -0500
committerDave Shawley <daveshawley@gmail.com>2020-11-17 08:19:38 -0500
commitb44e7a2031ad5cbe0a0d5ad2ab0763b7b9b8dc25 (patch)
treeb35eaa9b437c12d59a4aee455cb71e6ed7efe352
parenta7ef7e80d0024b71794c22fd09e35389c04de964 (diff)
downloadpython-json-patch-b44e7a2031ad5cbe0a0d5ad2ab0763b7b9b8dc25.tar.gz
Add tests for operation doc structure.
-rwxr-xr-xtests.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/tests.py b/tests.py
index a7c1a43..58e3ce2 100755
--- a/tests.py
+++ b/tests.py
@@ -688,6 +688,69 @@ class JsonPatchCreationTest(unittest.TestCase):
jsonpatch.JsonPatch([{'op': 'add', 'path': 'foo', 'value': 'bar'}])
+class UtilityMethodTests(unittest.TestCase):
+
+ def test_boolean_coercion(self):
+ empty_patch = jsonpatch.JsonPatch([])
+ self.assertFalse(empty_patch)
+
+ def test_patch_equality(self):
+ p = jsonpatch.JsonPatch([{'op': 'add', 'path': '/foo', 'value': 'bar'}])
+ q = jsonpatch.JsonPatch([{'op': 'add', 'path': '/foo', 'value': 'bar'}])
+ different_op = jsonpatch.JsonPatch([{'op': 'remove', 'path': '/foo'}])
+ different_path = jsonpatch.JsonPatch([{'op': 'add', 'path': '/bar', 'value': 'bar'}])
+ different_value = jsonpatch.JsonPatch([{'op': 'add', 'path': '/foo', 'value': 'foo'}])
+ self.assertNotEqual(p, different_op)
+ self.assertNotEqual(p, different_path)
+ self.assertNotEqual(p, different_value)
+ self.assertEqual(p, q)
+
+ def test_operation_equality(self):
+ add = jsonpatch.AddOperation({'path': '/new-element', 'value': 'new-value'})
+ add2 = jsonpatch.AddOperation({'path': '/new-element', 'value': 'new-value'})
+ rm = jsonpatch.RemoveOperation({'path': '/target'})
+ self.assertEqual(add, add2)
+ self.assertNotEqual(add, rm)
+
+ def test_add_operation_structure(self):
+ with self.assertRaises(jsonpatch.InvalidJsonPatch):
+ jsonpatch.AddOperation({'path': '/'}).apply({})
+
+ def test_replace_operation_structure(self):
+ with self.assertRaises(jsonpatch.InvalidJsonPatch):
+ jsonpatch.ReplaceOperation({'path': '/'}).apply({})
+
+ with self.assertRaises(jsonpatch.InvalidJsonPatch):
+ jsonpatch.ReplaceOperation({'path': '/top/-', 'value': 'foo'}).apply({'top': {'inner': 'value'}})
+
+ with self.assertRaises(jsonpatch.JsonPatchConflict):
+ jsonpatch.ReplaceOperation({'path': '/top/missing', 'value': 'foo'}).apply({'top': {'inner': 'value'}})
+
+ def test_move_operation_structure(self):
+ with self.assertRaises(jsonpatch.InvalidJsonPatch):
+ jsonpatch.MoveOperation({'path': '/target'}).apply({})
+
+ with self.assertRaises(jsonpatch.JsonPatchConflict):
+ jsonpatch.MoveOperation({'from': '/source', 'path': '/target'}).apply({})
+
+ def test_test_operation_structure(self):
+ with self.assertRaises(jsonpatch.JsonPatchTestFailed):
+ jsonpatch.TestOperation({'path': '/target'}).apply({})
+
+ with self.assertRaises(jsonpatch.InvalidJsonPatch):
+ jsonpatch.TestOperation({'path': '/target'}).apply({'target': 'value'})
+
+ def test_copy_operation_structure(self):
+ with self.assertRaises(jsonpatch.InvalidJsonPatch):
+ jsonpatch.CopyOperation({'path': '/target'}).apply({})
+
+ with self.assertRaises(jsonpatch.JsonPatchConflict):
+ jsonpatch.CopyOperation({'path': '/target', 'from': '/source'}).apply({})
+
+ with self.assertRaises(jsonpatch.JsonPatchConflict):
+ jsonpatch.CopyOperation({'path': '/target', 'from': '/source'}).apply({})
+
+
if __name__ == '__main__':
modules = ['jsonpatch']
@@ -704,6 +767,7 @@ if __name__ == '__main__':
suite.addTest(unittest.makeSuite(OptimizationTests))
suite.addTest(unittest.makeSuite(JsonPointerTests))
suite.addTest(unittest.makeSuite(JsonPatchCreationTest))
+ suite.addTest(unittest.makeSuite(UtilityMethodTests))
return suite