github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/validate/test/range_test.go (about) 1 package main 2 3 import ( 4 "github.com/isyscore/isc-gobase/validate" 5 "testing" 6 "time" 7 ) 8 9 var ( 10 LocationCST, err = time.LoadLocation("Asia/Shanghai") 11 ) 12 13 // 整数类型1 14 type RangeIntEntity1 struct { 15 Name string 16 Age int `match:"range=[1, 2]"` 17 } 18 19 // 整数类型2 20 type RangeIntEntity2 struct { 21 Name string 22 Age int `match:"range=[3,]"` 23 } 24 25 // 整数类型3 26 type RangeIntEntity3 struct { 27 Name string 28 Age int `match:"range=[3,)"` 29 } 30 31 // 整数类型4,todo待校验 32 //type RangeIntEntity4 struct { 33 // Name string 34 // Age int `match:"range=[2,1]"` 35 //} 36 37 // 整数类型5 38 type RangeIntEntity5 struct { 39 Name string 40 Age int `match:"range=(2, 7]"` 41 } 42 43 // 整数类型6 44 type RangeIntEntity6 struct { 45 Name string 46 Age int `match:"range=(2, 7)"` 47 } 48 49 // 整数类型7 50 type RangeIntEntity7 struct { 51 Name string 52 Age int `match:"range=(,7)"` 53 } 54 55 // 中文的逗号测试 56 type RangeIntEntityChina struct { 57 Name string 58 Age int `match:"range=[1,10]"` 59 } 60 61 // 浮点数类型 62 type RangeFloatEntity struct { 63 Name string 64 Money float32 `match:"range=[10.37, 20.31]"` 65 } 66 67 // 字符类型 68 type RangeStringEntity struct { 69 Name string `match:"range=[2, 12]"` 70 Age int 71 } 72 73 // 分片类型 74 type RangeSliceEntity struct { 75 Name string 76 Age []int `match:"range=[2, 6]"` 77 } 78 79 // 时间类型1 80 type RangeTimeEntity1 struct { 81 CreateTime time.Time `match:"range=[2019-07-13 12:00:23.321, 2019-08-23 12:00:23.321]"` 82 } 83 84 // 时间类型2 85 type RangeTimeEntity2 struct { 86 CreateTime time.Time `match:"range=[2019-07-13 12:00:23.321, ]"` 87 } 88 89 // 时间类型3 90 type RangeTimeEntity3 struct { 91 CreateTime time.Time `match:"range=(, 2019-07-23 12:00:23.321]"` 92 } 93 94 // 时间类型4 95 type RangeTimeEntity4 struct { 96 CreateTime time.Time `match:"range=[2019-07-23 12:00:23.321, now)"` 97 } 98 99 // 时间类型4 100 type RangeTimeEntity5 struct { 101 CreateTime time.Time `match:"range=past"` 102 } 103 104 // 时间类型4 105 type RangeTimeEntity6 struct { 106 CreateTime time.Time `match:"range=future"` 107 } 108 109 // 时间计算:年 110 type RangeTimeCalEntity1 struct { 111 Name string 112 CreateTime time.Time `match:"range=(-1y, )"` 113 } 114 115 // 时间计算:月 116 type RangeTimeCalEntity2 struct { 117 Name string 118 CreateTime time.Time `match:"range=(-1M, )"` 119 } 120 121 // 时间计算:月日 122 type RangeTimeCalEntity2And1 struct { 123 Name string 124 CreateTime time.Time `match:"range=(-1M3d, )"` 125 } 126 127 // 时间计算:年日 128 type RangeTimeCalEntity2And2 struct { 129 Name string 130 CreateTime time.Time `match:"range=(-1y3d, )"` 131 } 132 133 // 时间计算:日 134 type RangeTimeCalEntity3 struct { 135 Name string 136 CreateTime time.Time `match:"range=(-3d, )"` 137 } 138 139 // 时间计算:时 140 type RangeTimeCalEntity4 struct { 141 Name string 142 CreateTime time.Time `match:"range=(-4h, )"` 143 } 144 145 // 时间计算:分 146 type RangeTimeCalEntity5 struct { 147 Name string 148 CreateTime time.Time `match:"range=(-12m, )"` 149 } 150 151 // 时间计算:秒 152 type RangeTimeCalEntity6 struct { 153 Name string 154 CreateTime time.Time `match:"range=(-120s, )"` 155 } 156 157 // 时间计算:正负号 158 type RangeTimeCalEntity7 struct { 159 Name string 160 CreateTime time.Time `match:"range=(2h, )"` 161 } 162 163 // 测试整数类型1 164 func TestRangeInt1(t *testing.T) { 165 var value RangeIntEntity1 166 var result bool 167 var err string 168 169 //测试 正常情况 170 value = RangeIntEntity1{Age: 1} 171 result, err = validate.Check(value, "age") 172 TrueErr(t, result, err) 173 174 //测试 正常情况 175 value = RangeIntEntity1{Age: 3} 176 result, err = validate.Check(value, "age") 177 Equal(t, err, "属性 [Age] 值 [3] 没有命中只允许的范围 [[1, 2]]", result, false) 178 } 179 180 // 测试整数类型2 181 func TestRangeInt2(t *testing.T) { 182 var value RangeIntEntity2 183 var result bool 184 var err string 185 186 //测试 正常情况 187 value = RangeIntEntity2{Age: 3} 188 result, err = validate.Check(value, "age") 189 TrueErr(t, result, err) 190 191 //测试 正常情况 192 value = RangeIntEntity2{Age: 5} 193 result, err = validate.Check(value, "age") 194 TrueErr(t, result, err) 195 196 //测试 异常情况 197 value = RangeIntEntity2{Age: 2} 198 result, err = validate.Check(value, "age") 199 Equal(t, err, "属性 [Age] 值 [2] 没有命中只允许的范围 [[3,]]", result, false) 200 } 201 202 // 测试整数类型3 203 func TestRangeInt3(t *testing.T) { 204 var value RangeIntEntity3 205 var result bool 206 var err string 207 208 //测试 正常情况 209 value = RangeIntEntity3{Age: 3} 210 result, err = validate.Check(value, "age") 211 TrueErr(t, result, err) 212 213 //测试 正常情况 214 value = RangeIntEntity3{Age: 5} 215 result, err = validate.Check(value, "age") 216 TrueErr(t, result, err) 217 218 //测试 异常情况 219 value = RangeIntEntity3{Age: 2} 220 result, err = validate.Check(value, "age") 221 Equal(t, err, "属性 [Age] 值 [2] 没有命中只允许的范围 [[3,)]", result, false) 222 } 223 224 // 测试整数类型4 225 func TestRangeInt4(t *testing.T) { 226 227 //测试 正常情况 228 //value = RangeIntEntity4{Age: 3} 229 //result, err = validate.Check(value, "age") 230 //assert.TrueErr(t, result, err) 231 } 232 233 // 测试整数类型5 234 func TestRangeInt5(t *testing.T) { 235 var value RangeIntEntity5 236 var result bool 237 var err string 238 239 //测试 正常情况 240 value = RangeIntEntity5{Age: 3} 241 result, err = validate.Check(value, "age") 242 TrueErr(t, result, err) 243 244 //测试 正常情况 245 value = RangeIntEntity5{Age: 7} 246 result, err = validate.Check(value, "age") 247 TrueErr(t, result, err) 248 249 //测试 异常情况 250 value = RangeIntEntity5{Age: 8} 251 result, err = validate.Check(value, "age") 252 Equal(t, err, "属性 [Age] 值 [8] 没有命中只允许的范围 [(2, 7]]", result, false) 253 254 //测试 异常情况 255 value = RangeIntEntity5{Age: 2} 256 result, err = validate.Check(value, "age") 257 Equal(t, err, "属性 [Age] 值 [2] 没有命中只允许的范围 [(2, 7]]", result, false) 258 } 259 260 // 测试整数类型6 261 func TestRangeInt6(t *testing.T) { 262 var value RangeIntEntity6 263 var result bool 264 var err string 265 266 //测试 正常情况 267 value = RangeIntEntity6{Age: 3} 268 result, err = validate.Check(value, "age") 269 TrueErr(t, result, err) 270 271 //测试 正常情况 272 value = RangeIntEntity6{Age: 7} 273 result, err = validate.Check(value, "age") 274 Equal(t, err, "属性 [Age] 值 [7] 没有命中只允许的范围 [(2, 7)]", result, false) 275 276 //测试 异常情况 277 value = RangeIntEntity6{Age: 8} 278 result, err = validate.Check(value, "age") 279 Equal(t, err, "属性 [Age] 值 [8] 没有命中只允许的范围 [(2, 7)]", result, false) 280 281 //测试 异常情况 282 value = RangeIntEntity6{Age: 2} 283 result, err = validate.Check(value, "age") 284 Equal(t, err, "属性 [Age] 值 [2] 没有命中只允许的范围 [(2, 7)]", result, false) 285 } 286 287 // 测试整数类型7 288 func TestRangeInt7(t *testing.T) { 289 var value RangeIntEntity7 290 var result bool 291 var err string 292 293 //测试 正常情况 294 value = RangeIntEntity7{Age: 3} 295 result, err = validate.Check(value, "age") 296 TrueErr(t, result, err) 297 298 //测试 正常情况 299 value = RangeIntEntity7{Age: -1} 300 result, err = validate.Check(value, "age") 301 TrueErr(t, result, err) 302 303 //测试 异常情况 304 value = RangeIntEntity7{Age: 7} 305 result, err = validate.Check(value, "age") 306 Equal(t, err, "属性 [Age] 值 [7] 没有命中只允许的范围 [(,7)]", result, false) 307 308 //测试 异常情况 309 value = RangeIntEntity7{Age: 8} 310 result, err = validate.Check(value, "age") 311 Equal(t, err, "属性 [Age] 值 [8] 没有命中只允许的范围 [(,7)]", result, false) 312 } 313 314 // 测试中文逗号表示 315 func TestRangeIntChinaComma(t *testing.T) { 316 var value RangeIntEntityChina 317 var result bool 318 var err string 319 320 //测试 正常情况 321 value = RangeIntEntityChina{Age: 3} 322 result, err = validate.Check(value, "age") 323 TrueErr(t, result, err) 324 325 //测试 正常情况 326 value = RangeIntEntityChina{Age: 5} 327 result, err = validate.Check(value, "age") 328 TrueErr(t, result, err) 329 330 //测试 异常情况 331 value = RangeIntEntityChina{Age: 0} 332 result, err = validate.Check(value, "age") 333 Equal(t, err, "属性 [Age] 值 [0] 没有命中只允许的范围 [[1,10]]", result, false) 334 335 //测试 异常情况 336 value = RangeIntEntityChina{Age: 12} 337 result, err = validate.Check(value, "age") 338 Equal(t, err, "属性 [Age] 值 [12] 没有命中只允许的范围 [[1,10]]", result, false) 339 } 340 341 // 测试浮点数类型1 342 func TestRangeFloat1(t *testing.T) { 343 var value RangeFloatEntity 344 var result bool 345 var err string 346 347 //测试 正常情况 348 value = RangeFloatEntity{Money: 10.37} 349 result, err = validate.Check(value, "money") 350 TrueErr(t, result, err) 351 352 //测试 正常情况 353 value = RangeFloatEntity{Money: 15.0} 354 result, err = validate.Check(value, "money") 355 TrueErr(t, result, err) 356 357 //测试 正常情况 358 value = RangeFloatEntity{Money: 20.31} 359 result, err = validate.Check(value, "money") 360 TrueErr(t, result, err) 361 362 //测试 异常情况 363 value = RangeFloatEntity{Money: 10.01} 364 result, err = validate.Check(value, "money") 365 Equal(t, err, "属性 [Money] 值 [10.01] 没有命中只允许的范围 [[10.37, 20.31]]", result, false) 366 367 //测试 异常情况 368 value = RangeFloatEntity{Money: 20.32} 369 result, err = validate.Check(value, "money") 370 Equal(t, err, "属性 [Money] 值 [20.32] 没有命中只允许的范围 [[10.37, 20.31]]", result, false) 371 } 372 373 // 测试字符类型1 374 func TestRangeString(t *testing.T) { 375 var value RangeStringEntity 376 var result bool 377 var err string 378 379 //测试 正常情况 380 value = RangeStringEntity{Name: "zh"} 381 result, err = validate.Check(value, "name") 382 TrueErr(t, result, err) 383 384 //测试 正常情况 385 value = RangeStringEntity{Name: "zhou"} 386 result, err = validate.Check(value, "name") 387 TrueErr(t, result, err) 388 389 //测试 正常情况 390 value = RangeStringEntity{Name: "zhou zhen yo"} 391 result, err = validate.Check(value, "name") 392 TrueErr(t, result, err) 393 394 //测试 异常情况 395 value = RangeStringEntity{Name: "zhou zhen yong"} 396 result, err = validate.Check(value, "name") 397 Equal(t, err, "属性 [Name] 值 [zhou zhen yong] 长度没有命中只允许的范围 [[2, 12]]", result, false) 398 399 //测试 异常情况 400 value = RangeStringEntity{Name: "z"} 401 result, err = validate.Check(value, "name") 402 Equal(t, err, "属性 [Name] 值 [z] 长度没有命中只允许的范围 [[2, 12]]", result, false) 403 404 //测试中文 异常情况 405 value = RangeStringEntity{Name: "中"} 406 result, err = validate.Check(value, "name") 407 Equal(t, err, "属性 [Name] 值 [中] 长度没有命中只允许的范围 [[2, 12]]", result, false) 408 409 //测试中文 正常情况 410 value = RangeStringEntity{Name: "中国"} 411 result, err = validate.Check(value, "name") 412 TrueErr(t, result, err) 413 } 414 415 // 测试分片类型1 416 func TestRangeSlice(t *testing.T) { 417 var value RangeSliceEntity 418 var result bool 419 var err string 420 421 //测试 正常情况 422 value = RangeSliceEntity{Age: []int{1, 2}} 423 result, err = validate.Check(value, "age") 424 TrueErr(t, result, err) 425 426 //测试 正常情况 427 value = RangeSliceEntity{Age: []int{1, 2, 3, 4, 5}} 428 result, err = validate.Check(value, "age") 429 TrueErr(t, result, err) 430 431 //测试 正常情况 432 value = RangeSliceEntity{Age: []int{1, 2, 3, 4, 5, 6}} 433 result, err = validate.Check(value, "age") 434 TrueErr(t, result, err) 435 436 //测试 异常情况 437 value = RangeSliceEntity{Age: []int{1, 2, 3, 4, 5, 6, 7}} 438 result, err = validate.Check(value, "age") 439 Equal(t, err, "属性 [Age] 值 [[1 2 3 4 5 6 7]] 数组长度没有命中只允许的范围 [[2, 6]]", result, false) 440 441 //测试 异常情况 442 value = RangeSliceEntity{Age: []int{1}} 443 result, err = validate.Check(value, "age") 444 Equal(t, err, "属性 [Age] 值 [[1]] 数组长度没有命中只允许的范围 [[2, 6]]", result, false) 445 } 446 447 // 测试时间类型1 448 func TestRangeTime1(t *testing.T) { 449 var value RangeTimeEntity1 450 var result bool 451 var err string 452 453 //测试 正常情况 454 value = RangeTimeEntity1{CreateTime: time.Date(2019, 7, 14, 12, 0, 23, 321, LocationCST)} 455 result, err = validate.Check(value, "createTime") 456 TrueErr(t, result, err) 457 458 //测试 异常情况 459 value = RangeTimeEntity1{CreateTime: time.Date(2019, 6, 14, 12, 0, 23, 321, LocationCST)} 460 result, err = validate.Check(value, "createTime") 461 Equal(t, err, "属性 [CreateTime] 值 [2019-06-14 12:00:23.000000321 +0800 CST] 时间没有命中只允许的时间段 [[2019-07-13 12:00:23.321, 2019-08-23 12:00:23.321]] 中", result, false) 462 463 //测试 异常情况 464 value = RangeTimeEntity1{CreateTime: time.Date(2019, 9, 14, 12, 0, 23, 321, LocationCST)} 465 result, err = validate.Check(value, "createTime") 466 Equal(t, err, "属性 [CreateTime] 值 [2019-09-14 12:00:23.000000321 +0800 CST] 时间没有命中只允许的时间段 [[2019-07-13 12:00:23.321, 2019-08-23 12:00:23.321]] 中", result, false) 467 } 468 469 // 测试时间类型2 470 func TestRangeTime2(t *testing.T) { 471 var value RangeTimeEntity2 472 var result bool 473 var err string 474 475 //测试 正常情况 476 value = RangeTimeEntity2{CreateTime: time.Date(2019, 7, 14, 12, 0, 23, 321, LocationCST)} 477 result, err = validate.Check(value, "createTime") 478 TrueErr(t, result, err) 479 480 //测试 正常情况 481 value = RangeTimeEntity2{CreateTime: time.Date(2019, 9, 14, 12, 0, 23, 321, LocationCST)} 482 result, err = validate.Check(value, "createTime") 483 TrueErr(t, result, err) 484 485 //测试 异常情况 486 value = RangeTimeEntity2{CreateTime: time.Date(2019, 6, 14, 12, 0, 23, 321, LocationCST)} 487 result, err = validate.Check(value, "createTime") 488 Equal(t, err, "属性 [CreateTime] 值 [2019-06-14 12:00:23.000000321 +0800 CST] 时间没有命中只允许的时间段 [[2019-07-13 12:00:23.321, ]] 中", result, false) 489 } 490 491 // 测试时间类型3 492 func TestRangeTime3(t *testing.T) { 493 var value RangeTimeEntity3 494 var result bool 495 var err string 496 497 //测试 正常情况 498 value = RangeTimeEntity3{CreateTime: time.Date(2019, 6, 14, 12, 0, 23, 321, LocationCST)} 499 result, err = validate.Check(value, "createTime") 500 TrueErr(t, result, err) 501 502 //测试 异常情况 503 value = RangeTimeEntity3{CreateTime: time.Date(2019, 7, 24, 12, 0, 23, 321, LocationCST)} 504 result, err = validate.Check(value, "createTime") 505 Equal(t, err, "属性 [CreateTime] 值 [2019-07-24 12:00:23.000000321 +0800 CST] 时间没有命中只允许的时间段 [(, 2019-07-23 12:00:23.321]] 中", result, false) 506 } 507 508 // 测试时间类型4 509 func TestRangeTime4(t *testing.T) { 510 var value RangeTimeEntity4 511 var result bool 512 var err string 513 514 //测试 正常情况 515 value = RangeTimeEntity4{CreateTime: time.Date(2019, 7, 24, 12, 0, 23, 321, LocationCST)} 516 result, err = validate.Check(value, "createTime") 517 TrueErr(t, result, err) 518 519 //测试 异常情况 520 value = RangeTimeEntity4{CreateTime: time.Date(2018, 7, 24, 12, 0, 23, 321, LocationCST)} 521 result, err = validate.Check(value, "createTime") 522 Equal(t, err, "属性 [CreateTime] 值 [2018-07-24 12:00:23.000000321 +0800 CST] 时间没有命中只允许的时间段 [[2019-07-23 12:00:23.321, now)] 中", result, false) 523 524 //测试 异常情况 525 value = RangeTimeEntity4{CreateTime: time.Date(9018, 7, 24, 12, 0, 23, 321, LocationCST)} 526 result, err = validate.Check(value, "createTime") 527 Equal(t, err, "属性 [CreateTime] 值 [9018-07-24 12:00:23.000000321 +0800 CST] 时间没有命中只允许的时间段 [[2019-07-23 12:00:23.321, now)] 中", result, false) 528 } 529 530 // 测试时间类型5 531 func TestRangeTime5(t *testing.T) { 532 var value RangeTimeEntity5 533 var result bool 534 var err string 535 536 //测试 正常情况 537 value = RangeTimeEntity5{CreateTime: time.Date(2019, 7, 24, 12, 0, 23, 321, LocationCST)} 538 result, err = validate.Check(value, "createTime") 539 TrueErr(t, result, err) 540 541 //测试 异常情况 542 value = RangeTimeEntity5{CreateTime: time.Date(2218, 7, 24, 12, 0, 23, 321, LocationCST)} 543 result, err = validate.Check(value, "createTime") 544 Equal(t, err, "属性 [CreateTime] 值 [2218-07-24 12:00:23.000000321 +0800 CST] 时间没有命中只允许的时间段 [past] 中", result, false) 545 } 546 547 // 测试时间类型6 548 func TestRangeTime6(t *testing.T) { 549 var value RangeTimeEntity6 550 var result bool 551 var err string 552 553 //测试 正常情况 554 value = RangeTimeEntity6{CreateTime: time.Date(2119, 7, 24, 12, 0, 23, 321, LocationCST)} 555 result, err = validate.Check(value, "createTime") 556 TrueErr(t, result, err) 557 558 //测试 异常情况 559 value = RangeTimeEntity6{CreateTime: time.Date(1918, 7, 24, 12, 0, 23, 321, LocationCST)} 560 result, err = validate.Check(value, "createTime") 561 Equal(t, err, "属性 [CreateTime] 值 [1918-07-24 12:00:23.000000321 +0800 CST] 时间没有命中只允许的时间段 [future] 中", result, false) 562 } 563 564 // 测试时间计算:年 565 func TestRangeCalTime1(t *testing.T) { 566 var value RangeTimeCalEntity1 567 var result bool 568 var err string 569 570 //测试 正常情况 571 value = RangeTimeCalEntity1{CreateTime: time.Now().AddDate(0, -3, 0)} 572 result, err = validate.Check(value, "createTime") 573 TrueErr(t, result, err) 574 575 //测试 异常情况 576 value = RangeTimeCalEntity1{CreateTime: time.Now().AddDate(-2, 0, 0)} 577 result, err = validate.Check(value, "createTime") 578 Equal(t, result, false) 579 } 580 581 // 测试时间计算:月 582 func TestRangeCalTime2(t *testing.T) { 583 var value RangeTimeCalEntity2 584 var result bool 585 var err string 586 587 //测试 正常情况 588 value = RangeTimeCalEntity2{CreateTime: time.Now().AddDate(0, 0, -2)} 589 result, err = validate.Check(value, "createTime") 590 TrueErr(t, result, err) 591 592 //测试 正常情况 593 value = RangeTimeCalEntity2{CreateTime: time.Now().AddDate(0, -1, 1)} 594 result, err = validate.Check(value, "createTime") 595 TrueErr(t, result, err) 596 597 //测试 异常情况 598 value = RangeTimeCalEntity2{CreateTime: time.Now().AddDate(0, -1, -1)} 599 result, err = validate.Check(value, "createTime") 600 Equal(t, result, false) 601 } 602 603 // 测试时间计算:月日 604 func TestRangeCalTime2And1(t *testing.T) { 605 var value RangeTimeCalEntity2And1 606 var result bool 607 var err string 608 609 //测试 正常情况 610 value = RangeTimeCalEntity2And1{CreateTime: time.Now().AddDate(0, 0, -2)} 611 result, err = validate.Check(value, "createTime") 612 TrueErr(t, result, err) 613 614 //测试 正常情况 615 value = RangeTimeCalEntity2And1{CreateTime: time.Now().AddDate(0, -1, -1)} 616 result, err = validate.Check(value, "createTime") 617 TrueErr(t, result, err) 618 619 //测试 异常情况 620 value = RangeTimeCalEntity2And1{CreateTime: time.Now().AddDate(0, -1, -4)} 621 result, err = validate.Check(value, "createTime") 622 Equal(t, result, false) 623 } 624 625 // 测试时间计算:年日 626 func TestRangeCalTime2And2(t *testing.T) { 627 var value RangeTimeCalEntity2And2 628 var result bool 629 var err string 630 631 //测试 正常情况 632 value = RangeTimeCalEntity2And2{CreateTime: time.Now().AddDate(-1, 0, -2)} 633 result, err = validate.Check(value, "createTime") 634 TrueErr(t, result, err) 635 636 //测试 正常情况 637 value = RangeTimeCalEntity2And2{CreateTime: time.Now().AddDate(-1, 0, -1)} 638 result, err = validate.Check(value, "createTime") 639 TrueErr(t, result, err) 640 641 //测试 异常情况 642 value = RangeTimeCalEntity2And2{CreateTime: time.Now().AddDate(-1, -1, 0)} 643 result, err = validate.Check(value, "createTime") 644 Equal(t, result, false) 645 } 646 647 // 测试时间计算:日 648 func TestRangeCalTime3(t *testing.T) { 649 var value RangeTimeCalEntity3 650 var result bool 651 var err string 652 653 //测试 正常情况 654 value = RangeTimeCalEntity3{CreateTime: time.Now().AddDate(0, 0, -2)} 655 result, err = validate.Check(value, "createTime") 656 TrueErr(t, result, err) 657 658 //测试 正常情况 659 value = RangeTimeCalEntity3{CreateTime: time.Now().AddDate(0, 0, 1)} 660 result, err = validate.Check(value, "createTime") 661 TrueErr(t, result, err) 662 663 //测试 异常情况 664 value = RangeTimeCalEntity3{CreateTime: time.Now().AddDate(0, 0, -6)} 665 result, err = validate.Check(value, "createTime") 666 Equal(t, result, false) 667 } 668 669 // 测试时间计算:时 670 func TestRangeCalTime4(t *testing.T) { 671 var value RangeTimeCalEntity4 672 var result bool 673 var err string 674 675 //测试 正常情况 676 d, _ := time.ParseDuration("-1h") 677 value = RangeTimeCalEntity4{CreateTime: time.Now().Add(d)} 678 result, err = validate.Check(value, "createTime") 679 TrueErr(t, result, err) 680 681 //测试 正常情况 682 d, _ = time.ParseDuration("4h") 683 value = RangeTimeCalEntity4{CreateTime: time.Now().Add(d)} 684 result, err = validate.Check(value, "createTime") 685 TrueErr(t, result, err) 686 687 //测试 异常情况 688 d, _ = time.ParseDuration("-6h") 689 value = RangeTimeCalEntity4{CreateTime: time.Now().Add(d)} 690 result, err = validate.Check(value, "createTime") 691 Equal(t, result, false) 692 } 693 694 // 测试时间计算:分钟 695 func TestRangeCalTime5(t *testing.T) { 696 var value RangeTimeCalEntity5 697 var result bool 698 var err string 699 var d time.Duration 700 701 //测试 正常情况 702 d, _ = time.ParseDuration("-10m") 703 value = RangeTimeCalEntity5{CreateTime: time.Now().Add(d)} 704 result, err = validate.Check(value, "createTime") 705 TrueErr(t, result, err) 706 707 //测试 正常情况 708 d, _ = time.ParseDuration("4m") 709 value = RangeTimeCalEntity5{CreateTime: time.Now().Add(d)} 710 result, err = validate.Check(value, "createTime") 711 TrueErr(t, result, err) 712 713 //测试 异常情况 714 d, _ = time.ParseDuration("-20m") 715 value = RangeTimeCalEntity5{CreateTime: time.Now().Add(d)} 716 result, _ = validate.Check(value, "createTime") 717 Equal(t, result, false) 718 } 719 720 // 测试时间计算:秒 721 func TestRangeCalTime6(t *testing.T) { 722 var value RangeTimeCalEntity6 723 var result bool 724 var err string 725 var d time.Duration 726 727 //测试 正常情况 728 d, _ = time.ParseDuration("-10s") 729 value = RangeTimeCalEntity6{CreateTime: time.Now().Add(d)} 730 result, err = validate.Check(value, "createTime") 731 TrueErr(t, result, err) 732 733 //测试 正常情况 734 d, _ = time.ParseDuration("4s") 735 value = RangeTimeCalEntity6{CreateTime: time.Now().Add(d)} 736 result, err = validate.Check(value, "createTime") 737 TrueErr(t, result, err) 738 739 //测试 异常情况 740 d, _ = time.ParseDuration("-200s") 741 value = RangeTimeCalEntity6{CreateTime: time.Now().Add(d)} 742 result, _ = validate.Check(value, "createTime") 743 Equal(t, result, false) 744 } 745 746 // 测试时间计算:秒 747 func TestRangeCalTime7(t *testing.T) { 748 var value RangeTimeCalEntity7 749 var result bool 750 var err string 751 var d time.Duration 752 753 //测试 正常情况 754 d, _ = time.ParseDuration("10h") 755 value = RangeTimeCalEntity7{CreateTime: time.Now().Add(d)} 756 result, err = validate.Check(value, "createTime") 757 TrueErr(t, result, err) 758 759 //测试 正常情况 760 d, _ = time.ParseDuration("+3h") 761 value = RangeTimeCalEntity7{CreateTime: time.Now().Add(d)} 762 result, err = validate.Check(value, "createTime") 763 TrueErr(t, result, err) 764 765 //测试 异常情况 766 d, _ = time.ParseDuration("-5h") 767 value = RangeTimeCalEntity7{CreateTime: time.Now().Add(d)} 768 result, _ = validate.Check(value, "createTime") 769 Equal(t, result, false) 770 } 771 772 // 压测进行基准测试 773 func Benchmark_Range(b *testing.B) { 774 var value RangeSliceEntity 775 for i := 0; i < b.N; i++ { 776 value = RangeSliceEntity{Age: []int{1}} 777 validate.Check(value, "age") 778 } 779 }