-
Notifications
You must be signed in to change notification settings - Fork 49
/
jsonpatch_simple_test.go
120 lines (104 loc) · 3.63 KB
/
jsonpatch_simple_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
package jsonpatch
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
)
var simpleA = `{"a":100, "b":200, "c":"hello"}`
var simpleB = `{"a":100, "b":200, "c":"goodbye"}`
var simpleC = `{"a":100, "b":100, "c":"hello"}`
var simpleD = `{"a":100, "b":200, "c":"hello", "d":"foo"}`
var simpleE = `{"a":100, "b":200}`
var simplef = `{"a":100, "b":100, "d":"foo"}`
var simpleG = `{"a":100, "b":null, "d":"foo"}`
var empty = `{}`
func TestOneNullReplace(t *testing.T) {
patch, e := CreatePatch([]byte(simplef), []byte(simpleG))
assert.NoError(t, e)
assert.Equal(t, len(patch), 1, "they should be equal")
change := patch[0]
assert.Equal(t, change.Operation, "replace", "they should be equal")
assert.Equal(t, change.Path, "/b", "they should be equal")
assert.Equal(t, change.Value, nil, "they should be equal")
}
func TestSame(t *testing.T) {
patch, e := CreatePatch([]byte(simpleA), []byte(simpleA))
assert.NoError(t, e)
assert.Equal(t, len(patch), 0, "they should be equal")
}
func TestOneStringReplace(t *testing.T) {
patch, e := CreatePatch([]byte(simpleA), []byte(simpleB))
assert.NoError(t, e)
assert.Equal(t, len(patch), 1, "they should be equal")
change := patch[0]
assert.Equal(t, change.Operation, "replace", "they should be equal")
assert.Equal(t, change.Path, "/c", "they should be equal")
assert.Equal(t, change.Value, "goodbye", "they should be equal")
}
func TestOneIntReplace(t *testing.T) {
patch, e := CreatePatch([]byte(simpleA), []byte(simpleC))
assert.NoError(t, e)
assert.Equal(t, len(patch), 1, "they should be equal")
change := patch[0]
assert.Equal(t, change.Operation, "replace", "they should be equal")
assert.Equal(t, change.Path, "/b", "they should be equal")
var expected float64 = 100
assert.Equal(t, change.Value, expected, "they should be equal")
}
func TestOneAdd(t *testing.T) {
patch, e := CreatePatch([]byte(simpleA), []byte(simpleD))
assert.NoError(t, e)
assert.Equal(t, len(patch), 1, "they should be equal")
change := patch[0]
assert.Equal(t, change.Operation, "add", "they should be equal")
assert.Equal(t, change.Path, "/d", "they should be equal")
assert.Equal(t, change.Value, "foo", "they should be equal")
}
func TestOneRemove(t *testing.T) {
patch, e := CreatePatch([]byte(simpleA), []byte(simpleE))
assert.NoError(t, e)
assert.Equal(t, len(patch), 1, "they should be equal")
change := patch[0]
assert.Equal(t, change.Operation, "remove", "they should be equal")
assert.Equal(t, change.Path, "/c", "they should be equal")
assert.Equal(t, change.Value, nil, "they should be equal")
}
func TestVsEmpty(t *testing.T) {
patch, e := CreatePatch([]byte(simpleA), []byte(empty))
assert.NoError(t, e)
assert.Equal(t, len(patch), 3, "they should be equal")
sort.Sort(ByPath(patch))
change := patch[0]
assert.Equal(t, change.Operation, "remove", "they should be equal")
assert.Equal(t, change.Path, "/a", "they should be equal")
change = patch[1]
assert.Equal(t, change.Operation, "remove", "they should be equal")
assert.Equal(t, change.Path, "/b", "they should be equal")
change = patch[2]
assert.Equal(t, change.Operation, "remove", "they should be equal")
assert.Equal(t, change.Path, "/c", "they should be equal")
}
func BenchmarkBigArrays(b *testing.B) {
var a1, a2 []interface{}
a1 = make([]interface{}, 100)
a2 = make([]interface{}, 101)
for i := 0; i < 100; i++ {
a1[i] = i
a2[i+1] = i
}
for i := 0; i < b.N; i++ {
compareArray(a1, a2, "/")
}
}
func BenchmarkBigArrays2(b *testing.B) {
var a1, a2 []interface{}
a1 = make([]interface{}, 100)
a2 = make([]interface{}, 101)
for i := 0; i < 100; i++ {
a1[i] = i
a2[i] = i
}
for i := 0; i < b.N; i++ {
compareArray(a1, a2, "/")
}
}