-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsondiff_test.go
409 lines (360 loc) · 8.98 KB
/
jsondiff_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
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
package jf
import (
"math"
"regexp"
"testing"
"github.com/stretchr/objx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func re(t *testing.T, s string) *regexp.Regexp {
t.Helper()
re, err := regexp.Compile(s)
require.NoError(t, err)
return re
}
// TestSimpleMap tests diff handling of primitive types (int/string) and slices
func TestSimpleMap(t *testing.T) {
const jsonA = `{
"number": 42,
"string": "hello",
"strings": ["hello", "world"],
"ints": [4, 2, 1],
"bool": true,
"float": 11.1
}`
const jsonB = `{
"number": 43,
"string": "hellp",
"strings": ["hello", "worle"],
"ints": [4, 2, 99],
"bool": false,
"float": 11.11
}`
assert := assert.New(t)
lines, err := Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 6)
assert.Equal(SingleDiff{"bool", "true", "false"}, lines[0])
assert.Equal(SingleDiff{"float", "11.1", "11.11"}, lines[1])
assert.Equal(SingleDiff{"ints[2]", "1", "99"}, lines[2])
assert.Equal(SingleDiff{"number", "42", "43"}, lines[3])
assert.Equal(SingleDiff{"string", `"hello"`, `"hellp"`}, lines[4])
assert.Equal(SingleDiff{"strings[1]", `"world"`, `"worle"`}, lines[5])
}
// TestDifferentKeys tests the case that in MSI there are different keys
func TestDifferentKeys(t *testing.T) {
const jsonA = `{
"numberA": 42
}`
const jsonB = `{
"numberB": 42
}`
assert := assert.New(t)
lines, err := Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 2)
assert.Equal(SingleDiff{"numberA", "42", ""}, lines[0])
assert.Equal(SingleDiff{"numberB", "", "42"}, lines[1])
}
// TestDifferentArrays sizes
func TestDifferentArrays(t *testing.T) {
const jsonA = `{
"smaller": [1, 2],
"bigger": [1],
"weird": [10, 20]
}`
const jsonB = `{
"smaller": [1],
"bigger": [1, 20, 30],
"weird": [30, 40]
}`
assert := assert.New(t)
lines, err := Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 5)
assert.Equal(SingleDiff{"bigger[1]", "", "20"}, lines[0])
assert.Equal(SingleDiff{"bigger[2]", "", "30"}, lines[1])
assert.Equal(SingleDiff{"smaller[1]", "2", ""}, lines[2])
assert.Equal(SingleDiff{"weird[0]", "10", "30"}, lines[3])
assert.Equal(SingleDiff{"weird[1]", "20", "40"}, lines[4])
}
func TestMapInMap(t *testing.T) {
const jsonA = `{
"key": {
"id": 11,
"name": "joe"
}
}`
const jsonB = `{
"key": {
"id": 11,
"name": "Joe"
}
}`
assert := assert.New(t)
lines, err := Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 1)
assert.Equal(SingleDiff{"key.name", `"joe"`, `"Joe"`}, lines[0])
}
func TestMapInMapInMap(t *testing.T) {
const jsonA = `{
"key": {
"subkey": {
"id": 11,
"name": "joe"
}
}
}`
const jsonB = `{
"key": {
"subkey": {
"id": 11,
"name": "Joe"
}
}
}`
assert := assert.New(t)
lines, err := Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 1)
assert.Equal(SingleDiff{"key.subkey.name", `"joe"`, `"Joe"`}, lines[0])
}
func TestMapSlice(t *testing.T) {
const jsonA = `{
"data": [
{"id": 1, "name": "one"},
{"id": 2, "name": "two"}
]
}
`
const jsonB = `{
"data": [
{"id": 1, "name": "One"},
{"id": 2, "name": "Two"}
]
}
`
assert := assert.New(t)
lines, err := Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 2)
assert.Equal(SingleDiff{"data[0].name", `"one"`, `"One"`}, lines[0])
assert.Equal(SingleDiff{"data[1].name", `"two"`, `"Two"`}, lines[1])
}
func TestNil(t *testing.T) {
const jsonA = `{
"key": null
}`
const jsonB = `{
"key": 42
}`
assert := assert.New(t)
lines, err := Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 1)
assert.Equal(SingleDiff{"key", "null", "42"}, lines[0])
}
// TestCoerceNull tests null coercion of jsonA only, jsonB only and both
func TestCoerceNull(t *testing.T) {
const jsonA = `{
"key": null
}`
const jsonB = `{
"key": 0
}`
assert := assert.New(t)
// 1. no coercion, return one line: see TestNil
// 2. coercion of A, return 0 lines
lines, err := NewDiffer().AddCoerceNull(RuleA, re(t, `.*`)).Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 0)
// 3. coercion of B, return 1 line
lines, err = NewDiffer().AddCoerceNull(RuleB, re(t, `.*`)).Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 1)
assert.Equal(SingleDiff{"key", "null", "0"}, lines[0])
// 3. coercion of A/B, return 0 lines
lines, err = NewDiffer().AddCoerceNull(RuleAB, re(t, `.*`)).Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 0)
}
// TestCoerceNullMatch tests that regexp based match works
func TestCoerceNullMatch(t *testing.T) {
const jsonA = `{
"key": {
"subkey1": null,
"subkey2": null
}
}`
const jsonB = `{
"key": {
"subkey1": 0,
"subkey2": 0
}
}`
assert := assert.New(t)
lines, err := NewDiffer().AddCoerceNull(RuleA, re(t, `key\.subkey1`)).Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 1)
assert.Equal(SingleDiff{"key.subkey2", "null", "0"}, lines[0])
}
func TestIgnore(t *testing.T) {
const jsonA = `{
"id": 11
}`
const jsonB = `{
"id": 11,
"additional": 42
}`
assert := assert.New(t)
lines, err := NewDiffer().AddIgnore(RuleA, re(t, `additional`)).Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 1)
assert.Equal(SingleDiff{"additional", "", "42"}, lines[0])
lines, err = NewDiffer().AddIgnore(RuleB, re(t, `additional`)).Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 0)
}
func TestIgnoreZero(t *testing.T) {
const jsonA = `{
"number": 0,
"string": "",
"strings": [],
"objects": {},
"bool": false
}`
const jsonB = `{
}`
assert := assert.New(t)
lines, err := NewDiffer().
AddIgnoreIfZero(RuleA, re(t, "number")).
AddIgnoreIfZero(RuleA, re(t, "string")).
AddIgnoreIfZero(RuleA, re(t, "strings")).
AddIgnoreIfZero(RuleA, re(t, "objects")).
AddIgnoreIfZero(RuleA, re(t, "bool")).
Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 0)
}
// test custom float function
func TestFloatEqual(t *testing.T) {
const jsonA = `{
"float": 1234.003,
"floats": [1234.003],
"floatm": {"first": 1234.003}
}
`
const jsonB = `{
"float": 1234.1,
"floats": [1234.1],
"floatm": {"first": 1234.1}
}
`
assert := assert.New(t)
lines, err := Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 3)
eq := func(a, b float64) bool {
abs := math.Abs(a - b)
ret := abs <= 0.5
return ret
}
lines, err = NewDiffer().AddFloatEqual(re(t, ".*"), eq).Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 0)
}
// test int to float coercion
func TestFloatIntEqual(t *testing.T) {
const jsonA = `{
"float": 1234.003,
"floats": [1234.003],
"floatm": {"first": 1234.003}
}
`
const jsonB = `{
"float": 1234,
"floats": [1234],
"floatm": {"first": 1234}
}
`
assert := assert.New(t)
lines, err := Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 3)
eq := func(a, b float64) bool {
abs := math.Abs(a - b)
ret := abs <= 1.0
return ret
}
lines, err = NewDiffer().AddFloatEqual(re(t, ".*"), eq).Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 0)
}
// test ignore order for slices
func TestIgnoreOrder(t *testing.T) {
const jsonA = `{
"list": [1, 2, 3]
}
`
const jsonB = `{
"list": [3, 2, 1]
}
`
assert := assert.New(t)
lines, err := NewDiffer().AddIgnoreOrder(re(t, "list")).Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 0)
}
// test number equal number
func TestStringNumber(t *testing.T) {
const jsonA = `{
"int": "1",
"float": "11.11",
"notnumber": "a"
}
`
const jsonB = `{
"int": 1,
"float": 11.11,
"notnumber": "a"
}
`
assert := assert.New(t)
lines, err := NewDiffer().AddStringNumber(re(t, ".*")).Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 0)
}
// test number equal number
func TestCustomEqual(t *testing.T) {
const jsonA = `{
"int": 1110
}
`
const jsonB = `{
"int": 1120
}
`
// return true only if both are numbers and difference is less than 10
// artifical example as I know the types of items
eq := func(selector string, a, b *objx.Value) bool {
if selector != "int" {
return false
}
if !a.IsInt() || !b.IsInt() {
return false
}
intA := a.MustInt()
intB := a.MustInt()
diff := intA - intB
if diff < 0 {
diff *= -1
}
return diff < 20
}
assert := assert.New(t)
lines, err := NewDiffer().AddCustomEqual(re(t, ".*"), eq).Diff(jsonA, jsonB)
assert.NoError(err)
assert.Len(lines, 0)
}