forked from mattbaird/jsonpatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonpatch_optimise_test.go
42 lines (36 loc) · 1.75 KB
/
jsonpatch_optimise_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
package jsonpatch
import (
"fmt"
"log"
"testing"
"github.com/stretchr/testify/assert"
)
const lorem = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
// Test should replace the entire array instead of doing separate array operations
func TestReplaceInsteadOfArrayOps(t *testing.T) {
patch, e := CreatePatch([]byte(`{"a":[1, 2, 3]}`), []byte(`{"a":[1, 0, 0]}`))
assert.NoError(t, e)
log.Println(patch)
assert.Equal(t, 1, len(patch))
p := patch[0]
assert.Equal(t, "replace", p.Operation)
assert.Equal(t, "/a", p.Path)
assert.Equal(t, []interface{}{float64(1), float64(0), float64(0)}, p.Value)
}
// Test should do individual array operations because one of the constant values is too big for an efficient replace
func TestReplaceObjectInArray(t *testing.T) {
a := fmt.Sprintf(`[1, 2, {"a": "%s", "b": "2"}]`, lorem)
b := fmt.Sprintf(`[1, 2, {"a": "%s", "b": "1", "c": "3"}]`, lorem)
patch, e := CreatePatch([]byte(a), []byte(b))
assert.NoError(t, e)
log.Println(patch)
assert.Equal(t, 2, len(patch))
p1 := patch[0]
assert.Equal(t, "replace", p1.Operation)
assert.Equal(t, "/2/b", p1.Path)
assert.Equal(t, "1", p1.Value)
p2 := patch[1]
assert.Equal(t, "add", p2.Operation)
assert.Equal(t, "/2/c", p2.Path)
assert.Equal(t, "3", p2.Value)
}