-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_test.go
More file actions
221 lines (195 loc) · 5.62 KB
/
validator_test.go
File metadata and controls
221 lines (195 loc) · 5.62 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
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
package visivalidator
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type productWithNilMapper struct{}
func newProductWithNilMapper() *productWithNilMapper {
return &productWithNilMapper{}
}
func (p productWithNilMapper) ValidationMap() map[string]ValidateFunc {
return nil
}
type productWithEmptyMapper struct{}
func newProductWithEmptyMapper() *productWithEmptyMapper {
return &productWithEmptyMapper{}
}
func (p productWithEmptyMapper) ValidationMap() map[string]ValidateFunc {
return map[string]ValidateFunc{}
}
type product struct {
name string
unitPrice price
}
func newProduct(name string, unitPrice int) *product {
return &product{
name: name,
unitPrice: price(unitPrice),
}
}
func (p product) validateName() error {
if len(p.name) <= 0 {
return errors.New("name must not be empty")
}
return nil
}
func (p product) ValidationMap() map[string]ValidateFunc {
return map[string]ValidateFunc{
"name": p.validateName,
"price": p.unitPrice.validate,
}
}
type price int
func (p price) validate() error {
if p <= 0 {
return errors.New("price must be positive")
}
return nil
}
func TestValidate(t *testing.T) {
type in struct {
validator ValidationMapper
mask []string
}
type out struct {
errs []string
}
type validatorState struct {
validator ValidationMapper
state string
}
validatorStates := []validatorState{{
state: "validator is nil",
validator: nil,
}, {
state: "validator with nil mapper",
validator: newProductWithNilMapper(),
}, {
state: "validator with empty mapper",
validator: newProductWithEmptyMapper(),
}, {
state: "validator with invalid values",
validator: newProduct("", 0),
}, {
state: "validator with valid values",
validator: newProduct("my product", 123),
}}
type maskState struct {
mask []string
state string
}
maskStates := []maskState{{
state: "mask is nil",
mask: nil,
}, {
state: "mask is empty",
mask: []string{},
}, {
state: "mask with invalid field",
mask: []string{"id"},
}, {
state: "mask with all fields",
mask: []string{"name", "price"},
}, {
state: "mask with some fields",
mask: []string{"price"},
}}
tests := []struct {
message string
in in
out out
}{{
message: fmt.Sprintf("%s", validatorStates[0].state),
in: in{validator: validatorStates[0].validator},
out: out{errs: []string{
ErrNilMapper.Error(),
}},
}, {
message: fmt.Sprintf("%s", validatorStates[1].state),
in: in{validator: validatorStates[1].validator},
out: out{errs: []string{
ErrNilOrEmptyValidationMap.Error(),
}},
}, {
message: fmt.Sprintf("%s", validatorStates[2].state),
in: in{validator: validatorStates[2].validator},
out: out{errs: []string{
ErrNilOrEmptyValidationMap.Error(),
}},
}, {
message: fmt.Sprintf("%s • %s", validatorStates[3].state, maskStates[0].state),
in: in{validator: validatorStates[3].validator, mask: maskStates[0].mask},
out: out{errs: []string{
"field name is invalid: name must not be empty",
"field price is invalid: price must be positive",
}},
}, {
message: fmt.Sprintf("%s • %s", validatorStates[3].state, maskStates[1].state),
in: in{validator: validatorStates[3].validator, mask: maskStates[1].mask},
out: out{errs: []string{
"field name is invalid: name must not be empty",
"field price is invalid: price must be positive",
}},
}, {
message: fmt.Sprintf("%s • %s", validatorStates[3].state, maskStates[2].state),
in: in{validator: validatorStates[3].validator, mask: maskStates[2].mask},
out: out{errs: []string{
"field id is unknown",
}},
}, {
message: fmt.Sprintf("%s • %s", validatorStates[3].state, maskStates[3].state),
in: in{validator: validatorStates[3].validator, mask: maskStates[3].mask},
out: out{errs: []string{
"field name is invalid: name must not be empty",
"field price is invalid: price must be positive",
}},
}, {
message: fmt.Sprintf("%s • %s", validatorStates[3].state, maskStates[4].state),
in: in{validator: validatorStates[3].validator, mask: maskStates[4].mask},
out: out{errs: []string{
"field price is invalid: price must be positive",
}},
}, {
message: fmt.Sprintf("%s • %s", validatorStates[4].state, maskStates[0].state),
in: in{validator: validatorStates[4].validator, mask: maskStates[0].mask},
out: out{errs: nil},
}, {
message: fmt.Sprintf("%s • %s", validatorStates[4].state, maskStates[1].state),
in: in{validator: validatorStates[4].validator, mask: maskStates[1].mask},
out: out{errs: nil},
}, {
message: fmt.Sprintf("%s • %s", validatorStates[4].state, maskStates[2].state),
in: in{validator: validatorStates[4].validator, mask: maskStates[2].mask},
out: out{errs: []string{
"field id is unknown",
}},
}, {
message: fmt.Sprintf("%s • %s", validatorStates[4].state, maskStates[3].state),
in: in{validator: validatorStates[4].validator, mask: maskStates[3].mask},
out: out{errs: nil},
}, {
message: fmt.Sprintf("%s • %s", validatorStates[4].state, maskStates[4].state),
in: in{validator: validatorStates[4].validator, mask: maskStates[4].mask},
out: out{errs: nil},
}}
for _, test := range tests {
err := Validate(test.in.validator, test.in.mask...)
if test.out.errs != nil {
var errs []string
if ve, ok := err.(ValidationErrors); ok {
for _, e := range ve {
errs = append(errs, e.Error())
}
} else {
errs = []string{err.Error()}
}
for i, e := range test.out.errs {
assert.Equal(t, e, errs[i], test.message)
}
} else {
assert.Nil(t, err, test.message)
}
}
}