-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice_test.go
348 lines (331 loc) · 7.41 KB
/
service_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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package govalidator
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestService_Validate(t *testing.T) {
type RecordHas struct {
Id bool
Name bool
Phone bool
Email bool
}
type Req struct {
Id int `validate:"required"`
}
type Record struct {
Id int
Name *string `validate:"required"`
Phone string `validate:"phone"`
Email string `validate:"omitempty,email"`
Has *RecordHas `setMarker:"true"`
}
type BasicRecord struct {
ID int
Phone string `validate:"phone"`
Email string `validate:"omitempty,email"`
}
type RequiredCheck struct {
ID int
Name string `validate:"required"`
}
type SkipRoot struct {
ID int
Record *BasicRecord `validate:"skipPath"`
}
var testCases = []struct {
description string
input interface{}
expectFailed bool
options []Option
}{
{
description: "required int",
input: struct {
N int `validate:"required"`
}{N: 1},
expectFailed: false,
},
{
description: "required string",
input: struct {
N *string `validate:"required"`
}{N: stringPtr("ddd")},
expectFailed: false,
},
{
description: "required string",
input: struct {
N *string `validate:"required"`
}{N: nil},
expectFailed: true,
},
{
description: "basic email validation",
input: struct {
Email string `validate:"email"`
}{
Email: "abc",
},
expectFailed: true,
},
{
description: "nested validation",
input: struct {
ID int
Contact struct {
Email string `validate:"email"`
}
}{
ID: 1,
Contact: struct {
Email string `validate:"email"`
}{"xyz"},
},
expectFailed: true,
},
{
description: "repeated validation",
input: struct {
ID int
Contact []struct {
XX int
Email string `validate:"email"`
}
}{
ID: 1,
Contact: []struct {
XX int
Email string `validate:"email"`
}{{0, "xyz"}, {0, "zz@wp.pl"}, {0, "rrrr"}},
},
expectFailed: true,
},
{
description: "repeated ptr validation",
input: []*BasicRecord{{ID: 1, Phone: "213-300-2222"}, {ID: 1, Phone: "213-300-22222"}, {ID: 1, Email: "aaa"}},
expectFailed: true,
},
{
description: "valid phone",
input: &BasicRecord{ID: 1, Phone: "213-300-2222"},
expectFailed: false,
},
{
description: "repeated ptr validation",
input: SkipRoot{Record: &BasicRecord{ID: 1, Phone: "213-300-085"}},
expectFailed: true,
},
{
description: "required",
input: &RequiredCheck{ID: 1323},
expectFailed: true,
},
{
description: "required string",
input: struct {
N string `validate:"required"`
}{N: "ddd"},
expectFailed: false,
},
{
description: "required struct - zero value timestamp",
input: struct {
time time.Time `validate:"required"`
}{time: time.Time{}}, // zero value time time.Parse(time.RFC3339, "0001-01-01T00:00:00Z")
expectFailed: true,
},
{
description: "required struct - non zero value timestamp",
input: struct {
time time.Time `validate:"required"`
}{time: getTime("2023-06-29T23:09:15Z")},
expectFailed: false,
},
{
description: "With Presence pass",
input: &Record{
Id: 1,
Has: &RecordHas{},
},
options: []Option{WithSetMarker()},
expectFailed: false,
},
{
description: "With marker failed",
input: &Record{
Id: 1,
Has: &RecordHas{
Name: true,
},
},
options: []Option{WithSetMarker()},
expectFailed: true,
},
{
description: "shallow",
input: SkipRoot{Record: &BasicRecord{ID: 1, Phone: "213-300-085"}},
expectFailed: false,
options: []Option{WithShallow(true)},
},
{
description: "ge passed",
input: struct {
Value int `validate:"ge=3"`
}{5},
expectFailed: false,
},
{
description: "gte failed",
input: struct {
N string
F float64
Value int `validate:"gte=6"`
}{"", 0.0, 5},
expectFailed: true,
},
{
description: "phone valid ptr",
input: struct {
Phone *string `validate:"omitempty,phone"`
}{Phone: stringPtr("213-222-0001")},
expectFailed: false,
},
{
description: "valid domain",
input: struct {
Phone *string `validate:"omitempty,domain"`
}{Phone: stringPtr("wp.pl")},
expectFailed: false,
},
{
description: "invalid domain",
input: struct {
Phone *string `validate:"omitempty,domain"`
}{Phone: stringPtr("wp-.pl")},
expectFailed: true,
},
{
description: "valid www domain",
input: struct {
Value *string `validate:"omitempty,wwwDomain"`
}{Value: stringPtr("www.wp.pl")},
expectFailed: false,
},
{
description: "invalid www domain",
input: struct {
Value *string `validate:"omitempty,wwwDomain"`
}{Value: stringPtr("lll.wp.pl")},
expectFailed: true,
},
{
description: "valid top level domain",
input: struct {
Value *string `validate:"omitempty,domain,nonWWWDomain"`
}{Value: stringPtr("lll.wp.pl")},
expectFailed: false,
},
{
description: "invalid top level domain",
input: struct {
Value *string `validate:"omitempty,domain,nonWWWDomain"`
}{Value: stringPtr("www.wp.pl")},
expectFailed: true,
},
{
description: "less than 3 - violation",
input: struct {
Value *string `validate:"omitempty,lt(3)"`
}{Value: stringPtr("434")},
expectFailed: true,
},
{
description: "less than 3 - valid",
input: struct {
Value string `validate:"omitempty,lt(5)"`
}{Value: "123"},
expectFailed: false,
},
{
description: "less than 300- valid",
input: struct {
Value int `validate:"omitempty,gt(0),lt(300)"`
}{Value: 200},
expectFailed: false,
},
{
description: "less between 0 .. 300 - invalid",
input: struct {
Value int `validate:"omitempty,gt(0),lt(100)"`
}{Value: 200},
expectFailed: true,
},
{
description: "less between 0 .. 300 - invalid",
input: struct {
Value int `validate:"omitempty,gt(0),lt(100)"`
}{Value: -1},
expectFailed: true,
},
{
description: "IAB category - valid 1",
input: struct {
Value string `validate:"omitempty,iabCategory"`
}{Value: "IAB2-22"},
expectFailed: false,
},
{
description: "IAB category- valid 2",
input: struct {
Value string `validate:"omitempty,iabCategory"`
}{Value: "IAB2"},
expectFailed: false,
},
{
description: "IAB categories - valid",
input: struct {
Value string `validate:"omitempty,iabCategories"`
}{Value: "IAB2-22,IAB8"},
expectFailed: false,
},
{
description: "IAB categories - invalid",
input: struct {
Value string `validate:"omitempty,iabCategories"`
}{Value: "sAB2-22,IAB8"},
expectFailed: true,
},
{
description: "choice valid",
input: struct {
Value string `validate:"choice(AZ,AK,ZZ),omitempty"`
}{Value: "AK"},
expectFailed: false,
},
}
for _, testCase := range testCases {
srv := New()
validation, err := srv.Validate(context.Background(), testCase.input, testCase.options...)
if !assert.Nil(t, err, testCase.description) {
continue
}
if !assert.EqualValues(t, testCase.expectFailed, validation.Failed, testCase.description) {
fmt.Printf("%v\n", validation.String())
fmt.Printf("%v", validation)
}
}
}
func stringPtr(s string) *string {
return &s
}
func getTime(timeStr string) time.Time {
r, err := time.Parse(time.RFC3339, timeStr)
if err != nil {
return time.Time{}
}
return r
}