-
Notifications
You must be signed in to change notification settings - Fork 0
/
patchy_test.go
169 lines (159 loc) · 4.63 KB
/
patchy_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
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
160
161
162
163
164
165
166
167
168
169
package patchy
import (
"errors"
"reflect"
"strings"
"testing"
)
func TestToSnakeCase(t *testing.T) {
tests := []struct {
input, want string
}{
{"TitleCaseString", "title_case_string"},
// {"HTTPStatusCode", "http_status_code"},
{"", ""},
{"a", "a"},
{"A", "a"},
{"_", "_"},
}
for _, tt := range tests {
got := ToSnakeCase(tt.input)
if got != tt.want {
t.Errorf("toSnakeCase(%q) = %q; want %q", tt.input, got, tt.want)
}
}
}
func TestGetFieldMetadata(t *testing.T) {
type EmbeddedAddress struct {
Street string `json:"street"`
City string `json:"city"`
}
type Address struct {
Street string `json:"street"`
City string `json:"city"`
}
type Person struct {
EmbeddedAddress `json:"embeddedAddress"`
Name string `json:"name"`
Age int `json:"age"`
Address *Address `json:"address"`
Hobbies []string `json:"hobbies"`
Pets map[string]string `json:"pets"`
}
patchy, err := NewPatchy(reflect.TypeOf(Person{}))
if err != nil {
t.Fatal(err)
}
type TestCase struct {
name string
pointer string
expectedValue *FieldMetadata
expectedError error
}
testCases := []TestCase{
{
name: "regular field",
pointer: "/name",
expectedValue: &FieldMetadata{
Type: reflect.String,
StructFieldName: "Name",
ColumnName: "name",
},
expectedError: nil,
},
{
name: "embedded field",
pointer: "/embeddedAddress/street",
expectedValue: &FieldMetadata{
Type: reflect.String,
// Tags: "json:\"street\"",
StructFieldName: "Street",
ColumnName: "street",
},
expectedError: nil,
},
{
name: "slice index",
pointer: "/hobbies/0",
expectedValue: &FieldMetadata{
Type: reflect.Slice,
SubElemType: reflect.String,
PathTarget: "0",
// Tags: "json:\"hobbies\"",
StructFieldName: "Hobbies",
ColumnName: "hobbies",
},
expectedError: nil,
},
{
name: "map key",
pointer: "/pets/dog",
expectedValue: &FieldMetadata{
Type: reflect.TypeOf(map[string]string{}).Kind(),
SubElemType: reflect.String,
PathTarget: "dog",
// Tags: "json:\"pets\"",
StructFieldName: "Pets",
ColumnName: "pets",
},
expectedError: nil,
},
{
name: "root",
pointer: "",
expectedValue: nil,
expectedError: errors.New("invalid JSON pointer"),
},
{
name: "nonexistent field",
pointer: "/nonexistent",
expectedValue: nil,
expectedError: errors.New("field not found"),
},
{
name: "invalid slice index",
pointer: "/hobbies/invalid",
expectedValue: nil,
expectedError: errors.New("invalid array index"),
},
{
name: "nested nil pointer",
pointer: "/address/notfound",
expectedValue: nil,
expectedError: errors.New("field not found"),
},
}
for _, tc := range testCases {
actualValue, actualError := patchy.getFieldMetadataRec(patchy.entityType, strings.Split(tc.pointer, "/")[1:])
if actualError != nil && tc.expectedError == nil {
t.Errorf("%s: unexpected error found '%v'", tc.name, actualError)
continue
}
if tc.expectedError != nil && actualError == nil {
t.Errorf("%s: expected error '%v' not found", tc.name, tc.expectedError)
continue
}
if tc.expectedError != nil && actualError != nil && tc.expectedError.Error() != actualError.Error() {
t.Errorf("%s: expected error '%v', actual error '%v'", tc.name, tc.expectedError, actualError)
continue
}
if tc.expectedError != nil && actualError != nil && tc.expectedError.Error() == actualError.Error() {
continue
}
if actualValue.Type != tc.expectedValue.Type {
t.Errorf("%s: expected type %v, actual type %v", tc.name, tc.expectedValue.Type, actualValue.Type)
}
if actualValue.SubElemType != tc.expectedValue.SubElemType {
t.Errorf("%s: expected SliceElemType %v, actual SliceElemType %v", tc.name, tc.expectedValue.SubElemType, actualValue.SubElemType)
}
if actualValue.PathTarget != tc.expectedValue.PathTarget {
t.Errorf("%s: expected PathTarget %v, actual PathTarget %v", tc.name, tc.expectedValue.PathTarget, actualValue.PathTarget)
}
if actualValue.ColumnName != tc.expectedValue.ColumnName {
t.Errorf("%s: expected ColumnName %v, actual ColumnName %v", tc.name, tc.expectedValue.ColumnName, actualValue.ColumnName)
}
if actualValue.StructFieldName != tc.expectedValue.StructFieldName {
t.Errorf("%s: expected StructFieldName %s, actual StructFieldName %s", tc.name, tc.expectedValue.StructFieldName, actualValue.StructFieldName)
}
}
}