Skip to content

Commit b44e7a2

Browse files
committed
Add tests for operation doc structure.
1 parent a7ef7e8 commit b44e7a2

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

tests.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,69 @@ def test_creation_fails_with_invalid_patch(self):
688688
jsonpatch.JsonPatch([{'op': 'add', 'path': 'foo', 'value': 'bar'}])
689689

690690

691+
class UtilityMethodTests(unittest.TestCase):
692+
693+
def test_boolean_coercion(self):
694+
empty_patch = jsonpatch.JsonPatch([])
695+
self.assertFalse(empty_patch)
696+
697+
def test_patch_equality(self):
698+
p = jsonpatch.JsonPatch([{'op': 'add', 'path': '/foo', 'value': 'bar'}])
699+
q = jsonpatch.JsonPatch([{'op': 'add', 'path': '/foo', 'value': 'bar'}])
700+
different_op = jsonpatch.JsonPatch([{'op': 'remove', 'path': '/foo'}])
701+
different_path = jsonpatch.JsonPatch([{'op': 'add', 'path': '/bar', 'value': 'bar'}])
702+
different_value = jsonpatch.JsonPatch([{'op': 'add', 'path': '/foo', 'value': 'foo'}])
703+
self.assertNotEqual(p, different_op)
704+
self.assertNotEqual(p, different_path)
705+
self.assertNotEqual(p, different_value)
706+
self.assertEqual(p, q)
707+
708+
def test_operation_equality(self):
709+
add = jsonpatch.AddOperation({'path': '/new-element', 'value': 'new-value'})
710+
add2 = jsonpatch.AddOperation({'path': '/new-element', 'value': 'new-value'})
711+
rm = jsonpatch.RemoveOperation({'path': '/target'})
712+
self.assertEqual(add, add2)
713+
self.assertNotEqual(add, rm)
714+
715+
def test_add_operation_structure(self):
716+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
717+
jsonpatch.AddOperation({'path': '/'}).apply({})
718+
719+
def test_replace_operation_structure(self):
720+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
721+
jsonpatch.ReplaceOperation({'path': '/'}).apply({})
722+
723+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
724+
jsonpatch.ReplaceOperation({'path': '/top/-', 'value': 'foo'}).apply({'top': {'inner': 'value'}})
725+
726+
with self.assertRaises(jsonpatch.JsonPatchConflict):
727+
jsonpatch.ReplaceOperation({'path': '/top/missing', 'value': 'foo'}).apply({'top': {'inner': 'value'}})
728+
729+
def test_move_operation_structure(self):
730+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
731+
jsonpatch.MoveOperation({'path': '/target'}).apply({})
732+
733+
with self.assertRaises(jsonpatch.JsonPatchConflict):
734+
jsonpatch.MoveOperation({'from': '/source', 'path': '/target'}).apply({})
735+
736+
def test_test_operation_structure(self):
737+
with self.assertRaises(jsonpatch.JsonPatchTestFailed):
738+
jsonpatch.TestOperation({'path': '/target'}).apply({})
739+
740+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
741+
jsonpatch.TestOperation({'path': '/target'}).apply({'target': 'value'})
742+
743+
def test_copy_operation_structure(self):
744+
with self.assertRaises(jsonpatch.InvalidJsonPatch):
745+
jsonpatch.CopyOperation({'path': '/target'}).apply({})
746+
747+
with self.assertRaises(jsonpatch.JsonPatchConflict):
748+
jsonpatch.CopyOperation({'path': '/target', 'from': '/source'}).apply({})
749+
750+
with self.assertRaises(jsonpatch.JsonPatchConflict):
751+
jsonpatch.CopyOperation({'path': '/target', 'from': '/source'}).apply({})
752+
753+
691754
if __name__ == '__main__':
692755
modules = ['jsonpatch']
693756

@@ -704,6 +767,7 @@ def get_suite():
704767
suite.addTest(unittest.makeSuite(OptimizationTests))
705768
suite.addTest(unittest.makeSuite(JsonPointerTests))
706769
suite.addTest(unittest.makeSuite(JsonPatchCreationTest))
770+
suite.addTest(unittest.makeSuite(UtilityMethodTests))
707771
return suite
708772

709773

0 commit comments

Comments
 (0)