github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/validate/test/mikilin_test.go (about) 1 package main 2 3 import ( 4 "github.com/isyscore/isc-gobase/validate" 5 "testing" 6 ) 7 8 type MikilinBaseEntity struct { 9 Name string `match:"value={zhou, 宋江} isBlank=true"` 10 Age int `match:"value={12, 13}"` 11 } 12 13 type MikilinBaseEntity2 struct { 14 Name string `match:"value={zhou, 宋江} isBlank"` 15 Age int `match:"value={12, 13}"` 16 } 17 18 func TestMkBase1(t *testing.T) { 19 var value MikilinBaseEntity 20 var result bool 21 var err string 22 23 //测试 正常情况 24 value = MikilinBaseEntity{Age: 12} 25 result, err = validate.Check(value) 26 TrueErr(t, result, err) 27 28 // 测试 正常情况 29 value = MikilinBaseEntity{Age: 13, Name: "zhou"} 30 result, err = validate.Check(value) 31 TrueErr(t, result, err) 32 33 // 测试 异常情况 34 value = MikilinBaseEntity{Age: 13, Name: "陈真"} 35 result, err = validate.Check(value) 36 Equal(t, err, "[\"属性 Name 的值 陈真 不在只可用列表 [zhou 宋江] 中\",\"属性 Name 的值为非空字符\"]", result, false) 37 } 38 39 func TestMkBase2(t *testing.T) { 40 var value MikilinBaseEntity2 41 var result bool 42 var err string 43 44 //测试 正常情况 45 value = MikilinBaseEntity2{Age: 12} 46 result, err = validate.Check(value) 47 TrueErr(t, result, err) 48 49 // 测试 正常情况 50 value = MikilinBaseEntity2{Age: 13, Name: "zhou"} 51 result, err = validate.Check(value) 52 TrueErr(t, result, err) 53 54 // 测试 异常情况 55 value = MikilinBaseEntity2{Age: 13, Name: "陈真"} 56 result, err = validate.Check(value) 57 Equal(t, "[\"属性 Name 的值 陈真 不在只可用列表 [zhou 宋江] 中\",\"属性 Name 的值为非空字符\"]", err, result, false) 58 } 59 60 // 压测进行基准测试 61 func BenchmarkMkBase3(b *testing.B) { 62 for i := 0; i < b.N; i++ { 63 validate.Check(MikilinBaseEntity{Age: 12}) 64 } 65 }