-
Notifications
You must be signed in to change notification settings - Fork 49
/
jsonpatch_array_test.go
92 lines (76 loc) · 2.82 KB
/
jsonpatch_array_test.go
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
package jsonpatch
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
var (
arrayBase = `{
"persons": [{"name":"Ed"},{}]
}`
arrayUpdated = `{
"persons": [{"name":"Ed"},{},{}]
}`
)
func TestArrayAddMultipleEmptyObjects(t *testing.T) {
patch, e := CreatePatch([]byte(arrayBase), []byte(arrayUpdated))
assert.NoError(t, e)
t.Log("Patch:", patch)
assert.Equal(t, 1, len(patch), "they should be equal")
sort.Sort(ByPath(patch))
change := patch[0]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/persons/2", change.Path, "they should be equal")
assert.Equal(t, map[string]interface{}{}, change.Value, "they should be equal")
}
func TestArrayRemoveMultipleEmptyObjects(t *testing.T) {
patch, e := CreatePatch([]byte(arrayUpdated), []byte(arrayBase))
assert.NoError(t, e)
t.Log("Patch:", patch)
assert.Equal(t, 1, len(patch), "they should be equal")
sort.Sort(ByPath(patch))
change := patch[0]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/persons/2", change.Path, "they should be equal")
assert.Equal(t, nil, change.Value, "they should be equal")
}
var (
arrayWithSpacesBase = `{
"persons": [{"name":"Ed"},{},{},{"name":"Sally"},{}]
}`
arrayWithSpacesUpdated = `{
"persons": [{"name":"Ed"},{},{"name":"Sally"},{}]
}`
)
// TestArrayRemoveSpaceInbetween tests removing one blank item from a group blanks which is in between non blank items which also end with a blank item. This tests that the correct index is removed
func TestArrayRemoveSpaceInbetween(t *testing.T) {
t.Skip("This test fails. TODO change compareArray algorithm to match by index instead of by object equality")
patch, e := CreatePatch([]byte(arrayWithSpacesBase), []byte(arrayWithSpacesUpdated))
assert.NoError(t, e)
t.Log("Patch:", patch)
assert.Equal(t, 1, len(patch), "they should be equal")
sort.Sort(ByPath(patch))
change := patch[0]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/persons/2", change.Path, "they should be equal")
assert.Equal(t, nil, change.Value, "they should be equal")
}
var (
arrayRemoveMultiBase = `{
"persons": [{"name":"Ed"},{"name":"Ee"},{"name":"Ef"},{"name":"Sally"},{}]
}`
arrayRemoveMultisUpdated = `{
"persons": [{"name":"Ef"},{},{"name":"Sally"},{}]
}`
)
// TestArrayRemoveMulti tests removing multi groups. This tests that the correct index is removed
func TestArrayRemoveMulti(t *testing.T) {
patch, e := CreatePatch([]byte(arrayRemoveMultiBase), []byte(arrayRemoveMultisUpdated))
assert.NoError(t, e)
t.Log("Patch:", patch)
assert.Equal(t, 3, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/persons/1", change.Path, "they should be equal")
assert.Equal(t, nil, change.Value, "they should be equal")
}