-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_test.go
More file actions
555 lines (465 loc) · 15.1 KB
/
apply_test.go
File metadata and controls
555 lines (465 loc) · 15.1 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package jsonpatch
import (
"encoding/json"
"strings"
"testing"
)
// RFC 6902 Appendix A examples
func TestApply_A1_AddingObjectMember(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "add", "path": "/baz", "value": "qux"}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"baz": "qux", "foo": "bar"}`, string(result))
}
func TestApply_A2_AddingArrayElement(t *testing.T) {
doc := []byte(`{"foo": ["bar", "baz"]}`)
patch := []byte(`[{"op": "add", "path": "/foo/1", "value": "qux"}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": ["bar", "qux", "baz"]}`, string(result))
}
func TestApply_A3_RemovingObjectMember(t *testing.T) {
doc := []byte(`{"baz": "qux", "foo": "bar"}`)
patch := []byte(`[{"op": "remove", "path": "/baz"}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": "bar"}`, string(result))
}
func TestApply_A4_RemovingArrayElement(t *testing.T) {
doc := []byte(`{"foo": ["bar", "qux", "baz"]}`)
patch := []byte(`[{"op": "remove", "path": "/foo/1"}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": ["bar", "baz"]}`, string(result))
}
func TestApply_A5_ReplacingValue(t *testing.T) {
doc := []byte(`{"baz": "qux", "foo": "bar"}`)
patch := []byte(`[{"op": "replace", "path": "/baz", "value": "boo"}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"baz": "boo", "foo": "bar"}`, string(result))
}
func TestApply_A6_MovingValue(t *testing.T) {
doc := []byte(`{
"foo": {"bar": "baz", "waldo": "fred"},
"qux": {"corge": "grault"}
}`)
patch := []byte(`[{"op": "move", "from": "/foo/waldo", "path": "/qux/thud"}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{
"foo": {"bar": "baz"},
"qux": {"corge": "grault", "thud": "fred"}
}`, string(result))
}
func TestApply_A7_MovingArrayElement(t *testing.T) {
doc := []byte(`{"foo": ["all", "grass", "cows", "eat"]}`)
patch := []byte(`[{"op": "move", "from": "/foo/1", "path": "/foo/3"}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": ["all", "cows", "eat", "grass"]}`, string(result))
}
func TestApply_A8_TestingValueSuccess(t *testing.T) {
doc := []byte(`{"baz": "qux", "foo": ["a", 2, "c"]}`)
patch := []byte(`[
{"op": "test", "path": "/baz", "value": "qux"},
{"op": "test", "path": "/foo/1", "value": 2}
]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"baz": "qux", "foo": ["a", 2, "c"]}`, string(result))
}
func TestApply_A9_TestingValueError(t *testing.T) {
doc := []byte(`{"baz": "qux"}`)
patch := []byte(`[{"op": "test", "path": "/baz", "value": "bar"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected test operation to fail")
}
}
func TestApply_A10_AddingNestedMemberObject(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "add", "path": "/child", "value": {"grandchild": {}}}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": "bar", "child": {"grandchild": {}}}`, string(result))
}
func TestApply_A11_IgnoringUnrecognizedElements(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "add", "path": "/baz", "value": "qux", "xyz": 123}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": "bar", "baz": "qux"}`, string(result))
}
func TestApply_A12_AddingToNonexistentTarget(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "add", "path": "/baz/bat", "value": "qux"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error when adding to nonexistent target")
}
}
func TestApply_A14_TildeEscapeOrdering(t *testing.T) {
doc := []byte(`{"/": 9, "~1": 10}`)
patch := []byte(`[{"op": "test", "path": "/~01", "value": 10}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"/": 9, "~1": 10}`, string(result))
}
func TestApply_A15_ComparingStringsAndNumbers(t *testing.T) {
doc := []byte(`{"/": 9, "~1": 10}`)
patch := []byte(`[{"op": "test", "path": "/~01", "value": "10"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error: string '10' should not equal number 10")
}
}
func TestApply_A16_AddingArrayValue(t *testing.T) {
doc := []byte(`{"foo": ["bar"]}`)
patch := []byte(`[{"op": "add", "path": "/foo/-", "value": ["abc", "def"]}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": ["bar", ["abc", "def"]]}`, string(result))
}
// Additional edge case tests
func TestApply_CopyOperation(t *testing.T) {
doc := []byte(`{"foo": {"bar": "baz"}, "qux": {}}`)
patch := []byte(`[{"op": "copy", "from": "/foo/bar", "path": "/qux/bar"}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": {"bar": "baz"}, "qux": {"bar": "baz"}}`, string(result))
}
func TestApply_ReplaceRoot(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "replace", "path": "", "value": {"baz": "qux"}}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"baz": "qux"}`, string(result))
}
func TestApply_AddRoot(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "add", "path": "", "value": {"baz": "qux"}}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"baz": "qux"}`, string(result))
}
func TestApply_ReplaceNonexistent(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "replace", "path": "/baz", "value": "qux"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error when replacing nonexistent path")
}
}
func TestApply_RemoveNonexistent(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "remove", "path": "/baz"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error when removing nonexistent path")
}
}
func TestApply_MoveCannotMoveIntoChild(t *testing.T) {
doc := []byte(`{"foo": {"bar": "baz"}}`)
patch := []byte(`[{"op": "move", "from": "/foo", "path": "/foo/bar/baz"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error: cannot move a value into one of its children")
}
}
func TestApply_MultipleOperationsSequential(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[
{"op": "add", "path": "/baz", "value": "qux"},
{"op": "replace", "path": "/foo", "value": "updated"},
{"op": "add", "path": "/arr", "value": [1, 2, 3]},
{"op": "remove", "path": "/arr/1"},
{"op": "test", "path": "/arr", "value": [1, 3]}
]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": "updated", "baz": "qux", "arr": [1, 3]}`, string(result))
}
func TestApply_AtomicFailure(t *testing.T) {
// Per RFC 6902 Section 5: if any operation fails, the entire patch fails
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[
{"op": "add", "path": "/baz", "value": "qux"},
{"op": "test", "path": "/baz", "value": "wrong"}
]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected test failure to cause entire patch to fail")
}
}
func TestApply_AddReplaceExistingMember(t *testing.T) {
// Per RFC 6902 Section 4.1: if the target location specifies an object
// member that does exist, that member's value is replaced.
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "add", "path": "/foo", "value": "baz"}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": "baz"}`, string(result))
}
func TestApply_InvalidOperation(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "invalid", "path": "/foo"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error for invalid operation")
}
}
func TestApply_InvalidPatchJSON(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`not json`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error for invalid patch JSON")
}
}
func TestApply_InvalidDocJSON(t *testing.T) {
doc := []byte(`not json`)
patch := []byte(`[{"op": "add", "path": "/foo", "value": "bar"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error for invalid document JSON")
}
}
func TestApply_AddMissingValue(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "add", "path": "/baz"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error: add operation requires value")
}
}
func TestApply_MoveMissingFrom(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "move", "path": "/baz"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error: move operation requires from")
}
}
func TestApply_ReplaceArrayElement(t *testing.T) {
doc := []byte(`{"foo": [1, 2, 3]}`)
patch := []byte(`[{"op": "replace", "path": "/foo/1", "value": 99}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": [1, 99, 3]}`, string(result))
}
func TestApply_AddToEndOfArray(t *testing.T) {
doc := []byte(`{"foo": [1, 2]}`)
patch := []byte(`[{"op": "add", "path": "/foo/-", "value": 3}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": [1, 2, 3]}`, string(result))
}
func TestApply_CopyArray(t *testing.T) {
doc := []byte(`{"foo": [1, 2, 3]}`)
patch := []byte(`[{"op": "copy", "from": "/foo", "path": "/bar"}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": [1, 2, 3], "bar": [1, 2, 3]}`, string(result))
}
func TestApply_TestWithNull(t *testing.T) {
doc := []byte(`{"foo": null}`)
patch := []byte(`[{"op": "test", "path": "/foo", "value": null}]`)
_, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
}
func TestApply_TestWithBoolean(t *testing.T) {
doc := []byte(`{"foo": true, "bar": false}`)
patch := []byte(`[
{"op": "test", "path": "/foo", "value": true},
{"op": "test", "path": "/bar", "value": false}
]`)
_, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
}
func TestApply_TestWithNestedObject(t *testing.T) {
doc := []byte(`{"foo": {"bar": [1, 2, 3]}}`)
patch := []byte(`[{"op": "test", "path": "/foo", "value": {"bar": [1, 2, 3]}}]`)
_, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
}
// --- Targeted compliance tests ---
func TestApply_MissingPathIsRejected(t *testing.T) {
// RFC 6902: every operation object MUST have exactly one "path" member.
tests := []struct {
name string
patch string
}{
{"add without path", `[{"op": "add", "value": "x"}]`},
{"remove without path", `[{"op": "remove"}]`},
{"replace without path", `[{"op": "replace", "value": "x"}]`},
{"move without path", `[{"op": "move", "from": "/foo"}]`},
{"copy without path", `[{"op": "copy", "from": "/foo"}]`},
{"test without path", `[{"op": "test", "value": "bar"}]`},
}
doc := []byte(`{"foo": "bar"}`)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := Apply(doc, []byte(tt.patch))
if err == nil {
t.Fatalf("expected error for patch without path: %s", tt.patch)
}
})
}
}
func TestApply_MoveFromRootPointer(t *testing.T) {
// from:"" is the root pointer and must be accepted by validation.
// Moving root to a child is blocked by the prefix check (correctly),
// but the operation must not be rejected for a missing "from".
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "move", "from": "", "path": "/new"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error: root is a prefix of /new")
}
// The error must be about prefix, NOT about missing "from" member.
if got := err.Error(); !strings.Contains(got, "prefix") {
t.Fatalf("expected prefix error, got: %v", err)
}
}
func TestApply_CopyFromRootPointer(t *testing.T) {
// from:"" is the root pointer and must be accepted for copy.
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"op": "copy", "from": "", "path": "/dup"}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatalf("copy with from='' (root) should succeed: %v", err)
}
assertJSONEqual(t, `{"foo": "bar", "dup": {"foo": "bar"}}`, string(result))
}
func TestApply_DashTokenOnlyValidForAdd(t *testing.T) {
// The "-" token references the nonexistent element after the last array element.
// It is only valid as the final token for an "add" target path.
doc := []byte(`{"foo": [1, 2, 3]}`)
tests := []struct {
name string
patch string
}{
{"test on /-", `[{"op": "test", "path": "/foo/-", "value": 3}]`},
{"replace on /-", `[{"op": "replace", "path": "/foo/-", "value": 99}]`},
{"remove on /-", `[{"op": "remove", "path": "/foo/-"}]`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := Apply(doc, []byte(tt.patch))
if err == nil {
t.Fatalf("expected error for %s: %s", tt.name, tt.patch)
}
})
}
}
func TestApply_AddWithDashStillWorks(t *testing.T) {
doc := []byte(`{"foo": [1, 2]}`)
patch := []byte(`[{"op": "add", "path": "/foo/-", "value": 3}]`)
result, err := Apply(doc, patch)
if err != nil {
t.Fatal(err)
}
assertJSONEqual(t, `{"foo": [1, 2, 3]}`, string(result))
}
func TestApply_MissingOpIsRejected(t *testing.T) {
// RFC 6902 Section 4: operation objects MUST have exactly one "op" member.
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`[{"path": "/foo", "value": "baz"}]`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error for operation without op member")
}
}
func TestApply_NonStringOpRejected(t *testing.T) {
doc := []byte(`{"foo": "bar"}`)
tests := []struct {
name string
patch string
}{
{"op as number", `[{"op": 123, "path": "/foo", "value": "x"}]`},
{"op as boolean", `[{"op": true, "path": "/foo", "value": "x"}]`},
{"op as null", `[{"op": null, "path": "/foo", "value": "x"}]`},
{"op as array", `[{"op": [], "path": "/foo", "value": "x"}]`},
{"op as object", `[{"op": {}, "path": "/foo", "value": "x"}]`},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := Apply(doc, []byte(tt.patch))
if err == nil {
t.Fatalf("expected error for non-string op: %s", tt.patch)
}
})
}
}
func TestApply_A13_InvalidPatchDocument(t *testing.T) {
// RFC 6902 Appendix A.13: A JSON Patch document that is not an array
// is an invalid patch document.
doc := []byte(`{"foo": "bar"}`)
patch := []byte(`{"op": "add", "path": "/baz", "value": "qux"}`)
_, err := Apply(doc, patch)
if err == nil {
t.Fatal("expected error for patch document that is not an array")
}
}
// assertJSONEqual compares two JSON strings for semantic equality.
func assertJSONEqual(t *testing.T, expected, actual string) {
t.Helper()
var e, a interface{}
if err := json.Unmarshal([]byte(expected), &e); err != nil {
t.Fatalf("invalid expected JSON: %v", err)
}
if err := json.Unmarshal([]byte(actual), &a); err != nil {
t.Fatalf("invalid actual JSON: %v\nraw: %s", err, actual)
}
if !jsonEqual(e, a) {
t.Errorf("JSON not equal:\n expected: %s\n actual: %s", expected, actual)
}
}