-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation_test.go
116 lines (104 loc) · 3.91 KB
/
validation_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
package structdbpostgres
import (
"fmt"
"testing"
"time"
)
// Test struct for validation tests
type ValidationTestStruct struct {
ID int64 `json:"test_struct_id"`
Flags int64 `json:"test_struct_flags"`
// Test email validation
PrimaryEmail string `json:"email" 2db:"req"`
EmailSecondary string `json:"email2" 2db:"req email"`
// Test length validation
FirstName string `json:"first_name" 2db:"req lenmin:2 lenmax:30"`
LastName string `json:"last_name" 2db:"req lenmin:0 lenmax:255"`
// Test int value validation
Age int `json:"age" 2db:"valmin:1 valmax:120"`
Price int `json:"price" 2db:"valmin:0 valmax:999"`
// Test regular expression
PostCode string `json:"post_code" 2db:"req lenmin:6 regexp:^[0-9]{2}\\-[0-9]{3}$"`
PostCode2 string `json:"post_code2" 2db:"lenmin:6" 2db_regexp:"^[0-9]{2}\\-[0-9]{3}$"`
// Some other fields
Password string `json:"password"`
CreatedByUserID int64 `json:"created_by_user_id"`
// Test unique tag
Key string `json:"key" 2db:"req uniq lenmin:30 lenmax:255"`
}
func getValidationTestStructWithData() *ValidationTestStruct {
ts := &ValidationTestStruct{}
ts.Flags = 4
ts.PrimaryEmail = "[email protected]"
ts.EmailSecondary = "[email protected]"
ts.FirstName = "John"
ts.LastName = "Smith"
ts.Age = 37
ts.Price = 444
ts.PostCode = "00-000"
ts.PostCode2 = "11-111"
ts.Password = "yyy"
ts.CreatedByUserID = 4
ts.Key = fmt.Sprintf("12345679012345678901234567890%d", time.Now().UnixNano())
return ts
}
// TestValidateWithValidStruct tests if Validate successfully validates object with valid values
func TestValidateWithValidStruct(t *testing.T) {
ts := getValidationTestStructWithData()
b, failedFields, err := testController.Validate(ts, nil)
if !b {
t.Fatalf("Validate failed validate valid struct")
}
if len(failedFields) > 0 {
t.Fatalf("Validate return non-empty failed field list when validating a valid struct")
}
if err != nil {
t.Fatalf("Validate failed to validate valid struct: %s", err.Error())
}
}
// TestValidateWithValidStructAndListOfFields tests if Validate successfully validates object with valid values
func TestValidateWithValidStructAndListOfFields(t *testing.T) {
ts := getValidationTestStructWithData()
ts.Age = 0
ts.FirstName = "x"
ts.Key = "tooshort"
ts.PrimaryEmail = "[email protected]"
b, failedFields, err := testController.Validate(ts, map[string]interface{}{
"PrimaryEmail": true,
"Price": true,
})
if !b {
t.Fatalf("Validate failed to validate listed fields")
}
if len(failedFields) > 0 {
t.Fatalf("Validate return non-empty failed field list when validating listed fields")
}
if err != nil {
t.Fatalf("Validate failed to validate listed fields: %s", err.Error())
}
}
// TestValidateWithInvalidStruct tests if Validate invalidates object with invalid values
func TestValidateWithInvalidStruct(t *testing.T) {
ts := getValidationTestStructWithData()
ts.PrimaryEmail = "invalidemail"
ts.EmailSecondary = "invalidemail"
ts.FirstName = "x"
ts.LastName = "aFbdsZFYxMpUNKCkBrHhhODrMBEHtmRAJjoqSSfUotvsfMXcJGPrCRaDOsyuyrXYfACjsJEMUoxNvTwRMUaWYruOxgzTXJRzobmxaFbdsZFYxMpUNKCkBrHhhODrMBEHtmRAJjoqSSfUotvsfMXcJGPrCRaDOsyuyrXYfACjsJEMUoxNvTwRMUaWYruOxgzTXJRzobmxaFbdsZFYxMpUNKCkBrHhhODrMBEHtmRAJjoqSSfUotvsfMXcJGPrCRaDOsyuyrXYfACjsJEMUoxNvTwRMUaWYruOxgzTXJRzobmxaFbdsZFYxMpUNKCkBrHhhODrMBEHtmRAJjoqSSfUotvsfMXcJGPrCRaDOsyuyrXYfACjsJEMUoxNvTwRMUaWYruOxgzTXJRzobmx"
ts.Age = 0
ts.Price = 1000
ts.PostCode = "inv"
ts.PostCode2 = "inv"
ts.Key = "tooshort"
b, failedFields, err := testController.Validate(ts, nil)
if err != nil {
t.Fatalf("Validate failed with an err")
}
if b {
t.Fatalf("Validate failed to return false for struct with invalid field values")
}
for _, f := range []string{"PrimaryEmail", "EmailSecondary", "FirstName", "LastName", "Age", "Price", "PostCode", "PostCode2", "Key"} {
if failedFields[f] == 0 {
t.Fatalf(fmt.Sprintf("Validate failed to return field %s in failed fields", f))
}
}
}