github.com/astaxie/beego@v1.12.3/validation/util_test.go (about)

     1  // Copyright 2014 beego Author. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package validation
    16  
    17  import (
    18  	"log"
    19  	"reflect"
    20  	"testing"
    21  )
    22  
    23  type user struct {
    24  	ID    int
    25  	Tag   string `valid:"Maxx(aa)"`
    26  	Name  string `valid:"Required;"`
    27  	Age   int    `valid:"Required; Range(1, 140)"`
    28  	match string `valid:"Required; Match(/^(test)?\\w*@(/test/);com$/);Max(2)"`
    29  }
    30  
    31  func TestGetValidFuncs(t *testing.T) {
    32  	u := user{Name: "test", Age: 1}
    33  	tf := reflect.TypeOf(u)
    34  	var vfs []ValidFunc
    35  	var err error
    36  
    37  	f, _ := tf.FieldByName("ID")
    38  	if vfs, err = getValidFuncs(f); err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	if len(vfs) != 0 {
    42  		t.Fatal("should get none ValidFunc")
    43  	}
    44  
    45  	f, _ = tf.FieldByName("Tag")
    46  	if _, err = getValidFuncs(f); err.Error() != "doesn't exists Maxx valid function" {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	f, _ = tf.FieldByName("Name")
    51  	if vfs, err = getValidFuncs(f); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	if len(vfs) != 1 {
    55  		t.Fatal("should get 1 ValidFunc")
    56  	}
    57  	if vfs[0].Name != "Required" && len(vfs[0].Params) != 0 {
    58  		t.Error("Required funcs should be got")
    59  	}
    60  
    61  	f, _ = tf.FieldByName("Age")
    62  	if vfs, err = getValidFuncs(f); err != nil {
    63  		t.Fatal(err)
    64  	}
    65  	if len(vfs) != 2 {
    66  		t.Fatal("should get 2 ValidFunc")
    67  	}
    68  	if vfs[0].Name != "Required" && len(vfs[0].Params) != 0 {
    69  		t.Error("Required funcs should be got")
    70  	}
    71  	if vfs[1].Name != "Range" && len(vfs[1].Params) != 2 {
    72  		t.Error("Range funcs should be got")
    73  	}
    74  
    75  	f, _ = tf.FieldByName("match")
    76  	if vfs, err = getValidFuncs(f); err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	if len(vfs) != 3 {
    80  		t.Fatal("should get 3 ValidFunc but now is", len(vfs))
    81  	}
    82  }
    83  
    84  type User struct {
    85  	Name string `valid:"Required;MaxSize(5)" `
    86  	Sex  string `valid:"Required;" label:"sex_label"`
    87  	Age  int    `valid:"Required;Range(1, 140);" label:"age_label"`
    88  }
    89  
    90  func TestValidation(t *testing.T) {
    91  	u := User{"man1238888456", "", 1140}
    92  	valid := Validation{}
    93  	b, err := valid.Valid(&u)
    94  	if err != nil {
    95  		// handle error
    96  	}
    97  	if !b {
    98  		// validation does not pass
    99  		// blabla...
   100  		for _, err := range valid.Errors {
   101  			log.Println(err.Key, err.Message)
   102  		}
   103  		if len(valid.Errors) != 3 {
   104  			t.Error("must be has 3 error")
   105  		}
   106  	} else {
   107  		t.Error("must be has 3 error")
   108  	}
   109  }
   110  
   111  func TestCall(t *testing.T) {
   112  	u := user{Name: "test", Age: 180}
   113  	tf := reflect.TypeOf(u)
   114  	var vfs []ValidFunc
   115  	var err error
   116  	f, _ := tf.FieldByName("Age")
   117  	if vfs, err = getValidFuncs(f); err != nil {
   118  		t.Fatal(err)
   119  	}
   120  	valid := &Validation{}
   121  	vfs[1].Params = append([]interface{}{valid, u.Age}, vfs[1].Params...)
   122  	if _, err = funcs.Call(vfs[1].Name, vfs[1].Params...); err != nil {
   123  		t.Fatal(err)
   124  	}
   125  	if len(valid.Errors) != 1 {
   126  		t.Error("age out of range should be has an error")
   127  	}
   128  }