@@ -1533,6 +1533,35 @@ def test_copy_from_pointer(self):
15331533
15341534class JsonPatchCreationTest (unittest .TestCase ):
15351535
1536+ def test_creation_rejects_non_sequence_documents (self ):
1537+ for patch in ({}, {'op' : 'add' , 'path' : '/a' , 'value' : 1 },
1538+ MappingProxyType ({}), '' , b'' , None , True , False ,
1539+ 0 , 1 , 1.5 ):
1540+ with self .subTest (patch = patch ):
1541+ with self .assertRaises (jsonpatch .InvalidJsonPatch ):
1542+ jsonpatch .JsonPatch (patch )
1543+
1544+ def test_serialized_patch_rejects_non_array_documents (self ):
1545+ for patch in ({}, {'op' : 'add' , 'path' : '/a' , 'value' : 1 },
1546+ '' , 'text' , None , True , False , 0 , 1 , 1.5 ):
1547+ for serialized in (json .dumps (patch ), json .dumps (patch ).encode ()):
1548+ with self .subTest (patch = serialized ):
1549+ with self .assertRaises (jsonpatch .InvalidJsonPatch ):
1550+ jsonpatch .JsonPatch .from_string (serialized )
1551+ doc = {'a' : 'unchanged' }
1552+ with self .assertRaises (jsonpatch .InvalidJsonPatch ):
1553+ jsonpatch .apply_patch (doc , serialized , in_place = True )
1554+ self .assertEqual (doc , {'a' : 'unchanged' })
1555+
1556+ def test_creation_preserves_operation_sequences (self ):
1557+ for operations in ([], (),
1558+ [{'op' : 'add' , 'path' : '/a' , 'value' : 1 }],
1559+ ({'op' : 'add' , 'path' : '/a' , 'value' : 1 },)):
1560+ with self .subTest (operations = operations ):
1561+ patch = jsonpatch .JsonPatch (operations )
1562+ self .assertIs (patch .patch , operations )
1563+ self .assertEqual (patch .apply ({}), {'a' : 1 } if operations else {})
1564+
15361565 def test_creation_fails_with_invalid_patch (self ):
15371566 invalid_patches = [
15381567 { 'path' : '/foo' , 'value' : 'bar' },
0 commit comments