forked from BakedSoftware/go-validation
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate_test.go
182 lines (155 loc) · 4.2 KB
/
validate_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
package validate
import (
"fmt"
"reflect"
"sync"
"testing"
)
// invalidNumericTypes is for the types not allowed (numeric tests)
var (
invalidNumericTypes []reflect.Kind
)
// init load the default test data
func init() {
// Build the invalid numeric types
invalidNumericTypes = append(
invalidNumericTypes,
reflect.Array,
reflect.Bool,
reflect.Chan,
reflect.Complex128,
reflect.Complex64,
reflect.Func,
reflect.Map,
reflect.Ptr,
reflect.Slice,
reflect.String,
reflect.Struct,
reflect.UnsafePointer,
)
}
// TestValidationMap_Atomicity
func TestValidationMap_Atomicity(_ *testing.T) {
vm := Map{}
typ := reflect.TypeOf(vm) // todo: go vet: call of reflect.TypeOf copies lock value: govalidation.Map contains sync.Map contains sync.Mutex
wg1 := sync.WaitGroup{}
wg1.Add(1)
wg2 := sync.WaitGroup{}
wg2.Add(2)
count := 10000
go func() {
wg1.Wait()
for i := 0; i < count; i++ {
vm.set(typ, nil)
}
wg2.Done()
}()
go func() {
wg1.Wait()
for i := 0; i < count; i++ {
vm.get(typ)
}
wg2.Done()
}()
wg1.Done() // start !
wg2.Wait()
}
// TestValidation_SetFieldName test setting and getting field name
func TestValidation_SetFieldName(t *testing.T) {
inter, err := minValueValidation("10", reflect.Int)
if err != nil {
t.Fatal(err.Error())
}
// Set the name
testField := "test_field"
inter.SetFieldName(testField)
// Get the name
name := inter.FieldName()
if name != testField {
t.Fatal("Field name was not the same as when set", testField, name)
}
}
// TestValidation_SetFieldIndex test setting and getting field index
func TestValidation_SetFieldIndex(t *testing.T) {
inter, err := minValueValidation("10", reflect.Int)
if err != nil {
t.Fatal(err.Error())
}
// Set the index
indexNumber := 18
inter.SetFieldIndex(indexNumber)
// Get the index
index := inter.FieldIndex()
if index != indexNumber {
t.Fatal("Field index was not the same as when set", index, indexNumber)
}
}
// TestValidation_Validate test using the Validate method (valid and invalid)
func TestValidation_Validate(t *testing.T) {
// Test making an interface
minInterface, err := minValueValidation("10", reflect.Int)
if err != nil {
t.Fatal(err.Error())
}
// Set the field name
minInterface.SetFieldName("field")
// Test running the validate method
var testInt int32 = 1
testVal := reflect.ValueOf(testInt)
errs := minInterface.Validate(8, testVal)
if errs == nil {
t.Fatal("Expected to fail, 8 < 10")
}
// Test failure error response
errs = minInterface.Validate(8, testVal)
if errs.Error() != "field must be greater than or equal to 10" {
t.Fatal("Expected to fail, 8 < 10", errs)
}
}
// TestValidationError_Error tests using the Error method
func TestValidationError_Error(t *testing.T) {
newError := ValidationError{
Key: "the_key",
Message: "the_message",
}
// test if correct
errorResponse := newError.Error()
if errorResponse != newError.Key+" "+newError.Message {
t.Fatal("Error response was not `key message` as expected", errorResponse)
}
}
// TestValidationErrors_Error tests using the Error method
func TestValidationErrors_Error(t *testing.T) {
newError := ValidationError{
Key: "the_key",
Message: "the_message",
}
newError2 := ValidationError{
Key: "the_key2",
Message: "the_message2",
}
sliceOfErrors := ValidationErrors{}
sliceOfErrors = append(sliceOfErrors, newError, newError2)
// test if correct
errorResponse := sliceOfErrors.Error()
if errorResponse != newError.Key+" "+newError.Message+" and 1 other errors" {
t.Fatal("Error response was not `key message` as expected", errorResponse)
}
}
// ExampleValidationError_Error is showing how to use the errors
func ExampleValidationError_Error() {
type Person struct {
// Gender must not be longer than 10 characters
Gender string `validation:"max_length=10"`
}
var p Person
p.Gender = "This is invalid!" // Will fail since its > 10 characters
_, errs := IsValid(p)
fmt.Println(errs[0].Error())
// Output: Gender must be no more than 10 characters
}
// Tests that are still needed for full package coverage
// todo: TestMap_AddValidation(t *testing.T)
// todo: TestMap_IsValid(t *testing.T)
// todo: TestAddValidation(t *testing.T)
// todo: TestIsValid(t *testing.T)