Repository navigation
Expand file tree
/
Copy pathpatch_extract_test.go
More file actions
121 lines (111 loc) · 2.91 KB
/
Copy pathpatch_extract_test.go
File metadata and controls
121 lines (111 loc) · 2.91 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
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
package jsonpatch
import (
"encoding/json"
"testing"
)
func mustUnmarshal(t *testing.T, s string) any {
t.Helper()
var v any
if err := json.Unmarshal([]byte(s), &v); err != nil {
t.Fatalf("unmarshal: %v", err)
}
return v
}
func TestExtractAdded_ArrayAppendDash(t *testing.T) {
after := mustUnmarshal(t, `["a","b","c"]`)
patch := Patch{
{Op: Add, Path: "/-", Value: "c"},
}
rem, add, err := ExtractAdded(after, patch)
if err != nil {
t.Fatalf("ExtractAdded error: %v", err)
}
remJSON, _ := json.Marshal(rem)
addJSON, _ := json.Marshal(add)
if string(remJSON) != `["a","b"]` {
t.Fatalf("remaining mismatch: %s", remJSON)
}
if string(addJSON) != `["c"]` {
t.Fatalf("addedOnly mismatch: %s", addJSON)
}
// Ensure after unchanged
afterJSON, _ := json.Marshal(after)
if string(afterJSON) != `["a","b","c"]` {
t.Fatalf("after mutated: %s", afterJSON)
}
}
func TestExtractAdded_ArrayNumericInsideBase(t *testing.T) {
after := mustUnmarshal(t, `["a","x","b"]`)
patch := Patch{
{Op: Add, Path: "/1", Value: "x"},
}
rem, add, err := ExtractAdded(after, patch)
if err != nil {
t.Fatalf("ExtractAdded error: %v", err)
}
remJSON, _ := json.Marshal(rem)
addJSON, _ := json.Marshal(add)
if string(remJSON) != `["a","b"]` {
t.Fatalf("remaining mismatch: %s", remJSON)
}
if string(addJSON) != `["x"]` {
t.Fatalf("addedOnly mismatch: %s", addJSON)
}
}
func TestExtractAdded_ObjectNested(t *testing.T) {
after := mustUnmarshal(t, `{"a":{"b":{"c":1}}}`)
patch := Patch{
{Op: Add, Path: "/a/b/c", Value: 1},
}
rem, add, err := ExtractAdded(after, patch)
if err != nil {
t.Fatalf("ExtractAdded error: %v", err)
}
remJSON, _ := json.Marshal(rem)
addJSON, _ := json.Marshal(add)
if string(remJSON) != `{"a":{"b":{}}}` {
t.Fatalf("remaining mismatch: %s", remJSON)
}
if string(addJSON) != `{"a":{"b":{"c":1}}}` {
t.Fatalf("addedOnly mismatch: %s", addJSON)
}
}
func TestExtractAdded_ObjectRepeatedKey_LastWins(t *testing.T) {
after := mustUnmarshal(t, `{"x":2}`)
patch := Patch{
{Op: Add, Path: "/x", Value: 1},
{Op: Add, Path: "/x", Value: 2},
}
rem, add, err := ExtractAdded(after, patch)
if err != nil {
t.Fatalf("ExtractAdded error: %v", err)
}
remJSON, _ := json.Marshal(rem)
addJSON, _ := json.Marshal(add)
if string(remJSON) != `{}` {
t.Fatalf("remaining mismatch: %s", remJSON)
}
if string(addJSON) != `{"x":2}` {
t.Fatalf("addedOnly mismatch: %s", addJSON)
}
}
func TestExtractAdded_ErrRootAdd(t *testing.T) {
after := mustUnmarshal(t, `{"a":1}`)
patch := Patch{
{Op: Add, Path: "", Value: map[string]any{"b": 2}},
}
_, _, err := ExtractAdded(after, patch)
if err == nil {
t.Fatalf("expected error for root-level add")
}
}
func TestExtractAdded_ErrMissingParent(t *testing.T) {
after := mustUnmarshal(t, `{"z":1}`)
patch := Patch{
{Op: Add, Path: "/a/b", Value: 1},
}
_, _, err := ExtractAdded(after, patch)
if err == nil {
t.Fatalf("expected error for missing parent")
}
}