-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathpatch_test.go
More file actions
159 lines (146 loc) · 3.44 KB
/
patch_test.go
File metadata and controls
159 lines (146 loc) · 3.44 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package jsondiff
import (
"encoding/json"
"errors"
"os"
"testing"
)
func TestPatch_Invert(t *testing.T) {
t.Run("ambiguous-copy-op", func(t *testing.T) {
p := Patch{
Operation{Type: OperationCopy, From: "/foo", Path: "/foo/bar"},
}
_, err := p.Invert()
if !errors.Is(err, ErrAmbiguousCopyOp) {
t.Errorf("expected ErrAmbiguousCopyOp, got %v", err)
}
})
t.Run("remove-missing-test", func(t *testing.T) {
p := Patch{
Operation{Type: OperationRemove, From: "/bar", Path: "/baz"},
}
_, err := p.Invert()
if !errors.Is(err, ErrNonReversible) {
t.Errorf("expected ErrNonReversible, got %T (%s)", err, err)
}
})
t.Run("replace-missing-test", func(t *testing.T) {
p := Patch{
Operation{Type: OperationReplace, From: "/bar", Path: "/baz"},
}
_, err := p.Invert()
if !errors.Is(err, ErrNonReversible) {
t.Errorf("expected ErrNonReversible, got %T (%s)", err, err)
}
})
t.Run("mismatch-test-pointer", func(t *testing.T) {
p := Patch{
Operation{Type: OperationTest, Path: "/a", Value: "1"},
Operation{Type: OperationReplace, Path: "/b", Value: "3"},
}
_, err := p.Invert()
var testPtrErr *ErrTestPointer
if errors.As(err, &testPtrErr) {
if testPtrErr.Op != OperationReplace {
t.Errorf("expected replace op in error, got %v", testPtrErr.Op)
}
if testPtrErr.Error() == "" {
t.Errorf("expected non empty stringified error")
}
} else {
t.Errorf("expected ErrTestPointer, got %T", err)
}
})
t.Run("object", func(t *testing.T) {
cases, err := testCasesFromFile(t, "testdata/tests/jsonpatch/object.json")
if err != nil {
t.Fatal(err)
}
for _, c := range cases {
testInvert(t, c.Before, c.After)
}
})
t.Run("array", func(t *testing.T) {
cases, err := testCasesFromFile(t, "testdata/tests/jsonpatch/array.json")
if err != nil {
t.Fatal(err)
}
for _, c := range cases {
testInvert(t, c.Before, c.After)
}
})
}
type rawTestCase struct {
Before json.RawMessage `json:"before"`
After json.RawMessage `json:"after"`
}
func testCasesFromFile(t *testing.T, file string) ([]rawTestCase, error) {
t.Helper()
b, err := os.ReadFile(file)
if err != nil {
return nil, err
}
var cases []rawTestCase
if err := json.Unmarshal(b, &cases); err != nil {
return nil, err
}
return cases, nil
}
func testInvert(t *testing.T, src, tgt []byte) {
t.Helper()
t.Run("inverse", func(t *testing.T) {
p, err := CompareJSON(src, tgt, Invertible())
if err != nil {
t.Fatal(err)
}
ip, err := p.Invert()
if err != nil {
t.Fatal(err)
}
t.Logf("%s", ip.String())
src2, err := ip.apply(tgt, true)
if err != nil {
t.Fatal(err)
}
if !jsonBytesEqual(t, src, src2) {
t.Errorf("expected reverted document to equal source")
}
})
t.Run("involution", func(t *testing.T) {
p, err := CompareJSON(src, tgt, Invertible())
if err != nil {
t.Fatal(err)
}
ip, err := p.Invert()
if err != nil {
t.Fatal(err)
}
t.Logf("%s", ip.String())
iip, err := ip.Invert()
if err != nil {
t.Fatal(err)
}
t.Logf("%s", iip.String())
tgt2, err := iip.apply(src, true)
if err != nil {
t.Fatal(err)
}
if !jsonBytesEqual(t, tgt, tgt2) {
t.Errorf("expected double inverted document to equal target")
}
})
}
func jsonBytesEqual(t *testing.T, a, b []byte) bool {
t.Helper()
var (
aa any
bb any
)
if err := json.Unmarshal(a, &aa); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(b, &bb); err != nil {
t.Fatal(err)
}
return deepEqual(aa, bb)
}