-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_patchrequests_test.go
99 lines (95 loc) · 2.09 KB
/
model_patchrequests_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package jsonpatch
import (
"encoding/json"
"reflect"
"testing"
)
func TestPatchRequests_Apply(t *testing.T) {
type testCase[T any] struct {
name string
patchRequests PatchRequests[T]
input *T
newEmptyInputFunc func() *T
expect *T
expectErr bool
}
tests := []testCase[MyStruct]{
{
name: "apply_replace",
patchRequests: PatchRequests[MyStruct]{
Patches: []*PatchRequest[MyStruct]{
{
Operation: "replace",
Path: "$.field_string",
Value: "bar",
},
},
},
newEmptyInputFunc: NewMyStruct,
input: &MyStruct{
FieldString: "foo",
},
expect: &MyStruct{
FieldString: "bar",
},
expectErr: false,
},
{
name: "apply_remove_string",
patchRequests: PatchRequests[MyStruct]{
Patches: []*PatchRequest[MyStruct]{
{
Operation: "remove",
Path: "$.field_string",
},
},
},
newEmptyInputFunc: NewMyStruct,
input: &MyStruct{
FieldString: "foo",
FieldStringPtr: getPtr("fooz"),
},
expect: &MyStruct{
FieldString: "",
FieldStringPtr: getPtr("fooz"),
},
expectErr: false,
},
{
name: "apply_remove_string_ptr",
patchRequests: PatchRequests[MyStruct]{
Patches: []*PatchRequest[MyStruct]{
{
Operation: "remove",
Path: "$.field_string_ptr",
},
},
},
newEmptyInputFunc: NewMyStruct,
input: &MyStruct{
FieldString: "foo",
FieldStringPtr: getPtr("fooz"),
},
expect: &MyStruct{
FieldString: "foo",
FieldStringPtr: nil,
},
expectErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.patchRequests.Apply(tt.input, tt.newEmptyInputFunc)
if (err != nil) != tt.expectErr {
t.Errorf("Apply() error = %v, expectErr %v", err, tt.expectErr)
return
}
if !reflect.DeepEqual(got, tt.expect) {
bPatched, _ := json.Marshal(got)
bExpected, _ := json.Marshal(tt.expect)
t.Errorf("Apply() got = %s", string(bPatched))
t.Errorf("Apply() expect = %s", string(bExpected))
}
})
}
}