-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
jsonmerge_test.go
80 lines (65 loc) · 2.05 KB
/
jsonmerge_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
package runtime
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestJSONMerge(t *testing.T) {
t.Run("when object", func(t *testing.T) {
t.Run("Merges properties defined in both objects", func(t *testing.T) {
data := `{"foo": 1}`
patch := `{"foo": null}`
expected := `{"foo":null}`
actual, err := JSONMerge([]byte(data), []byte(patch))
assert.NoError(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("Sets property defined in only src object", func(t *testing.T) {
data := `{}`
patch := `{"source":"merge-me"}`
expected := `{"source":"merge-me"}`
actual, err := JSONMerge([]byte(data), []byte(patch))
assert.NoError(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("Handles child objects", func(t *testing.T) {
data := `{"channel":{"status":"valid"}}`
patch := `{"channel":{"id":1}}`
expected := `{"channel":{"id":1,"status":"valid"}}`
actual, err := JSONMerge([]byte(data), []byte(patch))
assert.NoError(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("Handles empty objects", func(t *testing.T) {
data := `{}`
patch := `{}`
expected := `{}`
actual, err := JSONMerge([]byte(data), []byte(patch))
assert.NoError(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("Handles nil data", func(t *testing.T) {
patch := `{"foo":"bar"}`
expected := `{"foo":"bar"}`
actual, err := JSONMerge(nil, []byte(patch))
assert.NoError(t, err)
assert.Equal(t, expected, string(actual))
})
t.Run("Handles nil patch", func(t *testing.T) {
data := `{"foo":"bar"}`
expected := `{"foo":"bar"}`
actual, err := JSONMerge([]byte(data), nil)
assert.NoError(t, err)
assert.Equal(t, expected, string(actual))
})
})
t.Run("when array", func(t *testing.T) {
t.Run("it does not merge", func(t *testing.T) {
data := `[{"foo": 1}]`
patch := `[{"foo": null}]`
expected := `[{"foo":1}]`
actual, err := JSONMerge([]byte(data), []byte(patch))
assert.NoError(t, err)
assert.Equal(t, expected, string(actual))
})
})
}