github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/validate/test/customize_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/isyscore/isc-gobase/isc"
     7  	"github.com/isyscore/isc-gobase/validate"
     8  	"github.com/isyscore/isc-gobase/validate/test/fun"
     9  	"math"
    10  	"math/rand"
    11  	"testing"
    12  	t0 "time"
    13  )
    14  
    15  func TestCustomize1(t *testing.T) {
    16  	var value fun.CustomizeEntity1
    17  	var result bool
    18  	var err string
    19  
    20  	// 测试 正常情况
    21  	value = fun.CustomizeEntity1{Name: "zhou"}
    22  	result, _ = validate.Check(value, "name")
    23  	True(t, result)
    24  
    25  	// 测试 正常情况
    26  	value = fun.CustomizeEntity1{Name: "宋江"}
    27  	result, _ = validate.Check(value, "name")
    28  	True(t, result)
    29  
    30  	// 测试 异常情况
    31  	value = fun.CustomizeEntity1{Name: "陈真"}
    32  	result, err = validate.Check(value)
    33  	Equal(t, err, "属性 Name 的值 陈真 没命中只允许条件回调 [judge1Name] ", result, false)
    34  }
    35  
    36  func TestCustomize2(t *testing.T) {
    37  	var value fun.CustomizeEntity2
    38  	var result bool
    39  	var err string
    40  
    41  	// 测试 正常情况
    42  	value = fun.CustomizeEntity2{Name: "zhou"}
    43  	result, err = validate.Check(value, "name")
    44  	True(t, result)
    45  
    46  	// 测试 正常情况
    47  	value = fun.CustomizeEntity2{Name: "宋江"}
    48  	result, err = validate.Check(value, "name")
    49  	True(t, result)
    50  
    51  	// 测试 异常情况
    52  	value = fun.CustomizeEntity2{Name: "陈真"}
    53  	result, err = validate.Check(value)
    54  	Equal(t, err, "没有命中可用的值'zhou'和'宋江'", result, false)
    55  }
    56  
    57  func TestCustomize3(t *testing.T) {
    58  	var value fun.CustomizeEntity3
    59  	var result bool
    60  	var err string
    61  
    62  	// 测试 正常情况
    63  	value = fun.CustomizeEntity3{Name: "zhou", Age: 20}
    64  	result, err = validate.Check(value, "name")
    65  	True(t, result)
    66  
    67  	// 测试 正常情况
    68  	value = fun.CustomizeEntity3{Name: "宋江", Age: 20}
    69  	result, _ = validate.Check(value, "name")
    70  	True(t, result)
    71  
    72  	// 测试 异常情况
    73  	value = fun.CustomizeEntity3{Name: "陈真"}
    74  	result, err = validate.Check(value)
    75  	Equal(t, err, "没有命中可用的值'zhou'和'宋江'", result, false)
    76  
    77  	// 测试 正常情况
    78  	value = fun.CustomizeEntity3{Name: "zhou", Age: 13}
    79  	result, _ = validate.Check(value)
    80  	True(t, result)
    81  
    82  	// 测试 异常情况
    83  	value = fun.CustomizeEntity3{Name: "zhou", Age: 10}
    84  	result, err = validate.Check(value)
    85  	Equal(t, err, "用户[zhou]没有满足年龄age > 12,当前年龄为:10", result, false)
    86  }
    87  
    88  func TestCustomize4(t *testing.T) {
    89  	var value fun.CustomizeEntity4
    90  	var result bool
    91  	var err string
    92  
    93  	// 测试 正常情况
    94  	value = fun.CustomizeEntity4{Name: "zhou", Age: 20}
    95  	result, err = validate.Check(value, "name")
    96  	True(t, result)
    97  
    98  	// 测试 正常情况
    99  	value = fun.CustomizeEntity4{Name: "宋江", Age: 20}
   100  	result, _ = validate.Check(value, "name")
   101  	True(t, result)
   102  
   103  	// 测试 异常情况
   104  	value = fun.CustomizeEntity4{Name: "陈真"}
   105  	result, err = validate.Check(value)
   106  	Equal(t, err, "没有命中可用的值'zhou'和'宋江'", result, false)
   107  
   108  	// 测试 正常情况
   109  	value = fun.CustomizeEntity4{Name: "zhou", Age: 13}
   110  	result, _ = validate.Check(value)
   111  	True(t, result)
   112  
   113  	// 测试 异常情况
   114  	value = fun.CustomizeEntity4{Name: "zhou", Age: 10}
   115  	result, err = validate.Check(value)
   116  	Equal(t, err, "用户[zhou]没有满足年龄age > 12,当前年龄为:10", result, false)
   117  }
   118  
   119  func TestCustomize5(t *testing.T) {
   120  	var value fun.CustomizeEntity4
   121  	var result bool
   122  	var err string
   123  
   124  	// 测试 正常情况
   125  	value = fun.CustomizeEntity4{Name: "zhou", Age: 20}
   126  	result, err = validate.Check(value, "name")
   127  	True(t, result)
   128  
   129  	// 测试 正常情况
   130  	value = fun.CustomizeEntity4{Name: "宋江", Age: 20}
   131  	result, _ = validate.Check(value, "name")
   132  	True(t, result)
   133  
   134  	// 测试 异常情况
   135  	value = fun.CustomizeEntity4{Name: "陈真"}
   136  	result, err = validate.Check(value)
   137  	Equal(t, err, "没有命中可用的值'zhou'和'宋江'", result, false)
   138  
   139  	// 测试 正常情况
   140  	value = fun.CustomizeEntity4{Name: "zhou", Age: 13}
   141  	result, _ = validate.Check(value)
   142  	True(t, result)
   143  
   144  	// 测试 异常情况
   145  	value = fun.CustomizeEntity4{Name: "zhou", Age: 10}
   146  	result, err = validate.Check(value)
   147  	Equal(t, err, "用户[zhou]没有满足年龄age > 12,当前年龄为:10", result, false)
   148  }
   149  
   150  func TestCustomize5_1(t *testing.T) {
   151  	var value fun.CustomizeEntity5
   152  	var result bool
   153  
   154  	// 测试 正常情况
   155  	value = fun.CustomizeEntity5{Name: "zhou", Age: 20}
   156  	result, _ = validate.Check(value, "name")
   157  	True(t, result)
   158  
   159  	// 测试 正常情况
   160  	value = fun.CustomizeEntity5{Name: "宋江", Age: 20}
   161  	result, _ = validate.Check(value, "name")
   162  	True(t, result)
   163  }
   164  
   165  func TestCustomize6(t *testing.T) {
   166  	var value fun.CustomizeEntity6
   167  	var result bool
   168  	var pMap map[string]interface{}
   169  
   170  	// 测试 正常情况
   171  	value = fun.CustomizeEntity6{}
   172  	pMap = map[string]interface{}{
   173  		"name": "zhou",
   174  		"age":  20,
   175  	}
   176  	result, _ = validate.CheckWithParameter(pMap, value, "name1")
   177  	True(t, result)
   178  
   179  	// 测试 正常情况
   180  	value = fun.CustomizeEntity6{Name2: "zhou"}
   181  	pMap = map[string]interface{}{
   182  		"age": 20,
   183  	}
   184  	result, _ = validate.CheckWithParameter(pMap, value, "name2")
   185  	True(t, result)
   186  
   187  	// 测试 正常情况
   188  	value = fun.CustomizeEntity6{Name3: "zhou"}
   189  	pMap = map[string]interface{}{
   190  		"age": 20,
   191  	}
   192  	result, _ = validate.CheckWithParameter(pMap, value, "name3")
   193  	True(t, result)
   194  }
   195  
   196  func TestCustomize7(t *testing.T) {
   197  	var value fun.CustomizeEntity7
   198  	var result bool
   199  
   200  	// 测试 正常情况
   201  	value = fun.CustomizeEntity7{Name: nil}
   202  	result, _ = validate.Check(value, "name")
   203  	False(t, result)
   204  
   205  	// 测试 正常情况
   206  	//name := "df"
   207  	//value = fun.CustomizeEntity7{Name: &name}
   208  	//result, _ = validate.Check(value, "name")
   209  	//True(t, result)
   210  }
   211  
   212  func TestCustomize7_1(t *testing.T) {
   213  	var value fun.CustomizeEntity7
   214  	var value1 fun.CustomizeEntity7
   215  	var result bool
   216  
   217  	// 测试 正常情况
   218  	value = fun.CustomizeEntity7{Flag: nil}
   219  	result, msg := validate.Check(value, "flag")
   220  	FalseMsg(t, result, msg)
   221  
   222  	// 测试 正常情况
   223  	flag := true
   224  	value = fun.CustomizeEntity7{Flag: &flag}
   225  	result, _ = validate.Check(value, "flag")
   226  	True(t, result)
   227  
   228  	str := "{\"name\":\"xxx\", \"age\":12}"
   229  	_ = json.Unmarshal([]byte(str), &value1)
   230  	result, msg = validate.Check(value1, "flag")
   231  	FalseMsg(t, result, msg)
   232  
   233  	value = fun.CustomizeEntity7{Flag2: nil}
   234  	result, msg = validate.Check(value, "flag2")
   235  	FalseMsg(t, result, msg)
   236  
   237  	flag = true
   238  	value = fun.CustomizeEntity7{Flag2: &flag}
   239  	result, _ = validate.Check(value, "flag2")
   240  	True(t, result)
   241  }
   242  
   243  func TestFun(t *testing.T) {
   244  	rand.Seed(t0.Now().UnixNano())
   245  	////随机生成100以内的正整数
   246  	//
   247  	//a := 1
   248  	//b := 3
   249  	//
   250  	//// [-1.0, 1.0)
   251  	//fmt.Println((isc.ToFloat64(a))/(isc.ToFloat64(b)*1.0))
   252  	//
   253  	//Solos := []*Solo{}
   254  	//
   255  	//Solos = append(Solos, &Solo{score: 12.0})
   256  	//Solos = append(Solos, &Solo{score: 1.0})
   257  	//Solos = append(Solos, &Solo{score: 132.2})
   258  	//Solos = append(Solos, &Solo{score: 54.2})
   259  	//Solos = append(Solos, &Solo{score: 32.2})
   260  	//
   261  	//group := Group{solos: Solos}
   262  	//
   263  	//sort.Sort(group)
   264  	//
   265  	//for _, solo := range group.solos {
   266  	//	fmt.Println(solo.score)
   267  	//}
   268  	//
   269  	//rand.Shuffle(len(group.solos), func(i, j int) {
   270  	//	group.solos[i], group.solos[j] = group.solos[j], group.solos[i]
   271  	//})
   272  	//
   273  	//fmt.Println("======")
   274  	//for _, solo := range group.solos {
   275  	//	fmt.Println(solo.score)
   276  	//}
   277  	//
   278  	//fmt.Println(rand.Intn(2))
   279  	//fmt.Println(rand.Intn(2))
   280  	//fmt.Println(rand.Intn(2))
   281  	//fmt.Println(rand.Intn(2))
   282  	//fmt.Println(rand.Intn(2))
   283  	//fmt.Println(rand.Intn(2))
   284  	//fmt.Println(rand.Intn(2))
   285  	//fmt.Println(rand.Intn(2))
   286  	//fmt.Println(rand.Intn(2))
   287  	//fmt.Println(rand.Intn(2))
   288  	//fmt.Println(rand.Intn(2))
   289  	//// 0,1
   290  	//fmt.Println(rand.Intn(2))
   291  
   292  
   293  	//datas := []int{1,2, 3, 4, 5}
   294  	//fmt.Println(datas[0:2])
   295  	//fmt.Println(datas[2:4])
   296  	//fmt.Println(datas[:len(datas)])
   297  
   298  	ratio := 0.3
   299  	num := 12
   300  
   301  	fmt.Println(isc.ToInt(math.Ceil(isc.ToFloat64(num) * ratio)))
   302  }
   303  
   304  // 个体
   305  type Solo struct {
   306  	// 评分
   307  	score float64
   308  }
   309  
   310  // 种群
   311  type Group struct {
   312  	// 参数
   313  	solos []*Solo
   314  }
   315  
   316  func (group Group) Len() int {
   317  	return len(group.solos)
   318  }
   319  
   320  func (group Group) Less(i, j int) bool {
   321  	return group.solos[i].score > group.solos[j].score
   322  }
   323  
   324  func (group Group) Swap(i, j int) {
   325  	group.solos[i], group.solos[j] = group.solos[j], group.solos[i]
   326  }