-
Notifications
You must be signed in to change notification settings - Fork 4
/
min_test.go
202 lines (185 loc) · 4.62 KB
/
min_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
package govalidator
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_MinInt(t *testing.T) {
tests := []struct {
name string
field string
value int
min int
isPassed bool
msg string
expectedMsg string
}{
{
name: "test integer value of `2` will pass if the validator minimum acceptable value is 1",
field: "number",
value: 2,
min: 1,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test integer value of `3` won't pass if the validator minimum acceptable value is 1",
field: "score",
value: 3,
min: 10,
isPassed: false,
msg: "",
expectedMsg: "score should be more than 10",
},
{
name: "test integer value of `17` won't pass if the validator minimum acceptable value is 18",
field: "age",
value: 17,
min: 18,
isPassed: false,
msg: "age must be greater than 18",
expectedMsg: "age must be greater than 18",
},
}
for _, test := range tests {
v := New()
v.MinInt(test.value, test.min, test.field, test.msg)
assert.Equal(t, test.isPassed, v.IsPassed())
if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed: expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}
func Test_MinFloat(t *testing.T) {
tests := []struct {
name string
field string
value float64
min float64
isPassed bool
msg string
expectedMsg string
}{
{
name: "test float value of `2.5` will pass if the validator minimum acceptable value is 1.5",
field: "score",
value: 2.5,
min: 1.5,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test float value of `21` will pass if the validator minimum acceptable value is 11",
field: "score",
value: 21,
min: 11,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test float value of `9.75` won't pass if the validator minimum acceptable value is 10",
field: "goal",
value: 9.75,
min: 10,
isPassed: false,
msg: "",
expectedMsg: "goal should be more than 10",
},
{
name: "test float value of `11.6` won't pass if the validator minimum acceptable value is 121.1",
field: "number",
value: 11.6,
min: 121.1,
isPassed: false,
msg: "number must be greater than 121.1",
expectedMsg: "number must be greater than 121.1",
},
}
for _, test := range tests {
v := New()
v.MinFloat(test.value, test.min, test.field, test.msg)
assert.Equal(t, test.isPassed, v.IsPassed())
if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed: expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}
func Test_MinString(t *testing.T) {
tests := []struct {
name string
field string
value string
min int
isPassed bool
msg string
expectedMsg string
}{
{
name: "test `rey` will pass validation when minimum valid length is 2",
field: "name",
value: "rey",
min: 2,
isPassed: true,
msg: "",
expectedMsg: "",
},
{
name: "test empty string won't pass validation when minimum valid length is 5",
field: "username",
value: "",
min: 5,
isPassed: false,
msg: "",
expectedMsg: "username should have more than 5 characters",
},
{
name: "test empty space string won't pass validation when minimum valid length is 2",
field: "username",
value: " ",
min: 2,
isPassed: false,
msg: "",
expectedMsg: "username should have more than 2 characters",
},
{
name: "test `abcd` won't pass validation when minimum valid length is 7",
field: "alphabet",
value: "abcd",
min: 7,
isPassed: false,
msg: "alphabet should have more than 7 characters",
expectedMsg: "alphabet should have more than 7 characters",
},
}
for _, test := range tests {
v := New()
v.MinString(test.value, test.min, test.field, test.msg)
assert.Equal(t, test.isPassed, v.IsPassed())
if !test.isPassed {
assert.Equalf(
t,
test.expectedMsg,
v.Errors()[test.field],
"test case %q failed: expected: %s, got: %s",
test.expectedMsg,
v.Errors()[test.field],
)
}
}
}