forked from particledecay/ansible-jsonpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_json_patch.py
338 lines (280 loc) · 10.1 KB
/
test_json_patch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from __future__ import (absolute_import, division, print_function)
import json
import pytest
from ansible.modules.files.json_patch import JSONPatcher, PathError
__metaclass__ = type
sample_json = json.dumps([
{"foo": {"one": 1, "two": 2, "three": 3}, "enabled": True},
{"bar": {"one": 1, "two": 2, "three": 3}, "enabled": False},
{"baz": [{"foo": "apples", "bar": "oranges"},
{"foo": "grapes", "bar": "oranges"},
{"foo": "bananas", "bar": "potatoes"}],
"enabled": False}])
# OPERATION: ADD
def test_op_add_foo_four():
"""Should add a `four` member to the first object."""
patches = [
{"op": "add", "path": "/0/foo/four", "value": 4}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
assert jp.obj[0]['foo']['four'] == 4
def test_op_add_object_list():
"""Should add a new first object to the 'baz' list."""
patches = [
{"op": "add", "path": "/2/baz/0", "value": {"foo": "kiwis", "bar": "strawberries"}}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
assert jp.obj[2]['baz'][0] == patches[0]['value']
def test_op_add_object_end_of_list():
"""should add a new last object to the 'baz' list."""
patches = [
{"op": "add", "path": "/2/baz/-", "value": {"foo": "raspberries", "bar": "blueberries"}}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
assert jp.obj[2]['baz'][-1] == patches[0]['value']
def test_op_add_replace_existing_value():
"""Should find an existing property and replace its value."""
patches = [
{"op": "add", "path": "/1/bar/three", "value": 10}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
assert jp.obj[1]['bar']['three'] == 10
def test_op_add_ignore_existing_value():
"""Should ignore an existing property with the same value."""
patches = [
{"op": "add", "path": "/1/bar/one", "value": 1}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is False
assert tested is None
assert jp.obj[1]['bar']['one'] == 1
# OPERATION: REMOVE
def test_op_remove_foo_three():
"""Should remove the 'three' member from the first object."""
patches = [
{"op": "remove", "path": "/0/foo/three"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
assert 'three' not in jp.obj[0]['foo']
def test_op_remove_baz_list_member():
"""Should remove the last fruit item from the 'baz' list."""
patches = [
{"op": "remove", "path": "/2/baz/2"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
for obj in jp.obj[2]['baz']:
assert obj['foo'] != 'bananas'
assert obj['bar'] != 'potatoes'
def test_op_remove_fail_on_nonexistent_path():
"""Should raise an exception if referencing a non-existent tree to remove."""
patches = [
{"op": "remove", "path": "/0/qux/one"}
]
jp = JSONPatcher(sample_json, *patches)
with pytest.raises(PathError):
jp.patch()
def test_op_remove_unchanged_on_nonexistent_member():
"""Should not raise an exception if referencing a non-existent leaf to remove."""
patches = [
{"op": "remove", "path": "/0/foo/four"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is False
assert tested is None
# OPERATION: REPLACE
def test_op_replace_foo_three():
"""Should replace the value for the 'three' member in 'foo'."""
patches = [
{"op": "replace", "path": "/0/foo/three", "value": "booyah"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
assert jp.obj[0]['foo']['three'] == 'booyah'
def test_op_replace_fail_on_nonexistent_path_or_member():
"""Should raise an exception if any part of the referenced path does not exist (RFC 6902)."""
patches = [
{"op": "replace", "path": "/0/foo/four", "value": 4}
]
jp = JSONPatcher(sample_json, *patches)
with pytest.raises(PathError):
jp.patch()
# OPERATION: MOVE
def test_op_move_foo_three_bar_four():
"""Should move the 'three' property from 'foo' to 'bar'."""
patches = [
{"op": "move", "from": "/0/foo/three", "path": "/1/bar/four"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
assert jp.obj[0]['foo'].get('three', 'DUMMY VALUE') == 'DUMMY VALUE'
assert jp.obj[1]['bar']['four'] == 3
def test_op_move_baz_list_foo():
"""Should move the 'baz' list of fruits to 'foo' object."""
patches = [
{"op": "move", "from": "/2/baz", "path": "/0/foo/fruits"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
assert jp.obj[2].get('baz', 'DUMMY VALUE') == 'DUMMY VALUE'
assert len(jp.obj[0]['foo']['fruits']) == 3
def test_op_move_unchanged_on_nonexistent():
"""Should not raise an exception if moving a non-existent object member."""
patches = [
{"op": "move", "from": "/0/foo/four", "path": "/1/bar/four"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is False
assert tested is None
def test_op_move_foo_object_end_of_list():
"""Should move the 'three' member in 'foo' to the end of the 'baz' list."""
patches = [
{"op": "move", "from": "/0/foo/three", "path": "/2/baz/-"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
assert jp.obj[0]['foo'].get('three', 'DUMMY VALUE') == 'DUMMY VALUE'
assert jp.obj[2]['baz'][-1] == 3
# OPERATION: COPY
def test_op_copy_foo_three_bar_four():
"""Should copy the 'three' member in 'foo' to the 'bar' object."""
patches = [
{"op": "copy", "from": "/0/foo/three", "path": "/1/bar/four"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
assert jp.obj[0]['foo']['three'] == 3
assert jp.obj[1]['bar']['four'] == 3
def test_op_copy_baz_list_bar():
"""Should copy the 'baz' list of fruits to 'foo' object."""
patches = [
{"op": "copy", "from": "/2/baz", "path": "/0/foo/fruits"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is None
assert len(jp.obj[2]['baz']) == 3
assert len(jp.obj[0]['foo']['fruits']) == 3
def test_op_copy_fail_on_nonexistent_member():
"""Should raise an exception when copying a non-existent member."""
patches = [
{"op": "copy", "from": "/1/bar/four", "path": "/0/foo/fruits"}
]
jp = JSONPatcher(sample_json, *patches)
with pytest.raises(PathError):
jp.patch()
# OPERATION: TEST
def test_op_test_string_equal():
"""Should return True that two strings are equal."""
patches = [
{"op": "test", "path": "/2/baz/0/foo", "value": "apples"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is None
assert tested is True
def test_op_test_string_unequal():
"""Should return False that two strings are unequal."""
patches = [
{"op": "test", "path": "/2/baz/0/foo", "value": "bananas"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is None
assert tested is False
def test_op_test_number_equal():
"""Should return True that two numbers are equal."""
patches = [
{"op": "test", "path": "/0/foo/one", "value": 1}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is None
assert tested is True
def test_op_test_number_unequal():
"""Should return False that two numbers are unequal."""
patches = [
{"op": "test", "path": "/0/foo/one", "value": "bananas"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is None
assert tested is False
def test_op_test_list_equal():
"""Should return True that two lists are equal."""
patches = [
{"op": "add", "path": "/0/foo/compare", "value": [1, 2, 3]},
{"op": "test", "path": "/0/foo/compare", "value": [1, 2, 3]}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is True
assert tested is True
def test_op_test_wildcard():
"""Should find an element in the 'baz' list with the matching value."""
patches = [
{"op": "test", "path": "/2/baz/*/foo", "value": "grapes"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is None
assert tested is True
def test_op_test_wildcard_not_found():
"""Should return False on not finding an element with the given value."""
patches = [
{"op": "test", "path": "/2/baz/*/bar", "value": "rocks"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is None
assert tested is False
def test_op_test_multiple_tests():
"""Should return False if at least one test returns False."""
patches = [
{"op": "test", "path": "/0/foo/one", "value": 2},
{"op": "test", "path": "/1/bar/one", "value": 1}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is None
assert tested is False
def test_op_test_nonexistent_member():
"""Should return False even if path does not exist."""
patches = [
{"op": "test", "path": "/10/20/foo", "value": "bar"}
]
jp = JSONPatcher(sample_json, *patches)
changed, tested = jp.patch()
assert changed is None
assert tested is False