github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/validate/test/value_test.go (about) 1 package main 2 3 import ( 4 "github.com/isyscore/isc-gobase/validate" 5 "testing" 6 ) 7 8 type ValueBaseEntity struct { 9 Name string `match:"value={zhou, 宋江}"` 10 Age int `match:"value={12, 13}"` 11 } 12 13 type ValueBaseEntityOne struct { 14 Name string `match:"value=zhou"` 15 Age int `match:"value=12"` 16 } 17 18 type ValueBasePtrEntity struct { 19 Name *string `match:"value={zhou, 宋江}"` 20 Age *int `match:"value={12, 13}"` 21 } 22 23 type ValueInnerEntity struct { 24 InnerName string `match:"value={inner_zhou, inner_宋江}"` 25 InnerAge int `match:"value={2212, 2213}"` 26 } 27 28 type ValueStructEntity struct { 29 Name string `match:"value={zhou, 宋江}"` 30 Age int `match:"value={12, 13}"` 31 32 Inner ValueInnerEntity `match:"check"` 33 } 34 35 type ValueStructPtrEntity struct { 36 Name string `match:"value={zhou, 宋江}"` 37 Age int `match:"value={12, 13}"` 38 39 Inner *ValueInnerEntity `match:"check"` 40 } 41 42 type ValueMapValueEntity struct { 43 Name string `match:"value={zhou, 宋江}"` 44 Age int `match:"value={12, 13}"` 45 46 InnerMap map[string]ValueInnerEntity `match:"check"` 47 } 48 49 type ValueMapKeyEntity struct { 50 Name string `match:"value={zhou, 宋江}"` 51 Age int `match:"value={12, 13}"` 52 53 InnerMap map[ValueInnerEntity]string `match:"check"` 54 } 55 56 type ValueMapValuePtrEntity struct { 57 Name string `match:"value={zhou, 宋江}"` 58 Age int `match:"value={12, 13}"` 59 60 InnerMap map[string]*ValueInnerEntity `match:"check"` 61 } 62 63 type ValueArrayEntity struct { 64 Inner [3]ValueInnerEntity `match:"check"` 65 } 66 67 type ValueArrayPtrEntity struct { 68 Inner [3]*ValueInnerEntity `match:"check"` 69 } 70 71 type ValueSliceEntity struct { 72 Inner []ValueInnerEntity `match:"check"` 73 } 74 75 type ValueSlicePtrEntity struct { 76 Inner []*ValueInnerEntity `match:"check"` 77 } 78 79 // 测试基本类型:一个值的情况 80 func TestValueBase2(t *testing.T) { 81 var value ValueBaseEntityOne 82 var result bool 83 var err string 84 85 //测试 正常情况 86 value = ValueBaseEntityOne{Age: 12} 87 result, err = validate.Check(value, "age") 88 TrueErr(t, result, err) 89 90 // 测试 正常情况 91 value = ValueBaseEntityOne{Age: 13} 92 result, err = validate.Check(value, "age") 93 Equal(t, err, "属性 Age 的值 13 不在只可用列表 [12] 中", result, false) 94 95 //测试 正常情况 96 value = ValueBaseEntityOne{Name: "zhou"} 97 result, err = validate.Check(value, "name") 98 TrueErr(t, result, err) 99 100 // 测试 正常情况 101 value = ValueBaseEntityOne{Name: "宋江"} 102 result, err = validate.Check(value, "name") 103 Equal(t, err, "属性 Name 的值 宋江 不在只可用列表 [zhou] 中", result, false) 104 } 105 106 // 测试基本类型:多个值的情况 107 func TestValueBase(t *testing.T) { 108 var value ValueBaseEntity 109 var result bool 110 var err string 111 112 //测试 正常情况 113 value = ValueBaseEntity{Age: 12} 114 result, err = validate.Check(value, "age") 115 TrueErr(t, result, err) 116 117 // 测试 正常情况 118 value = ValueBaseEntity{Age: 13} 119 result, err = validate.Check(value, "age") 120 TrueErr(t, result, err) 121 122 // 测试 异常情况 123 value = ValueBaseEntity{Age: 14} 124 result, err = validate.Check(value, "age") 125 Equal(t, err, "属性 Age 的值 14 不在只可用列表 [12 13] 中", false, result) 126 127 // 测试 正常情况 128 value = ValueBaseEntity{Name: "zhou"} 129 result, err = validate.Check(value, "name") 130 TrueErr(t, result, err) 131 132 // 测试 正常情况 133 value = ValueBaseEntity{Name: "宋江"} 134 result, err = validate.Check(value, "name") 135 TrueErr(t, result, err) 136 137 // 测试 异常情况 138 value = ValueBaseEntity{Name: "陈真"} 139 result, err = validate.Check(value, "name") 140 Equal(t, err, "属性 Name 的值 陈真 不在只可用列表 [zhou 宋江] 中", false, result) 141 } 142 143 // 测试基本类型:指针类型 144 func TestValueBasePtr(t *testing.T) { 145 var value *ValueBasePtrEntity 146 var result bool 147 var err string 148 var age int 149 var name string 150 151 //测试 正常情况 152 value = &ValueBasePtrEntity{} 153 age = 12 154 value.Age = &age 155 result, err = validate.Check(value, "age") 156 TrueErr(t, result, err) 157 158 //测试 正常情况 159 value = &ValueBasePtrEntity{} 160 age = 13 161 value.Age = &age 162 result, err = validate.Check(value, "age") 163 TrueErr(t, result, err) 164 165 // 测试 异常情况 166 value = &ValueBasePtrEntity{} 167 age = 14 168 value.Age = &age 169 result, err = validate.Check(value, "age") 170 Equal(t, err, "属性 Age 的值 14 不在只可用列表 [12 13] 中", result, false) 171 172 // 测试 正常情况 173 value = &ValueBasePtrEntity{} 174 name = "zhou" 175 value.Name = &name 176 result, err = validate.Check(value, "name") 177 TrueErr(t, result, err) 178 179 // 测试 正常情况 180 value = &ValueBasePtrEntity{} 181 name = "宋江" 182 value.Name = &name 183 result, err = validate.Check(value, "name") 184 TrueErr(t, result, err) 185 186 // 测试 异常情况 187 value = &ValueBasePtrEntity{} 188 name = "陈真" 189 value.Name = &name 190 result, err = validate.Check(value, "name") 191 Equal(t, err, "属性 Name 的值 陈真 不在只可用列表 [zhou 宋江] 中", result, false) 192 } 193 194 // 测试Struct类型 195 func TestValueStruct(t *testing.T) { 196 var value ValueStructEntity 197 var result bool 198 var err string 199 //测试 正常情况 200 value = ValueStructEntity{Inner: ValueInnerEntity{ 201 InnerAge: 2212, 202 InnerName: "inner_宋江", 203 }} 204 result, err = validate.Check(value, "inner") 205 TrueErr(t, result, err) 206 207 //测试 正常情况 208 value = ValueStructEntity{Inner: ValueInnerEntity{ 209 InnerAge: 2213, 210 InnerName: "inner_宋江", 211 }} 212 result, err = validate.Check(value, "inner") 213 TrueErr(t, result, err) 214 215 //测试 异常情况 216 value = ValueStructEntity{Inner: ValueInnerEntity{ 217 InnerAge: 2214, 218 InnerName: "inner_宋江", 219 }} 220 result, err = validate.Check(value, "inner") 221 Equal(t, err, "属性 InnerAge 的值 2214 不在只可用列表 [2212 2213] 中", false, result) 222 } 223 224 // 测试Struct类型:指针类型 225 func TestValueStructPtr(t *testing.T) { 226 var value ValueStructPtrEntity 227 var result bool 228 var err string 229 //// 测试 正常情况 230 //value = ValueStructPtrEntity{Inner: &ValueInnerEntity{ 231 // InnerAge: 2212, 232 // InnerName: "inner_宋江", 233 //}} 234 //result, err = validate.Check(value, "inner") 235 //TrueErr(t, result, err) 236 // 237 ////测试 正常情况 238 //value = ValueStructPtrEntity{Inner: &ValueInnerEntity{ 239 // InnerAge: 2213, 240 // InnerName: "inner_宋江", 241 //}} 242 //result, err = validate.Check(value, "inner") 243 //TrueErr(t, result, err) 244 // 245 //// 测试 核查其他情况 246 //value = ValueStructPtrEntity{Age: 12} 247 //result, err = validate.Check(value, "age") 248 //TrueErr(t, result, err) 249 // 250 //// 测试 核查其他情况 251 //value = ValueStructPtrEntity{Age: 12, Inner: &ValueInnerEntity{ 252 // InnerAge: 2213, 253 // InnerName: "inner_宋江", 254 //}} 255 //result, err = validate.Check(value, "age", "inner") 256 //TrueErr(t, result, err) 257 // 258 //// 测试 核查其他情况 259 //value = ValueStructPtrEntity{Age: 14, Inner: &ValueInnerEntity{ 260 // InnerAge: 2213, 261 // InnerName: "inner_宋江", 262 //}} 263 //result, err = validate.Check(value, "age", "inner") 264 //Equal(t, false, result, "属性 Age 的值 14 不在只可用列表 [12 13] 中", err) 265 266 //测试 异常情况 267 value = ValueStructPtrEntity{Inner: &ValueInnerEntity{ 268 InnerAge: 2214, 269 InnerName: "inner_宋江", 270 }} 271 result, err = validate.Check(value, "inner") 272 Equal(t, false, result, "属性 InnerAge 的值 2214 不在只可用列表 [2212 2213] 中", err) 273 } 274 275 // 测试Map:value的验证 276 func TestValueMapValue(t *testing.T) { 277 var value ValueMapValueEntity 278 var result bool 279 var err string 280 var innerMap map[string]ValueInnerEntity 281 282 // 测试 正常情况 283 value = ValueMapValueEntity{Age: 12, Name: "宋江"} 284 result, err = validate.Check(value) 285 TrueErr(t, result, err) 286 287 // 测试 正常情况 288 value = ValueMapValueEntity{} 289 innerMap = make(map[string]ValueInnerEntity) 290 innerMap["a"] = ValueInnerEntity{InnerAge: 2212, InnerName: "inner_zhou"} 291 value.InnerMap = innerMap 292 result, err = validate.Check(value, "InnerMap") 293 TrueErr(t, result, err) 294 295 // 测试 正常情况 296 value = ValueMapValueEntity{} 297 innerMap = make(map[string]ValueInnerEntity) 298 innerMap["a"] = ValueInnerEntity{InnerAge: 2213, InnerName: "inner_宋江"} 299 value.InnerMap = innerMap 300 result, err = validate.Check(value, "InnerMap") 301 TrueErr(t, result, err) 302 303 // 测试 异常情况 304 value = ValueMapValueEntity{} 305 innerMap = make(map[string]ValueInnerEntity) 306 innerMap["a"] = ValueInnerEntity{InnerAge: 2213} 307 value.InnerMap = innerMap 308 result, err = validate.Check(value, "InnerMap") 309 Equal(t, false, result, "属性 InnerName 的值 不在只可用列表 [inner_zhou inner_宋江] 中", err) 310 311 // 测试 异常情况 312 value = ValueMapValueEntity{} 313 innerMap = make(map[string]ValueInnerEntity) 314 innerMap["a"] = ValueInnerEntity{InnerAge: 2213, InnerName: "inner_陈"} 315 value.InnerMap = innerMap 316 result, err = validate.Check(value, "InnerMap") 317 Equal(t, false, result, "属性 InnerName 的值 inner_陈 不在只可用列表 [inner_zhou inner_宋江] 中", err) 318 } 319 320 // 测试Map:key的验证 321 func TestValueMapKey(t *testing.T) { 322 var value ValueMapKeyEntity 323 var result bool 324 var err string 325 var innerMap map[ValueInnerEntity]string 326 327 // 测试 正常情况 328 value = ValueMapKeyEntity{} 329 innerMap = make(map[ValueInnerEntity]string) 330 innerMap[ValueInnerEntity{InnerAge: 2212, InnerName: "inner_zhou"}] = "a" 331 value.InnerMap = innerMap 332 result, err = validate.Check(value, "InnerMap") 333 TrueErr(t, result, err) 334 335 // 测试 正常情况 336 value = ValueMapKeyEntity{} 337 innerMap = make(map[ValueInnerEntity]string) 338 innerMap[ValueInnerEntity{InnerAge: 2213, InnerName: "inner_zhou"}] = "a" 339 value.InnerMap = innerMap 340 result, err = validate.Check(value, "InnerMap") 341 TrueErr(t, result, err) 342 343 // 测试 异常情况 344 value = ValueMapKeyEntity{} 345 innerMap = make(map[ValueInnerEntity]string) 346 innerMap[ValueInnerEntity{InnerAge: 2214, InnerName: "inner_zhou"}] = "a" 347 value.InnerMap = innerMap 348 result, err = validate.Check(value, "InnerMap") 349 Equal(t, false, result, "属性 InnerAge 的值 2214 不在只可用列表 [2212 2213] 中", err) 350 } 351 352 // 测试Map:value的指针验证 353 func TestValueMapValuePtr(t *testing.T) { 354 var value ValueMapValuePtrEntity 355 var result bool 356 var err string 357 var innerMap map[string]*ValueInnerEntity 358 359 // 测试 正常情况 360 value = ValueMapValuePtrEntity{} 361 innerMap = make(map[string]*ValueInnerEntity) 362 innerMap["a"] = &ValueInnerEntity{InnerAge: 2212, InnerName: "inner_zhou"} 363 value.InnerMap = innerMap 364 result, err = validate.Check(value, "InnerMap") 365 TrueErr(t, result, err) 366 367 // 测试 正常情况 368 value = ValueMapValuePtrEntity{} 369 innerMap = make(map[string]*ValueInnerEntity) 370 innerMap["a"] = &ValueInnerEntity{InnerAge: 2213, InnerName: "inner_宋江"} 371 value.InnerMap = innerMap 372 result, err = validate.Check(value, "InnerMap") 373 TrueErr(t, result, err) 374 375 // 测试 异常情况 376 value = ValueMapValuePtrEntity{} 377 innerMap = make(map[string]*ValueInnerEntity) 378 innerMap["a"] = &ValueInnerEntity{InnerAge: 2213} 379 value.InnerMap = innerMap 380 result, err = validate.Check(value, "InnerMap") 381 Equal(t, false, result, "属性 InnerName 的值 不在只可用列表 [inner_zhou inner_宋江] 中", err) 382 383 // 测试 异常情况 384 value = ValueMapValuePtrEntity{} 385 innerMap = make(map[string]*ValueInnerEntity) 386 innerMap["a"] = &ValueInnerEntity{InnerAge: 2213, InnerName: "inner_陈"} 387 value.InnerMap = innerMap 388 result, err = validate.Check(value, "InnerMap") 389 Equal(t, false, result, "属性 InnerName 的值 inner_陈 不在只可用列表 [inner_zhou inner_宋江] 中", err) 390 } 391 392 // 测试Array 393 func TestValueArray(t *testing.T) { 394 var value ValueArrayEntity 395 var result bool 396 var err string 397 innerArray := [3]ValueInnerEntity{} 398 399 // 正常 400 value = ValueArrayEntity{} 401 innerArray[0] = ValueInnerEntity{InnerAge: 2212, InnerName: "inner_zhou"} 402 innerArray[1] = ValueInnerEntity{InnerAge: 2213, InnerName: "inner_zhou"} 403 innerArray[2] = ValueInnerEntity{InnerAge: 2212, InnerName: "inner_宋江"} 404 value.Inner = innerArray 405 406 result, err = validate.Check(value, "inner") 407 TrueErr(t, result, err) 408 409 // 异常 410 value = ValueArrayEntity{} 411 innerArray[0] = ValueInnerEntity{InnerAge: 2212, InnerName: "inner_zhou"} 412 innerArray[1] = ValueInnerEntity{InnerAge: 2213, InnerName: "inner_zhou"} 413 innerArray[2] = ValueInnerEntity{InnerAge: 2214, InnerName: "inner_宋江"} 414 value.Inner = innerArray 415 result, err = validate.Check(value, "inner") 416 Equal(t, err, "属性 InnerAge 的值 2214 不在只可用列表 [2212 2213] 中", false, result) 417 } 418 419 // 测试Array:指针类型 420 func TestValueArrayPtr(t *testing.T) { 421 var value ValueArrayPtrEntity 422 var result bool 423 var err string 424 innerArray := [3]*ValueInnerEntity{} 425 426 // 正常 427 value = ValueArrayPtrEntity{} 428 innerArray[0] = &ValueInnerEntity{InnerAge: 2212, InnerName: "inner_zhou"} 429 innerArray[1] = &ValueInnerEntity{InnerAge: 2213, InnerName: "inner_zhou"} 430 innerArray[2] = &ValueInnerEntity{InnerAge: 2212, InnerName: "inner_宋江"} 431 value.Inner = innerArray 432 433 result, err = validate.Check(value, "inner") 434 TrueErr(t, result, err) 435 436 // 异常 437 value = ValueArrayPtrEntity{} 438 innerArray[0] = &ValueInnerEntity{InnerAge: 2212, InnerName: "inner_zhou"} 439 innerArray[1] = &ValueInnerEntity{InnerAge: 2213, InnerName: "inner_zhou"} 440 innerArray[2] = &ValueInnerEntity{InnerAge: 2214, InnerName: "inner_宋江"} 441 value.Inner = innerArray 442 result, err = validate.Check(value, "inner") 443 Equal(t, err, "属性 InnerAge 的值 2214 不在只可用列表 [2212 2213] 中", false, result) 444 } 445 446 // 测试 Slice 447 func TestValueSlice(t *testing.T) { 448 var value ValueSliceEntity 449 var result bool 450 var err string 451 innerSlice := []ValueInnerEntity{} 452 453 // 正常 454 value = ValueSliceEntity{} 455 innerSlice = append(innerSlice, ValueInnerEntity{InnerAge: 2212, InnerName: "inner_zhou"}) 456 innerSlice = append(innerSlice, ValueInnerEntity{InnerAge: 2213, InnerName: "inner_宋江"}) 457 innerSlice = append(innerSlice, ValueInnerEntity{InnerAge: 2212, InnerName: "inner_宋江"}) 458 value.Inner = innerSlice 459 460 result, err = validate.Check(value, "inner") 461 TrueErr(t, result, err) 462 463 // 异常 464 value = ValueSliceEntity{} 465 innerSlice = append(innerSlice, ValueInnerEntity{InnerAge: 2212, InnerName: "inner_zhou"}) 466 innerSlice = append(innerSlice, ValueInnerEntity{InnerAge: 2213, InnerName: "inner_zhou"}) 467 innerSlice = append(innerSlice, ValueInnerEntity{InnerAge: 2214, InnerName: "inner_宋江"}) 468 value.Inner = innerSlice 469 470 result, err = validate.Check(value, "inner") 471 Equal(t, err, "属性 InnerAge 的值 2214 不在只可用列表 [2212 2213] 中", false, result) 472 } 473 474 // 测试 Slice:指针类型 475 func TestValueSlicePtr(t *testing.T) { 476 var value ValueSlicePtrEntity 477 var result bool 478 var err string 479 innerSlice := []*ValueInnerEntity{} 480 481 // 正常 482 value = ValueSlicePtrEntity{} 483 innerSlice = append(innerSlice, &ValueInnerEntity{InnerAge: 2212, InnerName: "inner_zhou"}) 484 innerSlice = append(innerSlice, &ValueInnerEntity{InnerAge: 2213, InnerName: "inner_zhou"}) 485 innerSlice = append(innerSlice, &ValueInnerEntity{InnerAge: 2212, InnerName: "inner_宋江"}) 486 value.Inner = innerSlice 487 488 result, err = validate.Check(value, "inner") 489 TrueErr(t, result, err) 490 491 // 异常 492 value = ValueSlicePtrEntity{} 493 innerSlice = append(innerSlice, &ValueInnerEntity{InnerAge: 2212, InnerName: "inner_zhou"}) 494 innerSlice = append(innerSlice, &ValueInnerEntity{InnerAge: 2213, InnerName: "inner_zhou"}) 495 innerSlice = append(innerSlice, &ValueInnerEntity{InnerAge: 2214, InnerName: "inner_宋江"}) 496 value.Inner = innerSlice 497 result, err = validate.Check(value, "inner") 498 Equal(t, err, "属性 InnerAge 的值 2214 不在只可用列表 [2212 2213] 中", false, result) 499 } 500 501 // value的基准测试 502 func Benchmark_Value(b *testing.B) { 503 var value ValueBaseEntityOne 504 for i := 0; i < b.N; i++ { 505 value = ValueBaseEntityOne{Age: 12} 506 validate.Check(value, "age") 507 } 508 }