forked from qor/validations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation_test.go
205 lines (174 loc) · 5.15 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
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
package validations_test
import (
"errors"
"fmt"
"regexp"
"testing"
"github.com/asaskevich/govalidator"
"github.com/jinzhu/gorm"
_ "github.com/mattn/go-sqlite3"
"github.com/qor/qor/test/utils"
"github.com/qor/validations"
)
var db *gorm.DB
type User struct {
gorm.Model
Name string `valid:"required"`
Password string `valid:"length(6|20)"`
SecurePassword string `valid:"numeric"`
Email string `valid:"email,uniqEmail~Email already be token"`
CompanyID int
Company Company
CreditCard CreditCard
Addresses []Address
Languages []Language `gorm:"many2many:user_languages"`
}
func (user *User) Validate(db *gorm.DB) {
govalidator.CustomTypeTagMap.Set("uniqEmail", govalidator.CustomTypeValidator(func(email interface{}, context interface{}) bool {
var count int
if db.Model(&User{}).Where("email = ?", email).Count(&count); count == 0 || email == "" {
return true
}
return false
}))
if user.Name == "invalid" {
db.AddError(validations.NewError(user, "Name", "invalid user name"))
}
}
type Company struct {
gorm.Model
Name string
}
func (company *Company) Validate(db *gorm.DB) {
if company.Name == "invalid" {
db.AddError(errors.New("invalid company name"))
}
}
type CreditCard struct {
gorm.Model
UserID int
Number string
}
func (card *CreditCard) Validate(db *gorm.DB) {
if !regexp.MustCompile("^(\\d){13,16}$").MatchString(card.Number) {
db.AddError(validations.NewError(card, "Number", "invalid card number"))
}
}
type Address struct {
gorm.Model
UserID int
Address string
}
func (address *Address) Validate(db *gorm.DB) {
if address.Address == "invalid" {
db.AddError(validations.NewError(address, "Address", "invalid address"))
}
}
type Language struct {
gorm.Model
Code string
}
func (language *Language) Validate(db *gorm.DB) error {
if language.Code == "invalid" {
return validations.NewError(language, "Code", "invalid language")
}
return nil
}
func init() {
db = utils.TestDB()
validations.RegisterCallbacks(db)
tables := []interface{}{&User{}, &Company{}, &CreditCard{}, &Address{}, &Language{}}
for _, table := range tables {
if err := db.DropTableIfExists(table).Error; err != nil {
panic(err)
}
db.AutoMigrate(table)
}
}
func TestGoValidation(t *testing.T) {
user := User{Name: "", Password: "123123", Email: "[email protected]"}
result := db.Save(&user)
if result.Error == nil {
t.Errorf("Should get error when save empty user")
}
if result.Error.Error() != "Name can't be blank" {
t.Errorf("Error message should be equal `Name can't be blank`")
}
user = User{Name: "", Password: "123", SecurePassword: "AB123", Email: "aagmail.com"}
result = db.Save(&user)
messages := []string{"Name can't be blank",
"Password is the wrong length (should be 6~20 characters)",
"SecurePassword is not a number",
"Email is not a valid email address"}
for i, err := range result.GetErrors() {
if messages[i] != err.Error() {
t.Errorf(fmt.Sprintf("Error message should be equal `%v`, but it is `%v`", messages[i], err.Error()))
}
}
user = User{Name: "A", Password: "123123", Email: "[email protected]"}
result = db.Save(&user)
user = User{Name: "B", Password: "123123", Email: "[email protected]"}
if result := db.Save(&user); result.Error.Error() != "Email already be token" {
t.Errorf("Should get email alredy be token error")
}
}
func TestSaveInvalidUser(t *testing.T) {
user := User{Name: "invalid"}
if result := db.Save(&user); result.Error == nil {
t.Errorf("Should get error when save invalid user")
}
}
func TestSaveInvalidCompany(t *testing.T) {
user := User{
Name: "valid",
Company: Company{Name: "invalid"},
}
if result := db.Save(&user); result.Error == nil {
t.Errorf("Should get error when save invalid company")
}
}
func TestSaveInvalidCreditCard(t *testing.T) {
user := User{
Name: "valid",
Company: Company{Name: "valid"},
CreditCard: CreditCard{Number: "invalid"},
}
if result := db.Save(&user); result.Error == nil {
t.Errorf("Should get error when save invalid credit card")
}
}
func TestSaveInvalidAddresses(t *testing.T) {
user := User{
Name: "valid",
Company: Company{Name: "valid"},
CreditCard: CreditCard{Number: "4111111111111111"},
Addresses: []Address{{Address: "invalid"}},
}
if result := db.Save(&user); result.Error == nil {
t.Errorf("Should get error when save invalid addresses")
}
}
func TestSaveInvalidLanguage(t *testing.T) {
user := User{
Name: "valid",
Company: Company{Name: "valid"},
CreditCard: CreditCard{Number: "4111111111111111"},
Addresses: []Address{{Address: "valid"}},
Languages: []Language{{Code: "invalid"}},
}
if result := db.Save(&user); result.Error == nil {
t.Errorf("Should get error when save invalid language")
}
}
func TestSaveAllValidData(t *testing.T) {
user := User{
Name: "valid",
Company: Company{Name: "valid"},
CreditCard: CreditCard{Number: "4111111111111111"},
Addresses: []Address{{Address: "valid1"}, {Address: "valid2"}},
Languages: []Language{{Code: "valid1"}, {Code: "valid2"}},
}
if result := db.Save(&user); result.Error != nil {
t.Errorf("Should get no error when save valid data, but got: %v", result.Error)
}
}