forked from mattbaird/jsonpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonpatch_set_test.go
More file actions
227 lines (204 loc) · 11.9 KB
/
Copy pathjsonpatch_set_test.go
File metadata and controls
227 lines (204 loc) · 11.9 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
package jsonpatch
import (
"testing"
"github.com/stretchr/testify/assert"
)
var simpleObjEmtpyPrmitiveSet = `{"a":100, "b":[]}`
var simpleObjPrimitiveSetWithOneItem = `{"a":100, "b":[1]}`
var simpleObjPrimitiveSetWithMultipleItems = `{"a":100, "b":[1,2]}`
var simpleObjAddSingleItemToPrimitiveSet = `{"b":[3]}`
var simpleObjAddMultipleItemsToPrimitiveSet = `{"b":[3,4]}`
var simpleObjAddDuplicateItemToPrimitiveSet = `{"b":[2]}`
var simpleObjSingletonObjectSet = `{"a":100, "b":[{"c":1}]}`
var simpleObjAddObjectSetItem = `{"b":[{"c":2}]}`
var simpleObjAddDuplicateObjectSetItem = `{"b":[{"c":1}]}`
var simpleObjAddObjectSetItemWithIgnoredValue = `{"b":[{"c":1, "d":"ignored"}]}`
var nestedObj = `{"a":100, "b":{"c":200}}`
var nestedObjModifyProp = `{"b":{"c":250}}`
var nestedObjAddProp = `{"b":{"d":"hello"}}`
var nestedObjPrimitiveSet = `{"a":100, "b":{"c":[200]}}`
var nestedObjAddPrimitiveSetItem = `{"b":{"c":[250]}}`
var setTestCollections = Collections{
EntitySets: EntitySets{},
Arrays: []Path{}, // No arrays in this test, only sets
}
var setTestIgnoredFields = []Path{"$.b[*].d"} // Ignored property for object sets
func TestCreatePatch_AddItemToEmptyPrimitiveSetInEnsureExistsMode_GeneratesAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjEmtpyPrmitiveSet), []byte(simpleObjAddSingleItemToPrimitiveSet), setTestCollections, setTestIgnoredFields, PatchStrategyEnsureExists)
assert.NoError(t, err)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/0", change.Path, "they should be equal")
var expected float64 = 3
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddItemToEmptyPrimitiveSetInEnsureExactMatchMode_GeneratesAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjEmtpyPrmitiveSet), []byte(simpleObjAddSingleItemToPrimitiveSet), setTestCollections, setTestIgnoredFields, PatchStrategyExactMatch)
assert.NoError(t, err)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/0", change.Path, "they should be equal")
var expected float64 = 3
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddItemToPrimitiveSetWithOneItemInEnsureExistsMode_GeneratesAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjPrimitiveSetWithOneItem), []byte(simpleObjAddSingleItemToPrimitiveSet), setTestCollections, setTestIgnoredFields, PatchStrategyEnsureExists)
assert.NoError(t, err)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/1", change.Path, "they should be equal")
var expected float64 = 3
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddItemToPrimitiveSetWithOneItemInExactMatchMode_GeneratesARemoveAndAnAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjPrimitiveSetWithOneItem), []byte(simpleObjAddSingleItemToPrimitiveSet), setTestCollections, setTestIgnoredFields, PatchStrategyExactMatch)
assert.NoError(t, err)
assert.Equal(t, 2, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/b/0", change.Path, "they should be equal")
change = patch[1]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/0", change.Path, "they should be equal")
var expected float64 = 3
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddItemToPrimitiveSetWithMultipleItems_InEnsureExistsMode_GeneratesAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjPrimitiveSetWithMultipleItems), []byte(simpleObjAddSingleItemToPrimitiveSet), setTestCollections, setTestIgnoredFields, PatchStrategyEnsureExists)
assert.NoError(t, err)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/2", change.Path, "they should be equal")
var expected float64 = 3
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddItemToPrimitiveSetWithMultipleItems_InExactMatchMode_GeneratesTwoRemovesAndOneAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjPrimitiveSetWithMultipleItems), []byte(simpleObjAddSingleItemToPrimitiveSet), setTestCollections, setTestIgnoredFields, PatchStrategyExactMatch)
assert.NoError(t, err)
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, "/b/1", change.Path, "they should be equal")
change = patch[1]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/b/0", change.Path, "they should be equal")
change = patch[2]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/0", change.Path, "they should be equal")
var expected float64 = 3
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddMultipleItemsToPrimitiveSetWithMultipleItems_InEnsureExistsMode_GeneratesTwoAddOperations(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjPrimitiveSetWithMultipleItems), []byte(simpleObjAddMultipleItemsToPrimitiveSet), setTestCollections, setTestIgnoredFields, PatchStrategyEnsureExists)
assert.NoError(t, err)
assert.Equal(t, 2, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/2", change.Path, "they should be equal")
var expected float64 = 3
assert.Equal(t, expected, change.Value, "they should be equal")
change = patch[1]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/3", change.Path, "they should be equal")
expected = 4
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddMultipleItemsToPrimitiveSetWithMultipleItems_InExactMatchMode_GeneratesTwoRemovesAndTwoAddOperations(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjPrimitiveSetWithMultipleItems), []byte(simpleObjAddMultipleItemsToPrimitiveSet), setTestCollections, setTestIgnoredFields, PatchStrategyExactMatch)
assert.NoError(t, err)
assert.Equal(t, 4, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/b/1", change.Path, "they should be equal")
change = patch[1]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/b/0", change.Path, "they should be equal")
change = patch[2]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/0", change.Path, "they should be equal")
var expected float64 = 3
assert.Equal(t, expected, change.Value, "they should be equal")
change = patch[3]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/1", change.Path, "they should be equal")
expected = 4
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddDuplicateItemToPrimitiveSetWithOneMultipleItems_InEnsureExistsMode_GeneratesNoOperations(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjPrimitiveSetWithMultipleItems), []byte(simpleObjAddDuplicateItemToPrimitiveSet), setTestCollections, setTestIgnoredFields, PatchStrategyEnsureExists)
assert.NoError(t, err)
assert.Equal(t, 0, len(patch), "they should be equal")
}
func TestCreatePatch_AddDuplicateItemToPrimitiveSetWithOneMultipleItems_InExactMatchMode_GeneratesARemoveOperation(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjPrimitiveSetWithMultipleItems), []byte(simpleObjAddDuplicateItemToPrimitiveSet), setTestCollections, setTestIgnoredFields, PatchStrategyExactMatch)
assert.NoError(t, err)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/b/0", change.Path, "they should be equal")
}
func TestCreatePatch_AddItemToPrimitiveSetInNestedObject_InEnsureExistsMode_GeneratesAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(nestedObjPrimitiveSet), []byte(nestedObjAddPrimitiveSetItem), setTestCollections, setTestIgnoredFields, PatchStrategyEnsureExists)
assert.NoError(t, err)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/c/1", change.Path, "they should be equal")
var expected float64 = 250
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddItemToPrimitiveSetInNestedObject_InExactMatchMode_GeneratesAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(nestedObjPrimitiveSet), []byte(nestedObjAddPrimitiveSetItem), setTestCollections, setTestIgnoredFields, PatchStrategyExactMatch)
assert.NoError(t, err)
assert.Equal(t, 2, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/b/c/0", change.Path, "they should be equal")
change = patch[1]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/c/0", change.Path, "they should be equal")
var expected float64 = 250
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddItemToObjectSetWithOneItem_InEnsureExistsMode_GeneratesAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjSingletonObjectSet), []byte(simpleObjAddObjectSetItem), setTestCollections, setTestIgnoredFields, PatchStrategyEnsureExists)
assert.NoError(t, err)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/1", change.Path, "they should be equal")
var expected = map[string]any{"c": float64(2)}
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddItemToObjectSetWithOneItem_InExactMatchMode_GeneratesAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjSingletonObjectSet), []byte(simpleObjAddObjectSetItem), setTestCollections, setTestIgnoredFields, PatchStrategyExactMatch)
assert.NoError(t, err)
assert.Equal(t, 2, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "remove", change.Operation, "they should be equal")
assert.Equal(t, "/b/0", change.Path, "they should be equal")
change = patch[1]
assert.Equal(t, "add", change.Operation, "they should be equal")
assert.Equal(t, "/b/0", change.Path, "they should be equal")
var expected = map[string]any{"c": float64(2)}
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddItemToObjectSetWithOneItemAndIgnoredValue_InEnsureExistsMode_GeneratesNoOperations(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjSingletonObjectSet), []byte(simpleObjAddObjectSetItemWithIgnoredValue), setTestCollections, setTestIgnoredFields, PatchStrategyEnsureExists)
assert.NoError(t, err)
assert.Equal(t, 0, len(patch), "they should be equal")
}
func TestCreatePatch_AddDuplicateItemToObjectSetWithOneItem_InEnsureExistsMode_GeneratesNoOperations(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjSingletonObjectSet), []byte(simpleObjAddDuplicateObjectSetItem), setTestCollections, setTestIgnoredFields, PatchStrategyEnsureExists)
assert.NoError(t, err)
assert.Equal(t, 0, len(patch), "they should be equal")
}
func TestCreatePatch_AddDuplicateItemToObjectSetWithOneItem_InExactMatchMode_GeneratesNoOperations(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObjSingletonObjectSet), []byte(simpleObjAddDuplicateObjectSetItem), setTestCollections, setTestIgnoredFields, PatchStrategyExactMatch)
assert.NoError(t, err)
assert.Equal(t, 0, len(patch), "they should be equal")
}