github.com/codingeasygo/util@v0.0.0-20231206062002-1ce2f004b7d9/xsql/xsql_test.go (about) 1 package xsql 2 3 // import ( 4 // "context" 5 // "encoding/json" 6 // "fmt" 7 // "sort" 8 // "testing" 9 // "time" 10 11 // "github.com/codingeasygo/util/attrvalid" 12 // "github.com/codingeasygo/util/converter" 13 // "github.com/codingeasygo/util/xmap" 14 // "github.com/codingeasygo/util/xtime" 15 // "github.com/jackc/pgx/v4/pgxpool" 16 // "github.com/shopspring/decimal" 17 // ) 18 19 // var Pool func() *pgxpool.Pool 20 21 // func init() { 22 // pool, err := pgxpool.Connect(context.Background(), "postgresql://dev:123@psql.loc:5432/dev") 23 // if err != nil { 24 // panic(err) 25 // } 26 // Pool = func() *pgxpool.Pool { 27 // return pool 28 // } 29 // } 30 31 // type timeObject struct { 32 // CreateTime Time 33 // } 34 35 // func TestTime(t *testing.T) { 36 // obj := &timeObject{ 37 // CreateTime: Time(time.Now()), 38 // } 39 // bys, err := json.Marshal(obj) 40 // if err != nil { 41 // t.Error(err) 42 // return 43 // } 44 // obj2 := &timeObject{} 45 // err = json.Unmarshal(bys, obj2) 46 // if err != nil { 47 // t.Error(err) 48 // return 49 // } 50 // if obj.CreateTime.Timestamp() != obj2.CreateTime.Timestamp() { 51 // t.Error("error") 52 // return 53 // } 54 // // 55 // t1 := TimeZero() 56 // bys, err = t1.MarshalJSON() 57 // if err != nil || string(bys) != "0" { 58 // t.Errorf("err:%v,bys:%v", err, string(bys)) 59 // return 60 // } 61 // // 62 // // 63 // t2 := Time{} 64 // err = t2.UnmarshalJSON([]byte("null")) 65 // if err != nil { 66 // t.Error(err) 67 // return 68 // } 69 // bys, err = t2.MarshalJSON() 70 // if err != nil || string(bys) != "0" { 71 // t.Errorf("err:%v,bys:%v", err, string(bys)) 72 // return 73 // } 74 // err = t2.Scan(time.Now()) 75 // if err != nil { 76 // t.Error(err) 77 // return 78 // } 79 // // 80 // // 81 // fmt.Printf("TimeZero--->%v\n", TimeZero()) 82 // fmt.Printf("TimeNow--->%v\n", TimeNow()) 83 // fmt.Printf("TimeStartOfToday--->%v\n", TimeStartOfToday()) 84 // fmt.Printf("TimeStartOfWeek--->%v\n", TimeStartOfWeek()) 85 // fmt.Printf("TimeStartOfMonth--->%v\n", TimeStartOfMonth()) 86 // fmt.Printf("TimeUnix--->%v\n", TimeUnix(0)) 87 // fmt.Printf("AsTime--->%v\n", t2.AsTime()) 88 // } 89 90 // func TestMap(t *testing.T) { 91 // var eval M 92 // err := eval.Scan(1) 93 // if err == nil { 94 // t.Error(err) 95 // return 96 // } 97 // // 98 // _, err = Pool().Exec(context.Background(), `drop table if exists xsql_test_map`) 99 // if err != nil { 100 // t.Error(err) 101 // return 102 // } 103 // _, err = Pool().Exec(context.Background(), `create table xsql_test_map(tid int,mval text)`) 104 // if err != nil { 105 // t.Error(err) 106 // return 107 // } 108 // { //normal 109 // var mval = M{"a": 1} 110 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_map values ($1,$2)`, 1, mval) 111 // if err != nil || !res.Insert() { 112 // t.Error(err) 113 // return 114 // } 115 // var mval1 M 116 // err = Pool().QueryRow(context.Background(), `select mval from xsql_test_map where tid=$1`, 1).Scan(&mval1) 117 // if err != nil || len(mval1) != 1 { 118 // t.Error(err) 119 // return 120 // } 121 // mv1 := xmap.Wrap(xmap.M(mval1)) 122 // if mv1.Int("a") != 1 { 123 // t.Error("error") 124 // } 125 // } 126 // { //nil 127 // var mval M = nil 128 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_map values ($1,$2)`, 2, mval) 129 // if err != nil || !res.Insert() { 130 // t.Error(err) 131 // return 132 // } 133 // var mval1 M 134 // err = Pool().QueryRow(context.Background(), `select mval from xsql_test_map where tid=$1`, 2).Scan(&mval1) 135 // if err != nil || len(mval1) != 0 { 136 // t.Error(err) 137 // return 138 // } 139 // mval1.RawMap() 140 // mval1.AsMap() 141 // } 142 // } 143 144 // func TestMapArray(t *testing.T) { 145 // var eval MArray 146 // err := eval.Scan(1) 147 // if err == nil { 148 // t.Error(err) 149 // return 150 // } 151 // // 152 // _, err = Pool().Exec(context.Background(), `drop table if exists xsql_test_map`) 153 // if err != nil { 154 // t.Error(err) 155 // return 156 // } 157 // _, err = Pool().Exec(context.Background(), `create table xsql_test_map(tid int,mval text)`) 158 // if err != nil { 159 // t.Error(err) 160 // return 161 // } 162 // { //normal 163 // var mval = MArray{{"a": 1}} 164 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_map values ($1,$2)`, 1, mval) 165 // if err != nil || !res.Insert() { 166 // t.Error(err) 167 // return 168 // } 169 // var mval1 MArray 170 // err = Pool().QueryRow(context.Background(), `select mval from xsql_test_map where tid=$1`, 1).Scan(&mval1) 171 // if err != nil || len(mval1) != 1 { 172 // t.Error(err) 173 // return 174 // } 175 // mv1 := xmap.Wrap(mval1[0]) 176 // if mv1.Int("a") != 1 { 177 // t.Error("error") 178 // } 179 // } 180 // { //nil 181 // var mval MArray = nil 182 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_map values ($1,$2)`, 2, mval) 183 // if err != nil || !res.Insert() { 184 // t.Error(err) 185 // return 186 // } 187 // var mval1 MArray 188 // err = Pool().QueryRow(context.Background(), `select mval from xsql_test_map where tid=$1`, 2).Scan(&mval1) 189 // if err != nil || len(mval1) != 0 { 190 // t.Error(err) 191 // return 192 // } 193 // } 194 // } 195 196 // func TestSQLScan(t *testing.T) { 197 // err := sqlScan(nil, nil, nil) 198 // if err != nil { 199 // t.Error(err) 200 // return 201 // } 202 // err = sqlScan("xx", nil, nil) 203 // if err == nil { 204 // t.Error(err) 205 // return 206 // } 207 // err = sqlScan("[xx", nil, nil) 208 // if err == nil { 209 // t.Error(err) 210 // return 211 // } 212 // err = sqlScan("", nil, nil) 213 // if err != nil { 214 // t.Error(err) 215 // return 216 // } 217 // err = sqlScan("null", nil, nil) 218 // if err != nil { 219 // t.Error(err) 220 // return 221 // } 222 // } 223 224 // func TestIntArray(t *testing.T) { 225 // var ary IntArray 226 // err := ary.Scan(1) 227 // if err == nil { 228 // t.Error(err) 229 // return 230 // } 231 // err = ary.Scan("a") 232 // if err == nil { 233 // t.Error(err) 234 // return 235 // } 236 // // ary.Value() 237 // // 238 // v0, v1, v2 := int(3), int(2), int(1) 239 // ary = append(ary, v0) 240 // ary = append(ary, v1) 241 // ary = append(ary, v2) 242 // sort.Sort(ary) 243 // // 244 // _, err = Pool().Exec(context.Background(), `drop table if exists xsql_test_int`) 245 // if err != nil { 246 // t.Error(err) 247 // return 248 // } 249 // _, err = Pool().Exec(context.Background(), `create table xsql_test_int(tid int,iarry text)`) 250 // if err != nil { 251 // t.Error(err) 252 // return 253 // } 254 // { //normal 255 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_int`) 256 // if err != nil { 257 // t.Error(err) 258 // return 259 // } 260 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int values ($1,$2)`, 1, ary) 261 // if err != nil || !res.Insert() { 262 // t.Error(err) 263 // return 264 // } 265 // var ary1 IntArray 266 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int where tid=$1`, 1).Scan(&ary1) 267 // if err != nil || len(ary1) != 3 { 268 // t.Error(err) 269 // return 270 // } 271 // if !ary1.HavingOne(3) { 272 // t.Error("error") 273 // return 274 // } 275 // if ary1.HavingOne(4) { 276 // t.Error("error") 277 // return 278 // } 279 // if ary1.Join(",") != "1,2,3" { 280 // t.Error("error") 281 // } 282 // if ary1.DbArray() != "{1,2,3}" { 283 // t.Error("error") 284 // } 285 // ary2 := append(ary1, 3).RemoveDuplicate() 286 // if ary2.DbArray() != "{1,2,3}" { 287 // t.Error("error") 288 // } 289 // } 290 // { //string array 291 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_int`) 292 // if err != nil { 293 // t.Error(err) 294 // return 295 // } 296 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int values ($1,$2)`, 1, ary.StrArray()) 297 // if err != nil || !res.Insert() { 298 // t.Error(err) 299 // return 300 // } 301 // var ary1 IntArray 302 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int where tid=$1`, 1).Scan(&ary1) 303 // if err != nil || len(ary1) != 3 { 304 // t.Error(err) 305 // return 306 // } 307 // if ary1.Join(",") != "1,2,3" { 308 // t.Error("error") 309 // } 310 // } 311 // { //nil 312 // var arynil IntArray = nil 313 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int values ($1,$2)`, 2, arynil) 314 // if err != nil || !res.Insert() { 315 // t.Error(err) 316 // return 317 // } 318 // var ary1 IntArray 319 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int where tid=$1`, 2).Scan(&ary1) 320 // if err != nil || len(ary1) != 0 { 321 // t.Error(err) 322 // return 323 // } 324 // } 325 // ary.AsPtrArray() 326 // } 327 328 // func TestIntPtrArray(t *testing.T) { 329 // var ary IntPtrArray 330 // err := ary.Scan(1) 331 // if err == nil { 332 // t.Error(err) 333 // return 334 // } 335 // err = ary.Scan("a") 336 // if err == nil { 337 // t.Error(err) 338 // return 339 // } 340 // // ary.Value() 341 // // 342 // v0, v1, v2 := int(3), int(2), int(1) 343 // ary = append(ary, &v0) 344 // ary = append(ary, &v1) 345 // ary = append(ary, &v2) 346 // ary = append(ary, nil) 347 // sort.Sort(ary) 348 // // 349 // _, err = Pool().Exec(context.Background(), `drop table if exists xsql_test_int`) 350 // if err != nil { 351 // t.Error(err) 352 // return 353 // } 354 // _, err = Pool().Exec(context.Background(), `create table xsql_test_int(tid int,iarry text)`) 355 // if err != nil { 356 // t.Error(err) 357 // return 358 // } 359 // { //normal 360 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_int`) 361 // if err != nil { 362 // t.Error(err) 363 // return 364 // } 365 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int values ($1,$2)`, 1, ary) 366 // if err != nil || !res.Insert() { 367 // t.Error(err) 368 // return 369 // } 370 // var ary1 IntPtrArray 371 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int where tid=$1`, 1).Scan(&ary1) 372 // if err != nil || len(ary1) != 4 { 373 // t.Errorf("%v,%v", err, ary1) 374 // return 375 // } 376 // if !ary1.HavingOne(3) { 377 // t.Error("error") 378 // return 379 // } 380 // if ary1.HavingOne(4) { 381 // t.Error("error") 382 // return 383 // } 384 // if ary1.Join(",") != "1,2,3" { 385 // t.Errorf("%v", ary1) 386 // return 387 // } 388 // if ary1.DbArray() != "{1,2,3}" { 389 // t.Error("error") 390 // } 391 // ary2 := append(ary1, converter.IntPtr(3)).RemoveDuplicate() 392 // if ary2.DbArray() != "{1,2,3}" { 393 // t.Error("error") 394 // } 395 // } 396 // { //string array 397 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_int`) 398 // if err != nil { 399 // t.Error(err) 400 // return 401 // } 402 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int values ($1,$2)`, 1, ary.StrArray()) 403 // if err != nil || !res.Insert() { 404 // t.Error(err) 405 // return 406 // } 407 // var ary1 IntPtrArray 408 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int where tid=$1`, 1).Scan(&ary1) 409 // if err != nil || len(ary1) != 3 { 410 // t.Error(err) 411 // return 412 // } 413 // if ary1.Join(",") != "1,2,3" { 414 // t.Error("error") 415 // } 416 // } 417 // { //nil 418 // var arynil IntPtrArray = nil 419 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int values ($1,$2)`, 2, arynil) 420 // if err != nil || !res.Insert() { 421 // t.Error(err) 422 // return 423 // } 424 // var ary1 IntPtrArray 425 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int where tid=$1`, 2).Scan(&ary1) 426 // if err != nil || len(ary1) != 0 { 427 // t.Error(err) 428 // return 429 // } 430 // { //join 431 // var ary1 = IntPtrArray{} 432 // ary1 = append(ary1, &v0) 433 // ary1 = append(ary1, nil) 434 // ary1 = append(ary1, &v2) 435 // sort.Sort(ary1) 436 // if ary1.Join(",") != "1,3" { 437 // t.Error(ary1.Join(",")) 438 // return 439 // } 440 // } 441 // } 442 // ary.AsArray() 443 // ary[0] = nil 444 // ary.AsArray() 445 // } 446 447 // func TestInt64Array(t *testing.T) { 448 // var ary Int64Array 449 // err := ary.Scan(1) 450 // if err == nil { 451 // t.Error(err) 452 // return 453 // } 454 // err = ary.Scan("a") 455 // if err == nil { 456 // t.Error(err) 457 // return 458 // } 459 // // ary.Value() 460 // // 461 // v0, v1, v2 := int64(3), int64(2), int64(1) 462 // ary = append(ary, v0) 463 // ary = append(ary, v1) 464 // ary = append(ary, v2) 465 // sort.Sort(ary) 466 // // 467 // _, err = Pool().Exec(context.Background(), `drop table if exists xsql_test_int64`) 468 // if err != nil { 469 // t.Error(err) 470 // return 471 // } 472 // _, err = Pool().Exec(context.Background(), `create table xsql_test_int64(tid int,iarry text)`) 473 // if err != nil { 474 // t.Error(err) 475 // return 476 // } 477 // { //normal 478 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_int64`) 479 // if err != nil { 480 // t.Error(err) 481 // return 482 // } 483 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int64 values ($1,$2)`, 1, ary) 484 // if err != nil || !res.Insert() { 485 // t.Error(err) 486 // return 487 // } 488 // var ary1 Int64Array 489 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int64 where tid=$1`, 1).Scan(&ary1) 490 // if err != nil || len(ary1) != 3 { 491 // t.Error(err) 492 // return 493 // } 494 // if !ary1.HavingOne(3) { 495 // t.Error("error") 496 // return 497 // } 498 // if ary1.HavingOne(4) { 499 // t.Error("error") 500 // return 501 // } 502 // if ary1.Join(",") != "1,2,3" { 503 // t.Error("error") 504 // } 505 // if ary1.DbArray() != "{1,2,3}" { 506 // t.Error("error") 507 // } 508 // ary2 := append(ary1, 3).RemoveDuplicate() 509 // if ary2.DbArray() != "{1,2,3}" { 510 // t.Error("error") 511 // } 512 // } 513 // { //string array 514 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_int64`) 515 // if err != nil { 516 // t.Error(err) 517 // return 518 // } 519 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int64 values ($1,$2)`, 1, ary.StrArray()) 520 // if err != nil || !res.Insert() { 521 // t.Error(err) 522 // return 523 // } 524 // var ary1 Int64Array 525 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int64 where tid=$1`, 1).Scan(&ary1) 526 // if err != nil || len(ary1) != 3 { 527 // t.Error(err) 528 // return 529 // } 530 // if ary1.Join(",") != "1,2,3" { 531 // t.Error("error") 532 // } 533 // } 534 // { //nil 535 // var arynil Int64Array = nil 536 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int64 values ($1,$2)`, 2, arynil) 537 // if err != nil || !res.Insert() { 538 // t.Error(err) 539 // return 540 // } 541 // var ary1 Int64Array 542 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int64 where tid=$1`, 2).Scan(&ary1) 543 // if err != nil || len(ary1) != 0 { 544 // t.Error(err) 545 // return 546 // } 547 // } 548 // ary.AsPtrArray() 549 // } 550 551 // func TestInt64PtrArray(t *testing.T) { 552 // var ary Int64PtrArray 553 // err := ary.Scan(1) 554 // if err == nil { 555 // t.Error(err) 556 // return 557 // } 558 // err = ary.Scan("a") 559 // if err == nil { 560 // t.Error(err) 561 // return 562 // } 563 // // ary.Value() 564 // // 565 // v0, v1, v2 := int64(3), int64(2), int64(1) 566 // ary = append(ary, &v0) 567 // ary = append(ary, &v1) 568 // ary = append(ary, &v2) 569 // sort.Sort(ary) 570 // // 571 // _, err = Pool().Exec(context.Background(), `drop table if exists xsql_test_int64`) 572 // if err != nil { 573 // t.Error(err) 574 // return 575 // } 576 // _, err = Pool().Exec(context.Background(), `create table xsql_test_int64(tid int,iarry text)`) 577 // if err != nil { 578 // t.Error(err) 579 // return 580 // } 581 // { //normal 582 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_int64`) 583 // if err != nil { 584 // t.Error(err) 585 // return 586 // } 587 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int64 values ($1,$2)`, 1, ary) 588 // if err != nil || !res.Insert() { 589 // t.Error(err) 590 // return 591 // } 592 // var ary1 Int64PtrArray 593 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int64 where tid=$1`, 1).Scan(&ary1) 594 // if err != nil || len(ary1) != 3 { 595 // t.Error(err) 596 // return 597 // } 598 // if !ary1.HavingOne(3) { 599 // t.Error("error") 600 // return 601 // } 602 // if ary1.HavingOne(4) { 603 // t.Error("error") 604 // return 605 // } 606 // if ary1.Join(",") != "1,2,3" { 607 // t.Error("error") 608 // } 609 // if ary1.DbArray() != "{1,2,3}" { 610 // t.Error("error") 611 // } 612 // ary2 := append(ary1, converter.Int64Ptr(3)).RemoveDuplicate() 613 // if ary2.DbArray() != "{1,2,3}" { 614 // t.Error("error") 615 // } 616 // } 617 // { //string array 618 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_int64`) 619 // if err != nil { 620 // t.Error(err) 621 // return 622 // } 623 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int64 values ($1,$2)`, 1, ary.StrArray()) 624 // if err != nil || !res.Insert() { 625 // t.Error(err) 626 // return 627 // } 628 // var ary1 Int64PtrArray 629 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int64 where tid=$1`, 1).Scan(&ary1) 630 // if err != nil || len(ary1) != 3 { 631 // t.Error(err) 632 // return 633 // } 634 // if ary1.Join(",") != "1,2,3" { 635 // t.Error("error") 636 // } 637 // } 638 // { //nil 639 // var arynil Int64PtrArray = nil 640 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_int64 values ($1,$2)`, 2, arynil) 641 // if err != nil || !res.Insert() { 642 // t.Error(err) 643 // return 644 // } 645 // var ary1 Int64PtrArray 646 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_int64 where tid=$1`, 2).Scan(&ary1) 647 // if err != nil || len(ary1) != 0 { 648 // t.Error(err) 649 // return 650 // } 651 // { //join 652 // var ary1 = Int64PtrArray{} 653 // ary1 = append(ary1, &v0) 654 // ary1 = append(ary1, nil) 655 // ary1 = append(ary1, &v2) 656 // sort.Sort(ary1) 657 // if ary1.Join(",") != "1,3" { 658 // t.Error(ary1.Join(",")) 659 // return 660 // } 661 // } 662 // } 663 // ary.AsArray() 664 // ary[0] = nil 665 // ary.AsArray() 666 // } 667 668 // func TestFloat64Array(t *testing.T) { 669 // var ary Float64Array 670 // err := ary.Scan(1) 671 // if err == nil { 672 // t.Error(err) 673 // return 674 // } 675 // err = ary.Scan("a") 676 // if err == nil { 677 // t.Error(err) 678 // return 679 // } 680 // // ary.Value() 681 // // 682 // v0, v1, v2 := float64(3), float64(2), float64(1) 683 // ary = append(ary, v0) 684 // ary = append(ary, v1) 685 // ary = append(ary, v2) 686 // sort.Sort(ary) 687 // // 688 // _, err = Pool().Exec(context.Background(), `drop table if exists xsql_test_float64`) 689 // if err != nil { 690 // t.Error(err) 691 // return 692 // } 693 // _, err = Pool().Exec(context.Background(), `create table xsql_test_float64(tid int,iarry text)`) 694 // if err != nil { 695 // t.Error(err) 696 // return 697 // } 698 // { //normal 699 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_float64`) 700 // if err != nil { 701 // t.Error(err) 702 // return 703 // } 704 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_float64 values ($1,$2)`, 1, ary) 705 // if err != nil || !res.Insert() { 706 // t.Error(err) 707 // return 708 // } 709 // var ary1 Float64Array 710 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_float64 where tid=$1`, 1).Scan(&ary1) 711 // if err != nil || len(ary1) != 3 { 712 // t.Error(err) 713 // return 714 // } 715 // if !ary1.HavingOne(3) { 716 // t.Error("error") 717 // return 718 // } 719 // if ary1.HavingOne(4) { 720 // t.Error("error") 721 // return 722 // } 723 // if ary1.Join(",") != "1,2,3" { 724 // t.Error("error") 725 // } 726 // if ary1.DbArray() != "{1,2,3}" { 727 // t.Error("error") 728 // } 729 // ary2 := append(ary1, 3).RemoveDuplicate() 730 // if ary2.DbArray() != "{1,2,3}" { 731 // t.Error("error") 732 // } 733 // } 734 // { //string array 735 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_float64`) 736 // if err != nil { 737 // t.Error(err) 738 // return 739 // } 740 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_float64 values ($1,$2)`, 1, ary.StrArray()) 741 // if err != nil || !res.Insert() { 742 // t.Error(err) 743 // return 744 // } 745 // var ary1 Float64Array 746 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_float64 where tid=$1`, 1).Scan(&ary1) 747 // if err != nil || len(ary1) != 3 { 748 // t.Error(err) 749 // return 750 // } 751 // if ary1.Join(",") != "1,2,3" { 752 // t.Error("error") 753 // } 754 // } 755 // { //nil 756 // var arynil Float64Array = nil 757 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_float64 values ($1,$2)`, 2, arynil) 758 // if err != nil || !res.Insert() { 759 // t.Error(err) 760 // return 761 // } 762 // var ary1 Float64Array 763 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_float64 where tid=$1`, 2).Scan(&ary1) 764 // if err != nil || len(ary1) != 0 { 765 // t.Error(err) 766 // return 767 // } 768 // } 769 // ary.AsPtrArray() 770 // } 771 772 // func TestFloat64PtrArray(t *testing.T) { 773 // var ary Float64PtrArray 774 // err := ary.Scan(1) 775 // if err == nil { 776 // t.Error(err) 777 // return 778 // } 779 // err = ary.Scan("a") 780 // if err == nil { 781 // t.Error(err) 782 // return 783 // } 784 // // ary.Value() 785 // // 786 // v0, v1, v2 := float64(3), float64(2), float64(1) 787 // ary = append(ary, &v0) 788 // ary = append(ary, &v1) 789 // ary = append(ary, &v2) 790 // sort.Sort(ary) 791 // // 792 // _, err = Pool().Exec(context.Background(), `drop table if exists xsql_test_float64`) 793 // if err != nil { 794 // t.Error(err) 795 // return 796 // } 797 // _, err = Pool().Exec(context.Background(), `create table xsql_test_float64(tid int,iarry text)`) 798 // if err != nil { 799 // t.Error(err) 800 // return 801 // } 802 // { //normal 803 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_float64`) 804 // if err != nil { 805 // t.Error(err) 806 // return 807 // } 808 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_float64 values ($1,$2)`, 1, ary) 809 // if err != nil || !res.Insert() { 810 // t.Error(err) 811 // return 812 // } 813 // var ary1 Float64PtrArray 814 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_float64 where tid=$1`, 1).Scan(&ary1) 815 // if err != nil || len(ary1) != 3 { 816 // t.Error(err) 817 // return 818 // } 819 // if !ary1.HavingOne(3) { 820 // t.Error("error") 821 // return 822 // } 823 // if ary1.HavingOne(4) { 824 // t.Error("error") 825 // return 826 // } 827 // if ary1.Join(",") != "1,2,3" { 828 // t.Error(ary1.Join(",")) 829 // } 830 // if ary1.DbArray() != "{1,2,3}" { 831 // t.Error("error") 832 // } 833 // ary2 := append(ary1, converter.Float64Ptr(3)).RemoveDuplicate() 834 // if ary2.DbArray() != "{1,2,3}" { 835 // t.Error("error") 836 // } 837 // } 838 // { //string array 839 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_float64`) 840 // if err != nil { 841 // t.Error(err) 842 // return 843 // } 844 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_float64 values ($1,$2)`, 1, ary.StrArray()) 845 // if err != nil || !res.Insert() { 846 // t.Error(err) 847 // return 848 // } 849 // var ary1 Float64PtrArray 850 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_float64 where tid=$1`, 1).Scan(&ary1) 851 // if err != nil || len(ary1) != 3 { 852 // t.Error(err) 853 // return 854 // } 855 // if ary1.Join(",") != "1,2,3" { 856 // t.Error("error") 857 // } 858 // } 859 // { //nil 860 // var arynil Float64PtrArray = nil 861 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_float64 values ($1,$2)`, 2, arynil) 862 // if err != nil || !res.Insert() { 863 // t.Error(err) 864 // return 865 // } 866 // var ary1 Float64PtrArray 867 // err = Pool().QueryRow(context.Background(), `select iarry from xsql_test_float64 where tid=$1`, 2).Scan(&ary1) 868 // if err != nil || len(ary1) != 0 { 869 // t.Error(err) 870 // return 871 // } 872 // } 873 // { //join 874 // var ary1 = Float64PtrArray{} 875 // ary1 = append(ary1, &v0) 876 // ary1 = append(ary1, nil) 877 // ary1 = append(ary1, &v2) 878 // sort.Sort(ary1) 879 // if ary1.Join(",") != "1,3" { 880 // t.Error(ary1.Join(",")) 881 // return 882 // } 883 // } 884 // ary.AsArray() 885 // ary[0] = nil 886 // ary.AsArray() 887 // } 888 889 // func TestStringArray(t *testing.T) { 890 // var ary StringArray 891 // err := ary.Scan(1) 892 // if err == nil { 893 // t.Error(err) 894 // return 895 // } 896 // err = ary.Scan("a") 897 // if err == nil { 898 // t.Error(err) 899 // return 900 // } 901 // // ary.Value() 902 // // 903 // v0, v1, v2 := "3", "2", "1" 904 // ary = append(ary, v0) 905 // ary = append(ary, v1) 906 // ary = append(ary, v2) 907 // sort.Sort(ary) 908 // // 909 // _, err = Pool().Exec(context.Background(), `drop table if exists xsql_test_string`) 910 // if err != nil { 911 // t.Error(err) 912 // return 913 // } 914 // _, err = Pool().Exec(context.Background(), `create table xsql_test_string(tid int,sarry text)`) 915 // if err != nil { 916 // t.Error(err) 917 // return 918 // } 919 // { //normal 920 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_string`) 921 // if err != nil { 922 // t.Error(err) 923 // return 924 // } 925 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_string values ($1,$2)`, 1, ary) 926 // if err != nil || !res.Insert() { 927 // t.Error(err) 928 // return 929 // } 930 // var ary1 StringArray 931 // err = Pool().QueryRow(context.Background(), `select sarry from xsql_test_string where tid=$1`, 1).Scan(&ary1) 932 // if err != nil || len(ary1) != 3 { 933 // t.Error(err) 934 // return 935 // } 936 // if !ary1.HavingOne("3") { 937 // t.Error("error") 938 // return 939 // } 940 // if ary1.HavingOne("4") { 941 // t.Error("error") 942 // return 943 // } 944 // if ary1.Join(",") != "1,2,3" { 945 // t.Error("error") 946 // } 947 // if ary1.DbArray() != "{1,2,3}" { 948 // t.Error("error") 949 // } 950 // ary2 := append(ary1, "3", "", " ").RemoveDuplicate(true, true) 951 // if ary2.DbArray() != "{1,2,3}" { 952 // t.Error("error") 953 // } 954 // ary3 := append(ary1, "", " ").RemoveEmpty(true) 955 // if ary3.DbArray() != "{1,2,3}" { 956 // t.Error("error") 957 // } 958 // } 959 // { //string array 960 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_string`) 961 // if err != nil { 962 // t.Error(err) 963 // return 964 // } 965 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_string values ($1,$2)`, 1, ary.StrArray()) 966 // if err != nil || !res.Insert() { 967 // t.Error(err) 968 // return 969 // } 970 // var ary1 StringArray 971 // err = Pool().QueryRow(context.Background(), `select sarry from xsql_test_string where tid=$1`, 1).Scan(&ary1) 972 // if err != nil || len(ary1) != 3 { 973 // t.Error(err) 974 // return 975 // } 976 // if ary1.Join(",") != "1,2,3" { 977 // t.Error("error") 978 // } 979 // } 980 // { //nil 981 // var arynil StringArray = nil 982 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_string values ($1,$2)`, 2, arynil) 983 // if err != nil || !res.Insert() { 984 // t.Error(err) 985 // return 986 // } 987 // var ary1 StringArray 988 // err = Pool().QueryRow(context.Background(), `select sarry from xsql_test_string where tid=$1`, 2).Scan(&ary1) 989 // if err != nil || len(ary1) != 0 { 990 // t.Error(err) 991 // return 992 // } 993 // } 994 // ary.AsPtrArray() 995 // } 996 997 // func TestStringPtrArray(t *testing.T) { 998 // var ary StringPtrArray 999 // err := ary.Scan(1) 1000 // if err == nil { 1001 // t.Error(err) 1002 // return 1003 // } 1004 // err = ary.Scan("a") 1005 // if err == nil { 1006 // t.Error(err) 1007 // return 1008 // } 1009 // // ary.Value() 1010 // // 1011 // v0, v1, v2 := "3", "2", "1" 1012 // ary = append(ary, &v0) 1013 // ary = append(ary, &v1) 1014 // ary = append(ary, &v2) 1015 // sort.Sort(ary) 1016 // // 1017 // _, err = Pool().Exec(context.Background(), `drop table if exists xsql_test_string`) 1018 // if err != nil { 1019 // t.Error(err) 1020 // return 1021 // } 1022 // _, err = Pool().Exec(context.Background(), `create table xsql_test_string(tid int,sarry text)`) 1023 // if err != nil { 1024 // t.Error(err) 1025 // return 1026 // } 1027 // { //normal 1028 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_string`) 1029 // if err != nil { 1030 // t.Error(err) 1031 // return 1032 // } 1033 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_string values ($1,$2)`, 1, ary) 1034 // if err != nil || !res.Insert() { 1035 // t.Error(err) 1036 // return 1037 // } 1038 // var ary1 StringPtrArray 1039 // err = Pool().QueryRow(context.Background(), `select sarry from xsql_test_string where tid=$1`, 1).Scan(&ary1) 1040 // if err != nil || len(ary1) != 3 { 1041 // t.Error(err) 1042 // return 1043 // } 1044 // if !ary1.HavingOne("3") { 1045 // t.Error("error") 1046 // return 1047 // } 1048 // if ary1.HavingOne("4") { 1049 // t.Error("error") 1050 // return 1051 // } 1052 // if ary1.Join(",") != "1,2,3" { 1053 // t.Error("error") 1054 // } 1055 // if ary1.DbArray() != "{1,2,3}" { 1056 // t.Error("error") 1057 // } 1058 // ary2 := append(ary1, nil, converter.StringPtr("3"), converter.StringPtr(""), converter.StringPtr(" ")).RemoveDuplicate(true, true) 1059 // if ary2.DbArray() != "{1,2,3}" { 1060 // t.Error("error") 1061 // } 1062 // ary3 := append(ary1, nil, converter.StringPtr(""), converter.StringPtr(" ")).RemoveEmpty(true) 1063 // if ary3.DbArray() != "{1,2,3}" { 1064 // t.Error("error") 1065 // } 1066 // } 1067 // { //string array 1068 // _, err = Pool().Exec(context.Background(), `delete from xsql_test_string`) 1069 // if err != nil { 1070 // t.Error(err) 1071 // return 1072 // } 1073 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_string values ($1,$2)`, 1, ary.StrArray()) 1074 // if err != nil || !res.Insert() { 1075 // t.Error(err) 1076 // return 1077 // } 1078 // var ary1 StringPtrArray 1079 // err = Pool().QueryRow(context.Background(), `select sarry from xsql_test_string where tid=$1`, 1).Scan(&ary1) 1080 // if err != nil || len(ary1) != 3 { 1081 // t.Error(err) 1082 // return 1083 // } 1084 // if ary1.Join(",") != "1,2,3" { 1085 // t.Error("error") 1086 // } 1087 // } 1088 // { //nil 1089 // var arynil StringPtrArray = nil 1090 // res, err := Pool().Exec(context.Background(), `insert into xsql_test_string values ($1,$2)`, 2, arynil) 1091 // if err != nil || !res.Insert() { 1092 // t.Error(err) 1093 // return 1094 // } 1095 // var ary1 StringPtrArray 1096 // err = Pool().QueryRow(context.Background(), `select sarry from xsql_test_string where tid=$1`, 2).Scan(&ary1) 1097 // if err != nil || len(ary1) != 0 { 1098 // t.Error(err) 1099 // return 1100 // } 1101 // } 1102 // { //join 1103 // var ary1 = StringPtrArray{} 1104 // ary1 = append(ary1, &v0) 1105 // ary1 = append(ary1, nil) 1106 // ary1 = append(ary1, &v2) 1107 // sort.Sort(ary1) 1108 // if ary1.Join(",") != "1,3" { 1109 // t.Error(ary1.Join(",")) 1110 // return 1111 // } 1112 // } 1113 // ary.AsArray() 1114 // ary[0] = nil 1115 // ary.AsArray() 1116 // } 1117 1118 // func TestIsNilZero(t *testing.T) { 1119 // var smap M 1120 // if !smap.IsNil() || !smap.IsZero() { 1121 // t.Error("error") 1122 // return 1123 // } 1124 // var smapArray MArray 1125 // if !smapArray.IsNil() || !smapArray.IsZero() { 1126 // t.Error("error") 1127 // return 1128 // } 1129 // var stime Time 1130 // if stime.IsNil() || !stime.IsZero() { 1131 // t.Error("error") 1132 // return 1133 // } 1134 // var sint IntArray 1135 // if !sint.IsNil() || !sint.IsZero() { 1136 // t.Error("error") 1137 // return 1138 // } 1139 // var sintPtr IntPtrArray 1140 // if !sintPtr.IsNil() || !sintPtr.IsZero() { 1141 // t.Error("error") 1142 // return 1143 // } 1144 // var sint64 Int64Array 1145 // if !sint64.IsNil() || !sint64.IsZero() { 1146 // t.Error("error") 1147 // return 1148 // } 1149 // var sint64Ptr Int64PtrArray 1150 // if !sint64Ptr.IsNil() || !sint64Ptr.IsZero() { 1151 // t.Error("error") 1152 // return 1153 // } 1154 // var sfloat64 Float64Array 1155 // if !sfloat64.IsNil() || !sfloat64.IsZero() { 1156 // t.Error("error") 1157 // return 1158 // } 1159 // var sfloat64Ptr Float64PtrArray 1160 // if !sfloat64Ptr.IsNil() || !sfloat64Ptr.IsZero() { 1161 // t.Error("error") 1162 // return 1163 // } 1164 // var sstr StringArray 1165 // if !sstr.IsNil() || !sstr.IsZero() { 1166 // t.Error("error") 1167 // return 1168 // } 1169 // var sstrPtr StringPtrArray 1170 // if !sstrPtr.IsNil() || !sstrPtr.IsZero() { 1171 // t.Error("error") 1172 // return 1173 // } 1174 // } 1175 1176 // func TestValidFormat(t *testing.T) { 1177 // data := attrvalid.M{ 1178 // "json": converter.JSON(M{"abc": 123}), 1179 // "json_list": converter.JSON([]M{{"abc": 123}}), 1180 // "time": xtime.TimeNow(), 1181 // "empty": "", 1182 // } 1183 // var smap M 1184 // var smapArray MArray 1185 // var stime Time 1186 // var sint IntArray 1187 // var sintPtr IntPtrArray 1188 // var sint64 Int64Array 1189 // var sint64Ptr Int64PtrArray 1190 // var sfloat64 Float64Array 1191 // var sfloat64Ptr Float64PtrArray 1192 // var sstr StringArray 1193 // var sstrPtr StringPtrArray 1194 // var etime Time 1195 // err := data.ValidFormat(` 1196 // json,R|S,L:0;json_list,R|S,L:0; 1197 // time,R|I,R:0; 1198 // time,R|I,R:0;time,R|I,R:0; 1199 // time,R|I,R:0;time,R|I,R:0; 1200 // time,R|I,R:0;time,R|I,R:0; 1201 // time,R|I,R:0;time,R|I,R:0; 1202 // empty,O|I,R:0; 1203 // `, 1204 // &smap, &smapArray, 1205 // &stime, 1206 // &sint, &sintPtr, 1207 // &sint64, &sint64Ptr, 1208 // &sfloat64, &sfloat64Ptr, 1209 // &sstr, &sstrPtr, 1210 // &etime, 1211 // ) 1212 // if err != nil || stime.Timestamp() < 1 || len(sint) < 1 || len(sintPtr) < 1 { 1213 // t.Error(err) 1214 // return 1215 // } 1216 // fmt.Println("-->", stime, sint, sintPtr) 1217 // if err = stime.Set(int64(0)); err != nil { 1218 // t.Error(err) 1219 // return 1220 // } 1221 // if err = stime.Set(Time{}); err != nil { 1222 // t.Error(err) 1223 // return 1224 // } 1225 // if err = stime.Set(&stime); err != nil { 1226 // t.Error(err) 1227 // return 1228 // } 1229 // if err = stime.Set(time.Now()); err != nil { 1230 // t.Error(err) 1231 // return 1232 // } 1233 // ntime := time.Now() 1234 // if err = stime.Set(&ntime); err != nil { 1235 // t.Error(err) 1236 // return 1237 // } 1238 // if err = stime.Set("xxx"); err == nil { 1239 // t.Error(err) 1240 // return 1241 // } 1242 // } 1243 1244 // func TestValidDecimal(t *testing.T) { 1245 // data := attrvalid.M{ 1246 // "int": 100, 1247 // "float": 100.0, 1248 // "string": "100.0", 1249 // } 1250 // var val0, val1, val2 decimal.Decimal 1251 // err := data.ValidFormat(` 1252 // int,R|I,R:0; 1253 // float,R|F,R:0; 1254 // string,R|F,R:0; 1255 // `, 1256 // &val0, &val1, &val2, 1257 // ) 1258 // if err != nil { 1259 // t.Error(err) 1260 // return 1261 // } 1262 // fmt.Println("-->", val0, val1, val2) 1263 1264 // var args = struct { 1265 // A decimal.Decimal `json:"a" valid:"a,r|f,r:0"` 1266 // }{} 1267 // err = attrvalid.Valid(&args, "#all", "") 1268 // if err == nil { 1269 // t.Error(err) 1270 // return 1271 // } 1272 // args.A = decimal.NewFromFloat(-1) 1273 // err = attrvalid.Valid(&args, "#all", "") 1274 // if err == nil { 1275 // t.Error(err) 1276 // return 1277 // } 1278 // args.A = decimal.NewFromFloat(10) 1279 // err = attrvalid.Valid(&args, "#all", "") 1280 // if err != nil { 1281 // t.Error(err) 1282 // return 1283 // } 1284 // } 1285 1286 // func TestValid(t *testing.T) { 1287 // var err error 1288 // errObject := struct { 1289 // Map M `json:"map" valid:"map,r|s,l:0;"` 1290 // MapArray MArray `json:"map_array" valid:"map_array,r|s,l:0;"` 1291 // }{} 1292 // err = attrvalid.Valid(&errObject, "#all", "") 1293 // if err == nil { 1294 // t.Error(err) 1295 // return 1296 // } 1297 // ok1Object := struct { 1298 // Map M `json:"map" valid:"map,r|s,l:0;"` 1299 // MapArray MArray `json:"map_array" valid:"map_array,r|s,l:0;"` 1300 // Time Time `json:"time" valid:"time,r|i,r:0;"` 1301 // }{ 1302 // Map: M{}, 1303 // MapArray: MArray{M{}}, 1304 // Time: TimeNow(), 1305 // } 1306 // err = attrvalid.Valid(&ok1Object, "#all", "") 1307 // if err != nil { 1308 // t.Error(err) 1309 // return 1310 // } 1311 // ok2Object := struct { 1312 // TID int64 `json:"tid" valid:"tid,r|i,r:0;"` 1313 // Time Time `json:"time" valid:"time,r|i,r:0;"` 1314 // }{ 1315 // Time: TimeZero(), 1316 // } 1317 // err = attrvalid.Valid(&ok2Object, "", "") 1318 // if err != nil { 1319 // t.Error(err) 1320 // return 1321 // } 1322 // } 1323 1324 // func TestAs(t *testing.T) { 1325 // if len(AsIntArray([]int{1})) != 1 { 1326 // t.Error("eror") 1327 // } 1328 // if len(AsIntPtrArray([]int{1})) != 1 { 1329 // t.Error("eror") 1330 // } 1331 // func() { 1332 // defer func() { 1333 // recover() 1334 // }() 1335 // AsIntArray("xxx") 1336 // }() 1337 // if len(AsInt64Array([]int{1})) != 1 { 1338 // t.Error("eror") 1339 // } 1340 // if len(AsInt64PtrArray([]int{1})) != 1 { 1341 // t.Error("eror") 1342 // } 1343 // func() { 1344 // defer func() { 1345 // recover() 1346 // }() 1347 // AsInt64Array("xxx") 1348 // }() 1349 // if len(AsFloat64Array([]int{1})) != 1 { 1350 // t.Error("eror") 1351 // } 1352 // if len(AsFloat64PtrArray([]int{1})) != 1 { 1353 // t.Error("eror") 1354 // } 1355 // func() { 1356 // defer func() { 1357 // recover() 1358 // }() 1359 // AsFloat64Array("xxx") 1360 // }() 1361 // if len(AsStringArray([]int{1})) != 1 { 1362 // t.Error("eror") 1363 // } 1364 // if len(AsStringPtrArray([]int{1})) != 1 { 1365 // t.Error("eror") 1366 // } 1367 // func() { 1368 // defer func() { 1369 // recover() 1370 // }() 1371 // AsStringArray(nil) 1372 // }() 1373 // }