@@ -255,3 +255,34 @@ def test_json_dict_complex_key():
255
255
assert v .validate_json ('{"1+2j": 2, "infj": 4}' ) == {complex (1 , 2 ): 2 , complex (0 , float ('inf' )): 4 }
256
256
with pytest .raises (ValidationError , match = 'Input should be a valid complex string' ):
257
257
v .validate_json ('{"1+2j": 2, "": 4}' ) == {complex (1 , 2 ): 2 , complex (0 , float ('inf' )): 4 }
258
+
259
+
260
+ ...
261
+ def test_ordered_dict_key_order_preservation ():
262
+ # GH 12273
263
+ v = SchemaValidator (cs .dict_schema (keys_schema = cs .str_schema (), values_schema = cs .int_schema ()))
264
+
265
+ # Test case from original issue
266
+ foo = OrderedDict ({"a" : 1 , "b" : 2 })
267
+ foo .move_to_end ("a" )
268
+
269
+ result = v .validate_python (foo )
270
+ assert list (result .keys ()) == list (foo .keys ()) == ['b' , 'a' ]
271
+ assert result == {'b' : 2 , 'a' : 1 }
272
+
273
+ # Test with more complex reordering
274
+ foo2 = OrderedDict ({"x" : 1 , "y" : 2 , "z" : 3 })
275
+ foo2 .move_to_end ("x" )
276
+
277
+ result2 = v .validate_python (foo2 )
278
+ assert list (result2 .keys ()) == list (foo2 .keys ()) == ['y' , 'z' , 'x' ]
279
+ assert result2 == {'y' : 2 , 'z' : 3 , 'x' : 1 }
280
+
281
+ # Test popitem and re-insertion
282
+ foo3 = OrderedDict ({"p" : 1 , "q" : 2 })
283
+ item = foo3 .popitem (last = False )
284
+ foo3 [item [0 ]] = item [1 ]
285
+
286
+ result3 = v .validate_python (foo3 )
287
+ assert list (result3 .keys ()) == list (foo3 .keys ()) == ['q' , 'p' ]
288
+ assert result3 == {'q' : 2 , 'p' : 1 }
0 commit comments