|
1 | 1 | import re
|
| 2 | +import sys |
2 | 3 | from collections import OrderedDict
|
3 | 4 | from collections.abc import Mapping
|
4 | 5 | from typing import Any
|
@@ -313,3 +314,29 @@ def test_dict_fail_fast(fail_fast, expected):
|
313 | 314 | v.validate_python({'a': 'b', 'c': 'd'})
|
314 | 315 |
|
315 | 316 | assert exc_info.value.errors(include_url=False) == expected
|
| 317 | + |
| 318 | + |
| 319 | +@pytest.mark.skipif( |
| 320 | + sys.implementation.name == 'graalpy', |
| 321 | + reason='GraalPy has a bug where PyMapping.items() does not preserve OrderedDict order. See: https://github.com/oracle/graalpython/issues/553', |
| 322 | +) |
| 323 | +@pytest.mark.parametrize('strict', [True, False]) |
| 324 | +def test_ordered_dict_key_order_preservation(strict): |
| 325 | + # GH 12273 |
| 326 | + v = SchemaValidator(cs.dict_schema(keys_schema=cs.str_schema(), values_schema=cs.int_schema())) |
| 327 | + |
| 328 | + # Original issue |
| 329 | + foo = OrderedDict({'a': 1, 'b': 2}) |
| 330 | + foo.move_to_end('a') |
| 331 | + |
| 332 | + result = v.validate_python(foo, strict=strict) |
| 333 | + assert list(result.keys()) == list(foo.keys()) == ['b', 'a'] |
| 334 | + assert result == {'b': 2, 'a': 1} |
| 335 | + |
| 336 | + # More complex case |
| 337 | + foo2 = OrderedDict({'x': 1, 'y': 2, 'z': 3}) |
| 338 | + foo2.move_to_end('x') |
| 339 | + |
| 340 | + result2 = v.validate_python(foo2, strict=strict) |
| 341 | + assert list(result2.keys()) == list(foo2.keys()) == ['y', 'z', 'x'] |
| 342 | + assert result2 == {'y': 2, 'z': 3, 'x': 1} |
0 commit comments