-
Notifications
You must be signed in to change notification settings - Fork 6
/
serialize_test.go
209 lines (180 loc) · 4.83 KB
/
serialize_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package gody
import (
"fmt"
"testing"
)
type TestSerializeStructA struct {
a string
}
type TestSerializeStructB struct {
b string
a TestSerializeStructA
}
type TestSerializeStructC struct {
c string
B TestSerializeStructB
A TestSerializeStructA
}
func TestSerializeBodyStruct(t *testing.T) {
cases := []struct {
param interface{}
ok bool
}{
{map[string]string{"test-key": "test-value"}, false},
{TestSerializeStructA{a: "a"}, true},
{TestSerializeStructB{b: "b", a: TestSerializeStructA{a: "a"}}, true},
{TestSerializeStructC{c: "c", B: TestSerializeStructB{b: "b", a: TestSerializeStructA{a: "a"}}, A: TestSerializeStructA{a: "a"}}, true},
{10, false},
{struct{}{}, true},
{"", false},
{nil, false},
}
for _, c := range cases {
_, err := Serialize(c.param)
if _, ok := err.(*ErrInvalidBody); ok == c.ok {
t.Error(err)
}
}
}
func TestSerializeBodyTagFormat(t *testing.T) {
cases := []struct {
param interface{}
ok bool
}{
{struct {
Value string `validate:"required"`
}{"test-value"}, true},
{struct {
Value string `validate:"required=true"`
}{"test-value"}, true},
{struct {
Value string `validate:"required="`
}{"test-value"}, false},
{struct {
Value string `validate:"=required="`
}{"test-value"}, false},
{struct {
Value string `validate:"="`
}{"test-value"}, false},
{struct {
Value string
}{"test-value"}, true},
}
for _, c := range cases {
_, err := Serialize(c.param)
if _, ok := err.(*ErrInvalidTag); ok == c.ok {
t.Error(err)
}
}
}
func TestSerialize(t *testing.T) {
body := struct {
A string `validate:"test"`
B int `json:"b"`
C bool `validate:"test test_number=true"`
}{"a-value", 10, true}
fields, err := Serialize(body)
if err != nil {
t.Error(err)
return
}
if got, want := len(fields), 2; got != want {
t.Errorf("Length of serialized fields isn't equals: got: %v want: %v", got, want)
return
}
wantedFields := []Field{
{Name: "a", Value: "a-value", Tags: map[string]string{"test": ""}},
{Name: "c", Value: "true", Tags: map[string]string{"test": "", "test_number": "true"}},
}
if got, want := fmt.Sprint(fields), fmt.Sprint(wantedFields); got != want {
t.Errorf("Serialized fields unexpected: got: %v want: %v", got, want)
return
}
}
type TestSerializeSliceA struct {
E int `validate:"test-slice"`
}
func TestSliceSerialize(t *testing.T) {
body := struct {
A string `validate:"test"`
B []TestSerializeSliceA
}{"a-value", []TestSerializeSliceA{{10}, {}}}
fields, err := Serialize(body)
if err != nil {
t.Error(err)
return
}
if got, want := len(fields), 3; got != want {
t.Errorf("Length of serialized fields isn't equals: got: %v want: %v", got, want)
return
}
wantedFields := []Field{
{Name: "a", Value: "a-value", Tags: map[string]string{"test": ""}},
{Name: "b[0].e", Value: "10", Tags: map[string]string{"test-slice": ""}},
{Name: "b[1].e", Value: "0", Tags: map[string]string{"test-slice": ""}},
}
if got, want := fmt.Sprint(fields), fmt.Sprint(wantedFields); got != want {
t.Errorf("Serialized fields unexpected: got: %v want: %v", got, want)
return
}
}
type TestSerializeStructE struct {
a string `validate:"test-private-struct-field=300"`
}
type TestSerializeStructD struct {
J string `validate:"test-struct"`
I TestSerializeStructE
}
func TestStructSlice(t *testing.T) {
body := struct {
A string `validate:"test"`
B TestSerializeStructD
}{"a-value", TestSerializeStructD{J: "j-test-struct", I: TestSerializeStructE{a: "a-test-private-struct-field"}}}
fields, err := Serialize(body)
if err != nil {
t.Error(err)
return
}
wantedFields := []Field{
{Name: "a", Value: "a-value", Tags: map[string]string{"test": ""}},
{Name: "b.j", Value: "j-test-struct", Tags: map[string]string{"test-struct": ""}},
{Name: "b.i.a", Value: "a-test-private-struct-field", Tags: map[string]string{"test-private-struct-field": "300"}},
}
if got, want := fmt.Sprint(fields), fmt.Sprint(wantedFields); got != want {
t.Errorf("Serialized fields unexpected: got: %v want: %v", got, want)
return
}
}
func TestRawSerializeWithEmptyTagName(t *testing.T) {
_, err := RawSerialize("", nil)
if err == nil {
t.Error("Unexpected nil value for error")
return
}
if _, ok := err.(*ErrEmptyTagName); !ok {
t.Error("Unexpected error type, not equal *ErrEmptyTagName")
return
}
}
func TestRawSerializeWithJSONTagName(t *testing.T) {
body := struct {
A string `json:"b" validate:"not_empty"`
}{}
fields, err := RawSerialize("validate", body)
if err != nil {
t.Error(err)
return
}
field := fields[0]
if got, want := field.Name, "b"; got != want {
t.Errorf("Unexpected field name, got: %s, want: %s", got, want)
return
}
}
func BenchmarkSerializeBodyStruct(b *testing.B) {
b.ResetTimer()
body := map[string]string{"test-key": "test-value"}
for n := 0; n < b.N; n++ {
Serialize(body)
}
}