|
19 | 19 | import stat |
20 | 20 | import subprocess |
21 | 21 | import tempfile |
22 | | -from collections.abc import Mapping |
| 22 | +from collections.abc import Mapping, Sequence |
23 | 23 | from types import MappingProxyType |
24 | 24 | from unittest import mock |
25 | 25 |
|
@@ -446,6 +446,187 @@ def test_test_literals_differ_from_numbers(self): |
446 | 446 | jsonpatch.apply_patch( |
447 | 447 | doc, [{'op': 'test', 'path': '/a', 'value': value}]) |
448 | 448 |
|
| 449 | + def test_test_read_only_mapping_preserves_boolean_types(self): |
| 450 | + for mapping_type in (dict, MappingProxyType): |
| 451 | + for nested in (False, True): |
| 452 | + for actual, expected in ((True, 1), (False, 0), |
| 453 | + (1, True), (0, False)): |
| 454 | + with self.subTest(mapping_type=mapping_type, nested=nested, |
| 455 | + actual=actual, expected=expected): |
| 456 | + mapping = mapping_type({'a': actual}) |
| 457 | + doc = {'nested': mapping} if nested else mapping |
| 458 | + path = '/nested' if nested else '' |
| 459 | + original = json.dumps(doc, default=dict) |
| 460 | + patch = [{'op': 'test', 'path': path, |
| 461 | + 'value': {'a': expected}}] |
| 462 | + |
| 463 | + with self.assertRaises(jsonpatch.JsonPatchTestFailed): |
| 464 | + jsonpatch.apply_patch(doc, patch, in_place=True) |
| 465 | + |
| 466 | + self.assertEqual(json.dumps(doc, default=dict), original) |
| 467 | + if nested: |
| 468 | + self.assertIs(doc['nested'], mapping) |
| 469 | + |
| 470 | + def test_test_nested_read_only_mapping_preserves_boolean_types(self): |
| 471 | + documents = [ |
| 472 | + ({'nested': MappingProxyType({'a': True})}, |
| 473 | + {'nested': {'a': 1}}), |
| 474 | + ([MappingProxyType({'a': False})], [{'a': 0}]), |
| 475 | + (MappingProxyType({'nested': [True]}), {'nested': [1]}), |
| 476 | + ] |
| 477 | + for doc, value in documents: |
| 478 | + with self.subTest(doc=doc, value=value): |
| 479 | + original = json.dumps(doc, default=dict) |
| 480 | + with self.assertRaises(jsonpatch.JsonPatchTestFailed): |
| 481 | + jsonpatch.apply_patch( |
| 482 | + doc, [{'op': 'test', 'path': '', 'value': value}], |
| 483 | + in_place=True) |
| 484 | + self.assertEqual(json.dumps(doc, default=dict), original) |
| 485 | + |
| 486 | + def test_test_read_only_mapping_success_preserves_input(self): |
| 487 | + for mapping_type in (dict, MappingProxyType): |
| 488 | + for nested in (False, True): |
| 489 | + for actual, expected in ((1, 1.0), (1.0, 1), (True, True), |
| 490 | + (False, False), (None, None)): |
| 491 | + with self.subTest(mapping_type=mapping_type, nested=nested, |
| 492 | + actual=actual, expected=expected): |
| 493 | + mapping = mapping_type({'a': actual}) |
| 494 | + doc = {'nested': mapping} if nested else mapping |
| 495 | + path = '/nested' if nested else '' |
| 496 | + original = json.dumps(doc, default=dict) |
| 497 | + result = jsonpatch.apply_patch( |
| 498 | + doc, [{'op': 'test', 'path': path, |
| 499 | + 'value': {'a': expected}}], in_place=True) |
| 500 | + |
| 501 | + self.assertIs(result, doc) |
| 502 | + self.assertEqual(json.dumps(doc, default=dict), original) |
| 503 | + if nested: |
| 504 | + self.assertIs(result['nested'], mapping) |
| 505 | + |
| 506 | + |
| 507 | + def test_test_read_only_sequences_preserve_boolean_types(self): |
| 508 | + for actual_type in (list, tuple): |
| 509 | + for expected_type in (list, tuple): |
| 510 | + for nested in (False, True): |
| 511 | + for actual, expected in ((True, 1), (False, 0), |
| 512 | + (1, True), (0, False)): |
| 513 | + with self.subTest(actual_type=actual_type, |
| 514 | + expected_type=expected_type, |
| 515 | + nested=nested, actual=actual, |
| 516 | + expected=expected): |
| 517 | + sequence = actual_type([actual]) |
| 518 | + doc = {'nested': sequence} if nested else sequence |
| 519 | + path = '/nested' if nested else '' |
| 520 | + original = json.dumps(doc) |
| 521 | + patch = [{'op': 'test', 'path': path, |
| 522 | + 'value': expected_type([expected])}] |
| 523 | + |
| 524 | + with self.assertRaises(jsonpatch.JsonPatchTestFailed): |
| 525 | + jsonpatch.apply_patch(doc, patch, in_place=True) |
| 526 | + |
| 527 | + self.assertEqual(json.dumps(doc), original) |
| 528 | + if nested: |
| 529 | + self.assertIs(doc['nested'], sequence) |
| 530 | + |
| 531 | + def test_test_read_only_sequence_success_preserves_input(self): |
| 532 | + for actual_type in (list, tuple): |
| 533 | + for expected_type in (list, tuple): |
| 534 | + for nested in (False, True): |
| 535 | + for actual, expected in ((1, 1.0), (1.0, 1), (True, True), |
| 536 | + (False, False), (None, None)): |
| 537 | + with self.subTest(actual_type=actual_type, |
| 538 | + expected_type=expected_type, |
| 539 | + nested=nested, actual=actual, |
| 540 | + expected=expected): |
| 541 | + sequence = actual_type([actual]) |
| 542 | + doc = {'nested': sequence} if nested else sequence |
| 543 | + path = '/nested' if nested else '' |
| 544 | + original = json.dumps(doc) |
| 545 | + patch = [{'op': 'test', 'path': path, |
| 546 | + 'value': expected_type([expected])}] |
| 547 | + |
| 548 | + result = jsonpatch.apply_patch(doc, patch, in_place=True) |
| 549 | + |
| 550 | + self.assertIs(result, doc) |
| 551 | + self.assertEqual(json.dumps(doc), original) |
| 552 | + if nested: |
| 553 | + self.assertIs(doc['nested'], sequence) |
| 554 | + |
| 555 | + def test_test_custom_read_only_sequences(self): |
| 556 | + class FrozenSequence(Sequence): |
| 557 | + def __init__(self, values): |
| 558 | + self.values = tuple(values) |
| 559 | + |
| 560 | + def __getitem__(self, index): |
| 561 | + return self.values[index] |
| 562 | + |
| 563 | + def __len__(self): |
| 564 | + return len(self.values) |
| 565 | + |
| 566 | + def snapshot(value): |
| 567 | + return json.dumps(value, default=lambda value: |
| 568 | + dict(value) if isinstance(value, Mapping) |
| 569 | + else list(value)) |
| 570 | + |
| 571 | + cases = [ |
| 572 | + (FrozenSequence([True]), [1], False), |
| 573 | + ([True], FrozenSequence([1]), False), |
| 574 | + (FrozenSequence([1]), [1.0], True), |
| 575 | + ([True], FrozenSequence([True]), True), |
| 576 | + (FrozenSequence([]), (), True), |
| 577 | + (FrozenSequence([{'a': (True,)}]), [{'a': [True]}], True), |
| 578 | + (FrozenSequence([MappingProxyType({'a': (True,)})]), |
| 579 | + [{'a': [1]}], False), |
| 580 | + ([{'a': FrozenSequence([False])}], ({'a': (0,)},), False), |
| 581 | + ] |
| 582 | + for doc, value, succeeds in cases: |
| 583 | + with self.subTest(doc=doc, value=value): |
| 584 | + original, original_value = snapshot(doc), snapshot(value) |
| 585 | + patch = [{'op': 'test', 'path': '', 'value': value}] |
| 586 | + if succeeds: |
| 587 | + self.assertIs(jsonpatch.apply_patch( |
| 588 | + doc, patch, in_place=True), doc) |
| 589 | + else: |
| 590 | + with self.assertRaises(jsonpatch.JsonPatchTestFailed): |
| 591 | + jsonpatch.apply_patch(doc, patch, in_place=True) |
| 592 | + self.assertEqual(snapshot(doc), original) |
| 593 | + self.assertEqual(snapshot(value), original_value) |
| 594 | + |
| 595 | + def test_test_read_only_sequence_contents_and_lengths(self): |
| 596 | + for doc, value in [ |
| 597 | + ((True,), (1,)), |
| 598 | + (({'a': (False,)},), [{'a': [0]}]), |
| 599 | + ((), [1]), |
| 600 | + ((1,), [1, 2]), |
| 601 | + ((1, 2), [2, 1]), |
| 602 | + ]: |
| 603 | + with self.subTest(doc=doc, value=value): |
| 604 | + with self.assertRaises(jsonpatch.JsonPatchTestFailed): |
| 605 | + jsonpatch.apply_patch( |
| 606 | + doc, [{'op': 'test', 'path': '', 'value': value}], |
| 607 | + in_place=True) |
| 608 | + |
| 609 | + def test_test_strings_and_bytes_remain_scalar(self): |
| 610 | + for doc, value, succeeds in [ |
| 611 | + ('', '', True), |
| 612 | + ('text', 'text', True), |
| 613 | + (b'', b'', True), |
| 614 | + (b'bytes', b'bytes', True), |
| 615 | + ('text', 'next', False), |
| 616 | + (b'bytes', b'other', False), |
| 617 | + ('text', ('t', 'e', 'x', 't'), False), |
| 618 | + (('t', 'e', 'x', 't'), 'text', False), |
| 619 | + (b'12', (49, 50), False), |
| 620 | + ((49, 50), b'12', False), |
| 621 | + ]: |
| 622 | + with self.subTest(doc=doc, value=value): |
| 623 | + patch = [{'op': 'test', 'path': '', 'value': value}] |
| 624 | + if succeeds: |
| 625 | + self.assertIs(jsonpatch.apply_patch( |
| 626 | + doc, patch, in_place=True), doc) |
| 627 | + else: |
| 628 | + with self.assertRaises(jsonpatch.JsonPatchTestFailed): |
| 629 | + jsonpatch.apply_patch(doc, patch, in_place=True) |
449 | 630 |
|
450 | 631 | def test_test_not_existing(self): |
451 | 632 | obj = {'bar': 'qux'} |
|
0 commit comments