Skip to content

Commit daa1b3c

Browse files
committed
test: improvements for idempotent operations
1 parent 6d0c1fe commit daa1b3c

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

test_json_patch.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,27 @@ def test_op_remove_baz_list_member():
101101
assert obj['bar'] != 'potatoes'
102102

103103

104-
def test_op_remove_fail_on_nonexistent_member():
105-
"""Should raise an exception if removing a non-existent object member."""
104+
def test_op_remove_fail_on_nonexistent_path():
105+
"""Should raise an exception if referencing a non-existent tree to remove."""
106106
patches = [
107-
{"op": "remove", "path": "/0/foo/four"}
107+
{"op": "remove", "path": "/0/qux/one"}
108108
]
109109
jp = JSONPatcher(sample_json, *patches)
110110
with pytest.raises(PathError):
111111
jp.patch()
112112

113113

114+
def test_op_remove_unchanged_on_nonexistent_member():
115+
"""Should not raise an exception if referencing a non-existent leaf to remove."""
116+
patches = [
117+
{"op": "remove", "path": "/0/foo/four"}
118+
]
119+
jp = JSONPatcher(sample_json, *patches)
120+
changed, tested = jp.patch()
121+
assert changed is False
122+
assert tested is None
123+
124+
114125
# OPERATION: REPLACE
115126
def test_op_replace_foo_three():
116127
"""Should replace the value for the 'three' member in 'foo'."""
@@ -124,8 +135,8 @@ def test_op_replace_foo_three():
124135
assert jp.obj[0]['foo']['three'] == 'booyah'
125136

126137

127-
def test_op_replace_fail_on_nonexistent_member():
128-
"""Should raise an exception if replacing a non-existent object member."""
138+
def test_op_replace_fail_on_nonexistent_path_or_member():
139+
"""Should raise an exception if any part of the referenced path does not exist (RFC 6902)."""
129140
patches = [
130141
{"op": "replace", "path": "/0/foo/four", "value": 4}
131142
]
@@ -161,14 +172,15 @@ def test_op_move_baz_list_foo():
161172
assert len(jp.obj[0]['foo']['fruits']) == 3
162173

163174

164-
def test_op_move_fail_on_nonexistent():
165-
"""Should raise an exception if moving a non-existent object member."""
175+
def test_op_move_unchanged_on_nonexistent():
176+
"""Should not raise an exception if moving a non-existent object member."""
166177
patches = [
167178
{"op": "move", "from": "/0/foo/four", "path": "/1/bar/four"}
168179
]
169180
jp = JSONPatcher(sample_json, *patches)
170-
with pytest.raises(PathError):
171-
jp.patch()
181+
changed, tested = jp.patch()
182+
assert changed is False
183+
assert tested is None
172184

173185

174186
def test_op_move_foo_object_end_of_list():

0 commit comments

Comments
 (0)