@@ -1987,6 +1987,95 @@ def test_copy_from_pointer(self):
19871987 ])
19881988 self .assertEqual (patch .apply ({'foo' : 1 }), {'foo' : 1 , 'bar' : 1 })
19891989
1990+ def pointer_and_string_operations (self , pointer_cls = jsonpointer .JsonPointer ,
1991+ paths = ('/foo' , '/bar/0' , '' )):
1992+ foo , bar , root = paths
1993+ with_strings = [
1994+ {'op' : 'add' , 'path' : foo , 'value' : [1 ]},
1995+ {'op' : 'move' , 'from' : foo , 'path' : bar },
1996+ {'op' : 'copy' , 'from' : root , 'path' : foo },
1997+ {'op' : 'copy' , 'from' : foo , 'path' : bar },
1998+ ]
1999+ with_pointers = [
2000+ {'op' : 'add' , 'path' : pointer_cls (foo ), 'value' : [1 ]},
2001+ {'op' : 'move' , 'from' : pointer_cls (foo ), 'path' : pointer_cls (bar )},
2002+ {'op' : 'copy' , 'from' : pointer_cls (root ), 'path' : foo },
2003+ {'op' : 'copy' , 'from' : foo , 'path' : pointer_cls (bar )},
2004+ ]
2005+ return with_pointers , with_strings
2006+
2007+ def test_patch_with_pointers_is_serializable (self ):
2008+ with_pointers , with_strings = self .pointer_and_string_operations ()
2009+ patch = jsonpatch .JsonPatch (with_pointers )
2010+ self .assertEqual (patch .patch , with_strings )
2011+ self .assertEqual (list (patch ), with_strings )
2012+ self .assertEqual (json .loads (patch .to_string ()), with_strings )
2013+ self .assertEqual (json .loads (str (patch )), with_strings )
2014+ self .assertEqual (jsonpatch .JsonPatch .from_string (patch .to_string ()), patch )
2015+
2016+ def test_patch_with_pointers_equals_patch_with_strings (self ):
2017+ with_pointers , with_strings = self .pointer_and_string_operations ()
2018+ patch = jsonpatch .JsonPatch (with_pointers )
2019+ other = jsonpatch .JsonPatch (with_strings )
2020+ self .assertEqual (patch , other )
2021+ self .assertEqual (hash (patch ), hash (other ))
2022+ self .assertEqual (len ({patch , other }), 1 )
2023+ doc = {'bar' : []}
2024+ self .assertEqual (patch .apply (doc ), other .apply (doc ))
2025+
2026+ def test_patch_with_pointers_does_not_change_the_operations (self ):
2027+ with_pointers , _ = self .pointer_and_string_operations ()
2028+ pointers = [(op , member , op [member ]) for op in with_pointers
2029+ for member in ('path' , 'from' )
2030+ if isinstance (op .get (member ), jsonpointer .JsonPointer )]
2031+ jsonpatch .JsonPatch (with_pointers ).apply ({'bar' : []})
2032+ self .assertEqual (len (pointers ), 5 )
2033+ for op , member , pointer in pointers :
2034+ self .assertIs (op [member ], pointer )
2035+
2036+ def test_patch_with_pointers_as_read_only_mappings (self ):
2037+ with_pointers , with_strings = self .pointer_and_string_operations ()
2038+ patch = jsonpatch .JsonPatch (map (MappingProxyType , with_pointers ))
2039+ self .assertEqual (patch .patch , with_strings )
2040+ self .assertTrue (all (type (op ) is dict for op in patch ))
2041+
2042+ def test_patch_with_pointers_of_custom_class (self ):
2043+ with_pointers , with_strings = self .pointer_and_string_operations (
2044+ DotJsonPointer , ('foo' , 'bar.0' , '' ))
2045+ patch = jsonpatch .JsonPatch (with_pointers , pointer_cls = DotJsonPointer )
2046+ other = jsonpatch .JsonPatch (with_strings , pointer_cls = DotJsonPointer )
2047+ self .assertEqual (patch .patch , with_strings )
2048+ self .assertEqual (patch , other )
2049+ self .assertEqual (patch .apply ({'bar' : []}), other .apply ({'bar' : []}))
2050+
2051+ def test_operation_with_pointers_equals_operation_with_strings (self ):
2052+ with_pointers , with_strings = self .pointer_and_string_operations ()
2053+ for pointers , strings in zip (with_pointers , with_strings ):
2054+ with self .subTest (operation = strings ):
2055+ cls = jsonpatch .JsonPatch .operations [strings ['op' ]]
2056+ operation , other = cls (pointers ), cls (strings )
2057+ self .assertEqual (operation , other )
2058+ self .assertEqual (other , operation )
2059+ self .assertEqual (hash (operation ), hash (other ))
2060+ self .assertEqual (operation .location , other .location )
2061+ # setters change the operation it was created from, so it
2062+ # is not replaced by a copy without pointers
2063+ self .assertIs (operation .operation , pointers )
2064+ self .assertNotEqual (
2065+ jsonpatch .RemoveOperation ({'op' : 'remove' , 'path' : jsonpointer .JsonPointer ('/a' )}),
2066+ jsonpatch .RemoveOperation ({'op' : 'remove' , 'path' : '/b' }))
2067+
2068+ def test_operation_setters_with_pointers (self ):
2069+ pointers = {'op' : 'move' , 'from' : jsonpointer .JsonPointer ('/a/b' ),
2070+ 'path' : jsonpointer .JsonPointer ('/c/d' )}
2071+ operation = jsonpatch .MoveOperation (pointers )
2072+ operation .key = 'e'
2073+ operation .from_key = 'f'
2074+ self .assertEqual (pointers ,
2075+ {'op' : 'move' , 'from' : '/a/f' , 'path' : '/c/e' })
2076+ self .assertEqual (operation .apply ({'a' : {'f' : 1 }, 'c' : {}}),
2077+ {'a' : {}, 'c' : {'e' : 1 }})
2078+
19902079
19912080class JsonPatchCreationTest (unittest .TestCase ):
19922081
0 commit comments