forked from mattbaird/jsonpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonpatch_object_test.go
More file actions
53 lines (46 loc) · 2.28 KB
/
Copy pathjsonpatch_object_test.go
File metadata and controls
53 lines (46 loc) · 2.28 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
package jsonpatch
import (
"testing"
"github.com/stretchr/testify/assert"
)
var simpleObj = `{"a":100, "b":20}`
var simpleObjModifyProp = `{"b":250}`
var simpleObjAddProp = `{"c":"hello"}`
func TestCreatePatch_ModifyProperty_GeneratesReplaceOperation(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObj), []byte(simpleObjModifyProp), setTestCollections, setTestIgnoredFields, PatchStrategyEnsureExists)
assert.NoError(t, err)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "replace", change.Operation, "they should be equal")
assert.Equal(t, "/b", change.Path, "they should be equal")
var expected float64 = 250
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_AddProperty_GeneratesAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(simpleObj), []byte(simpleObjAddProp), 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, "/c", change.Path, "they should be equal")
assert.Equal(t, "hello", change.Value, "they should be equal")
}
func TestCreatePatch_NestedObject_ModifyProperty_GeneratesReplaceOperation(t *testing.T) {
patch, err := CreatePatch([]byte(nestedObj), []byte(nestedObjModifyProp), setTestCollections, setTestIgnoredFields, PatchStrategyEnsureExists)
assert.NoError(t, err)
assert.Equal(t, 1, len(patch), "they should be equal")
change := patch[0]
assert.Equal(t, "replace", change.Operation, "they should be equal")
assert.Equal(t, "/b/c", change.Path, "they should be equal")
var expected float64 = 250
assert.Equal(t, expected, change.Value, "they should be equal")
}
func TestCreatePatch_NestedObject_AddProperty_GeneratesAddOperation(t *testing.T) {
patch, err := CreatePatch([]byte(nestedObj), []byte(nestedObjAddProp), 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/d", change.Path, "they should be equal")
assert.Equal(t, "hello", change.Value, "they should be equal")
}