Skip to content

Commit 181b0ba

Browse files
authored
Merge pull request #257 from liugedragon/fix/readonly-mapping-tests
Compare read-only containers recursively in test operations
2 parents 7fd7a07 + 51b32f1 commit 181b0ba

2 files changed

Lines changed: 186 additions & 5 deletions

File tree

‎jsonpatch.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,13 +1577,13 @@ def _json_equal(first, second):
15771577
if isinstance(first, bool) or isinstance(second, bool):
15781578
return isinstance(first, bool) and isinstance(second, bool) and \
15791579
first == second
1580-
if isinstance(first, MutableMapping) and \
1581-
isinstance(second, MutableMapping):
1580+
if isinstance(first, Mapping) and isinstance(second, Mapping):
15821581
return len(first) == len(second) and all(
15831582
key in second and _json_equal(first[key], second[key])
15841583
for key in first)
1585-
if isinstance(first, MutableSequence) and \
1586-
isinstance(second, MutableSequence):
1584+
if isinstance(first, Sequence) and isinstance(second, Sequence) and \
1585+
not isinstance(first, (str, bytes)) and \
1586+
not isinstance(second, (str, bytes)):
15871587
return len(first) == len(second) and all(
15881588
_json_equal(*items) for items in zip(first, second))
15891589
return first == second

‎tests.py‎

Lines changed: 182 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import stat
2020
import subprocess
2121
import tempfile
22-
from collections.abc import Mapping
22+
from collections.abc import Mapping, Sequence
2323
from types import MappingProxyType
2424
from unittest import mock
2525

@@ -446,6 +446,187 @@ def test_test_literals_differ_from_numbers(self):
446446
jsonpatch.apply_patch(
447447
doc, [{'op': 'test', 'path': '/a', 'value': value}])
448448

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)
449630

450631
def test_test_not_existing(self):
451632
obj = {'bar': 'qux'}

0 commit comments

Comments
 (0)