2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2013-07-23 06:42:14 +00:00
|
|
|
package validation
|
|
|
|
|
|
|
|
import (
|
2020-06-01 07:06:33 +00:00
|
|
|
"log"
|
2013-07-23 06:42:14 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type user struct {
|
2015-09-12 16:13:19 +00:00
|
|
|
ID int
|
2013-07-28 11:22:09 +00:00
|
|
|
Tag string `valid:"Maxx(aa)"`
|
|
|
|
Name string `valid:"Required;"`
|
2020-06-23 02:43:51 +00:00
|
|
|
Age int `valid:"Required; Range(1, 140)"`
|
2013-07-28 11:22:09 +00:00
|
|
|
match string `valid:"Required; Match(/^(test)?\\w*@(/test/);com$/);Max(2)"`
|
2013-07-23 06:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetValidFuncs(t *testing.T) {
|
|
|
|
u := user{Name: "test", Age: 1}
|
|
|
|
tf := reflect.TypeOf(u)
|
|
|
|
var vfs []ValidFunc
|
|
|
|
var err error
|
|
|
|
|
2015-09-12 16:13:19 +00:00
|
|
|
f, _ := tf.FieldByName("ID")
|
2013-07-23 06:42:14 +00:00
|
|
|
if vfs, err = getValidFuncs(f); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(vfs) != 0 {
|
|
|
|
t.Fatal("should get none ValidFunc")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, _ = tf.FieldByName("Tag")
|
2020-02-14 08:47:47 +00:00
|
|
|
if _, err = getValidFuncs(f); err.Error() != "doesn't exists Maxx valid function" {
|
2013-07-23 06:42:14 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, _ = tf.FieldByName("Name")
|
|
|
|
if vfs, err = getValidFuncs(f); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(vfs) != 1 {
|
|
|
|
t.Fatal("should get 1 ValidFunc")
|
|
|
|
}
|
|
|
|
if vfs[0].Name != "Required" && len(vfs[0].Params) != 0 {
|
|
|
|
t.Error("Required funcs should be got")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, _ = tf.FieldByName("Age")
|
|
|
|
if vfs, err = getValidFuncs(f); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(vfs) != 2 {
|
|
|
|
t.Fatal("should get 2 ValidFunc")
|
|
|
|
}
|
|
|
|
if vfs[0].Name != "Required" && len(vfs[0].Params) != 0 {
|
|
|
|
t.Error("Required funcs should be got")
|
|
|
|
}
|
|
|
|
if vfs[1].Name != "Range" && len(vfs[1].Params) != 2 {
|
|
|
|
t.Error("Range funcs should be got")
|
|
|
|
}
|
2013-07-28 11:22:09 +00:00
|
|
|
|
|
|
|
f, _ = tf.FieldByName("match")
|
|
|
|
if vfs, err = getValidFuncs(f); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(vfs) != 3 {
|
|
|
|
t.Fatal("should get 3 ValidFunc but now is", len(vfs))
|
|
|
|
}
|
2013-07-23 06:42:14 +00:00
|
|
|
}
|
2013-07-23 17:20:24 +00:00
|
|
|
|
2020-06-01 07:06:33 +00:00
|
|
|
type User struct {
|
|
|
|
Name string `valid:"Required;MaxSize(5)" `
|
2020-06-23 02:43:51 +00:00
|
|
|
Sex string `valid:"Required;" label:"sex_label"`
|
|
|
|
Age int `valid:"Required;Range(1, 140);" label:"age_label"`
|
2020-06-01 07:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidation(t *testing.T) {
|
|
|
|
u := User{"man1238888456", "", 1140}
|
|
|
|
valid := Validation{}
|
|
|
|
b, err := valid.Valid(&u)
|
|
|
|
if err != nil {
|
|
|
|
// handle error
|
|
|
|
}
|
|
|
|
if !b {
|
|
|
|
// validation does not pass
|
|
|
|
// blabla...
|
|
|
|
for _, err := range valid.Errors {
|
|
|
|
log.Println(err.Key, err.Message)
|
|
|
|
}
|
2020-06-18 09:25:48 +00:00
|
|
|
if len(valid.Errors) != 3 {
|
|
|
|
t.Error("must be has 3 error")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Error("must be has 3 error")
|
2020-06-01 07:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-23 17:20:24 +00:00
|
|
|
func TestCall(t *testing.T) {
|
|
|
|
u := user{Name: "test", Age: 180}
|
|
|
|
tf := reflect.TypeOf(u)
|
|
|
|
var vfs []ValidFunc
|
|
|
|
var err error
|
|
|
|
f, _ := tf.FieldByName("Age")
|
|
|
|
if vfs, err = getValidFuncs(f); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
valid := &Validation{}
|
2013-07-24 04:20:42 +00:00
|
|
|
vfs[1].Params = append([]interface{}{valid, u.Age}, vfs[1].Params...)
|
|
|
|
if _, err = funcs.Call(vfs[1].Name, vfs[1].Params...); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2013-07-23 17:20:24 +00:00
|
|
|
if len(valid.Errors) != 1 {
|
|
|
|
t.Error("age out of range should be has an error")
|
|
|
|
}
|
|
|
|
}
|