Skip to content

Commit ba92a73

Browse files
authored
Merge pull request #221 from stefankoegl/claude/busy-fermat-uzxg2o
2 parents 5d28cee + 67c44eb commit ba92a73

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

‎jsonpatch.py‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ def __hash__(self):
195195
def __eq__(self, other):
196196
if not isinstance(other, PatchOperation):
197197
return False
198-
return self.operation == other.operation
198+
# e.g. tests for 1 and for true behave differently
199+
return _json_equal(self.operation, other.operation)
199200

200201
def __ne__(self, other):
201202
return not(self == other)
@@ -419,7 +420,7 @@ def apply(self, obj):
419420
raise InvalidJsonPatch(
420421
"The operation does not contain a 'value' member")
421422

422-
if val != value:
423+
if not _json_equal(val, value):
423424
msg = '{0} ({1}) is not equal to tested value {2} ({3})'
424425
raise JsonPatchTestFailed(msg.format(val, type(val),
425426
value, type(value)))
@@ -970,6 +971,25 @@ def _positional_cost(old, new):
970971
return 1
971972

972973

974+
def _json_equal(first, second):
975+
""" Compares values the way RFC 6902, 4.6 does: numbers by value, but
976+
literals like true only with themselves, which Python considers equal to
977+
1, and arrays and objects member by member """
978+
if isinstance(first, bool) or isinstance(second, bool):
979+
return isinstance(first, bool) and isinstance(second, bool) and \
980+
first == second
981+
if isinstance(first, MutableMapping) and \
982+
isinstance(second, MutableMapping):
983+
return len(first) == len(second) and all(
984+
key in second and _json_equal(first[key], second[key])
985+
for key in first)
986+
if isinstance(first, MutableSequence) and \
987+
isinstance(second, MutableSequence):
988+
return len(first) == len(second) and all(
989+
_json_equal(*items) for items in zip(first, second))
990+
return first == second
991+
992+
973993
def _sorted_members(value):
974994
""" Copies the arrays and objects in value, with the members of objects
975995
sorted by key """

‎property_tests.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,13 @@ def test_replace_any_existing_location(self, case, value):
424424
doc, [{'op': 'replace', 'path': to_pointer(parts), 'value': value}])
425425
assert_json_equal(resolve(result, parts), value)
426426

427-
# RFC 6902, 4.6: literals like true are only equal to themselves, but
428-
# 'test' uses Python equality, which considers 1 and True equal
429-
@unittest.expectedFailure
427+
# RFC 6902, 4.6: literals like true are only equal to themselves, although
428+
# Python considers 1 and True equal, #216
430429
@given(doc_pairs)
431430
@example(values=(1, True))
431+
@example(values=([1], [True]))
432+
@example(values=({'a': 0}, {'a': False}))
433+
@example(values=(1, 1.0))
432434
def test_test_operation_uses_json_equality(self, values):
433435
value, tested = values
434436
result = outcome(jsonpatch.apply_patch, {'a': value},

‎tests.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,23 @@ def test_test_error(self):
307307
jsonpatch.apply_patch,
308308
obj, [{'op': 'test', 'path': '/bar', 'value': 'bar'}])
309309

310+
def test_test_numbers_by_value(self):
311+
obj = {'a': 1, 'b': [0.0], 'c': {'d': 2}}
312+
jsonpatch.apply_patch(obj, [{'op': 'test', 'path': '/a', 'value': 1.0},
313+
{'op': 'test', 'path': '/b', 'value': [0]},
314+
{'op': 'test', 'path': '/c', 'value': {'d': 2.0}}])
315+
316+
def test_test_literals_differ_from_numbers(self):
317+
# RFC 6902, 4.6: true and false are only equal to themselves, #216
318+
for doc, value in [({'a': 1}, True),
319+
({'a': [1]}, [True]),
320+
({'a': {'a': 0}}, {'a': False}),
321+
({'a': True}, 1),
322+
({'a': False}, 0)]:
323+
with self.assertRaises(jsonpatch.JsonPatchTestFailed):
324+
jsonpatch.apply_patch(
325+
doc, [{'op': 'test', 'path': '/a', 'value': value}])
326+
310327

311328
def test_test_not_existing(self):
312329
obj = {'bar': 'qux'}
@@ -382,6 +399,17 @@ def test_patch_unequal(self):
382399
patch2 = jsonpatch.JsonPatch([{'op': 'test', 'path': '/test1'}])
383400
self.assertNotEqual(patch1, patch2)
384401

402+
def test_patch_unequal_literal_and_number(self):
403+
# the patches behave differently, #216
404+
patch1 = jsonpatch.JsonPatch([{'op': 'test', 'path': '/a', 'value': 1}])
405+
patch2 = jsonpatch.JsonPatch([{'op': 'test', 'path': '/a', 'value': True}])
406+
self.assertNotEqual(patch1, patch2)
407+
408+
def test_patch_equal_numbers_by_value(self):
409+
patch1 = jsonpatch.JsonPatch([{'op': 'test', 'path': '/a', 'value': [1]}])
410+
patch2 = jsonpatch.JsonPatch([{'op': 'test', 'path': '/a', 'value': [1.0]}])
411+
self.assertEqual(patch1, patch2)
412+
385413
def test_patch_hash_equality(self):
386414
patch1 = jsonpatch.JsonPatch([{ "op": "add", "path": "/a/b/c", "value": "foo" }])
387415
patch2 = jsonpatch.JsonPatch([{ "path": "/a/b/c", "op": "add", "value": "foo" }])

0 commit comments

Comments
 (0)