@@ -729,6 +729,85 @@ def test_create_with_pointer(self):
729
729
self .assertEqual (result , expected )
730
730
731
731
732
+ class JsonPatchCreationTest (unittest .TestCase ):
733
+
734
+ def test_creation_fails_with_invalid_patch (self ):
735
+ invalid_patches = [
736
+ { 'path' : '/foo' , 'value' : 'bar' },
737
+ {'op' : 0xADD , 'path' : '/foo' , 'value' : 'bar' },
738
+ {'op' : 'boo' , 'path' : '/foo' , 'value' : 'bar' },
739
+ {'op' : 'add' , 'value' : 'bar' },
740
+ ]
741
+ for patch in invalid_patches :
742
+ with self .assertRaises (jsonpatch .InvalidJsonPatch ):
743
+ jsonpatch .JsonPatch ([patch ])
744
+
745
+ with self .assertRaises (jsonpointer .JsonPointerException ):
746
+ jsonpatch .JsonPatch ([{'op' : 'add' , 'path' : 'foo' , 'value' : 'bar' }])
747
+
748
+
749
+ class UtilityMethodTests (unittest .TestCase ):
750
+
751
+ def test_boolean_coercion (self ):
752
+ empty_patch = jsonpatch .JsonPatch ([])
753
+ self .assertFalse (empty_patch )
754
+
755
+ def test_patch_equality (self ):
756
+ p = jsonpatch .JsonPatch ([{'op' : 'add' , 'path' : '/foo' , 'value' : 'bar' }])
757
+ q = jsonpatch .JsonPatch ([{'op' : 'add' , 'path' : '/foo' , 'value' : 'bar' }])
758
+ different_op = jsonpatch .JsonPatch ([{'op' : 'remove' , 'path' : '/foo' }])
759
+ different_path = jsonpatch .JsonPatch ([{'op' : 'add' , 'path' : '/bar' , 'value' : 'bar' }])
760
+ different_value = jsonpatch .JsonPatch ([{'op' : 'add' , 'path' : '/foo' , 'value' : 'foo' }])
761
+ self .assertNotEqual (p , different_op )
762
+ self .assertNotEqual (p , different_path )
763
+ self .assertNotEqual (p , different_value )
764
+ self .assertEqual (p , q )
765
+
766
+ def test_operation_equality (self ):
767
+ add = jsonpatch .AddOperation ({'path' : '/new-element' , 'value' : 'new-value' })
768
+ add2 = jsonpatch .AddOperation ({'path' : '/new-element' , 'value' : 'new-value' })
769
+ rm = jsonpatch .RemoveOperation ({'path' : '/target' })
770
+ self .assertEqual (add , add2 )
771
+ self .assertNotEqual (add , rm )
772
+
773
+ def test_add_operation_structure (self ):
774
+ with self .assertRaises (jsonpatch .InvalidJsonPatch ):
775
+ jsonpatch .AddOperation ({'path' : '/' }).apply ({})
776
+
777
+ def test_replace_operation_structure (self ):
778
+ with self .assertRaises (jsonpatch .InvalidJsonPatch ):
779
+ jsonpatch .ReplaceOperation ({'path' : '/' }).apply ({})
780
+
781
+ with self .assertRaises (jsonpatch .InvalidJsonPatch ):
782
+ jsonpatch .ReplaceOperation ({'path' : '/top/-' , 'value' : 'foo' }).apply ({'top' : {'inner' : 'value' }})
783
+
784
+ with self .assertRaises (jsonpatch .JsonPatchConflict ):
785
+ jsonpatch .ReplaceOperation ({'path' : '/top/missing' , 'value' : 'foo' }).apply ({'top' : {'inner' : 'value' }})
786
+
787
+ def test_move_operation_structure (self ):
788
+ with self .assertRaises (jsonpatch .InvalidJsonPatch ):
789
+ jsonpatch .MoveOperation ({'path' : '/target' }).apply ({})
790
+
791
+ with self .assertRaises (jsonpatch .JsonPatchConflict ):
792
+ jsonpatch .MoveOperation ({'from' : '/source' , 'path' : '/target' }).apply ({})
793
+
794
+ def test_test_operation_structure (self ):
795
+ with self .assertRaises (jsonpatch .JsonPatchTestFailed ):
796
+ jsonpatch .TestOperation ({'path' : '/target' }).apply ({})
797
+
798
+ with self .assertRaises (jsonpatch .InvalidJsonPatch ):
799
+ jsonpatch .TestOperation ({'path' : '/target' }).apply ({'target' : 'value' })
800
+
801
+ def test_copy_operation_structure (self ):
802
+ with self .assertRaises (jsonpatch .InvalidJsonPatch ):
803
+ jsonpatch .CopyOperation ({'path' : '/target' }).apply ({})
804
+
805
+ with self .assertRaises (jsonpatch .JsonPatchConflict ):
806
+ jsonpatch .CopyOperation ({'path' : '/target' , 'from' : '/source' }).apply ({})
807
+
808
+ with self .assertRaises (jsonpatch .JsonPatchConflict ):
809
+ jsonpatch .CopyOperation ({'path' : '/target' , 'from' : '/source' }).apply ({})
810
+
732
811
733
812
if __name__ == '__main__' :
734
813
modules = ['jsonpatch' ]
@@ -745,6 +824,8 @@ def get_suite():
745
824
suite .addTest (unittest .makeSuite (ConflictTests ))
746
825
suite .addTest (unittest .makeSuite (OptimizationTests ))
747
826
suite .addTest (unittest .makeSuite (JsonPointerTests ))
827
+ suite .addTest (unittest .makeSuite (JsonPatchCreationTest ))
828
+ suite .addTest (unittest .makeSuite (UtilityMethodTests ))
748
829
return suite
749
830
750
831
0 commit comments