github.com/zhongdalu/gf@v1.0.0/g/database/gdb/gdb_unit_z_model_test.go (about) 1 // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package gdb_test 8 9 import ( 10 "database/sql" 11 "fmt" 12 "testing" 13 14 "github.com/zhongdalu/gf/g" 15 "github.com/zhongdalu/gf/g/os/gtime" 16 "github.com/zhongdalu/gf/g/test/gtest" 17 ) 18 19 func Test_Model_Insert(t *testing.T) { 20 table := createTable() 21 defer dropTable(table) 22 gtest.Case(t, func() { 23 result, err := db.Table(table).Filter().Data(g.Map{ 24 "id": 1, 25 "uid": 1, 26 "passport": "t1", 27 "password": "25d55ad283aa400af464c76d713c07ad", 28 "nickname": "name_1", 29 "create_time": gtime.Now().String(), 30 }).Insert() 31 gtest.Assert(err, nil) 32 n, _ := result.LastInsertId() 33 gtest.Assert(n, 1) 34 35 result, err = db.Table(table).Filter().Data(g.Map{ 36 "id": "2", 37 "uid": "2", 38 "passport": "t2", 39 "password": "25d55ad283aa400af464c76d713c07ad", 40 "nickname": "name_2", 41 "create_time": gtime.Now().String(), 42 }).Insert() 43 gtest.Assert(err, nil) 44 n, _ = result.RowsAffected() 45 gtest.Assert(n, 1) 46 47 type User struct { 48 Id int `gconv:"id"` 49 Uid int `gconv:"uid"` 50 Passport string `json:"passport"` 51 Password string `gconv:"password"` 52 Nickname string `gconv:"nickname"` 53 CreateTime string `json:"create_time"` 54 } 55 result, err = db.Table(table).Filter().Data(User{ 56 Id: 3, 57 Uid: 3, 58 Passport: "t3", 59 Password: "25d55ad283aa400af464c76d713c07ad", 60 Nickname: "name_3", 61 CreateTime: gtime.Now().String(), 62 }).Insert() 63 gtest.Assert(err, nil) 64 n, _ = result.RowsAffected() 65 gtest.Assert(n, 1) 66 value, err := db.Table(table).Fields("passport").Where("id=3").Value() 67 gtest.Assert(err, nil) 68 gtest.Assert(value.String(), "t3") 69 70 result, err = db.Table(table).Filter().Data(&User{ 71 Id: 4, 72 Uid: 4, 73 Passport: "t4", 74 Password: "25d55ad283aa400af464c76d713c07ad", 75 Nickname: "T4", 76 CreateTime: gtime.Now().String(), 77 }).Insert() 78 gtest.Assert(err, nil) 79 n, _ = result.RowsAffected() 80 gtest.Assert(n, 1) 81 value, err = db.Table(table).Fields("passport").Where("id=4").Value() 82 gtest.Assert(err, nil) 83 gtest.Assert(value.String(), "t4") 84 85 result, err = db.Table(table).Where("id>?", 1).Delete() 86 gtest.Assert(err, nil) 87 n, _ = result.RowsAffected() 88 gtest.Assert(n, 3) 89 }) 90 91 } 92 93 func Test_Model_Batch(t *testing.T) { 94 // bacth insert 95 gtest.Case(t, func() { 96 table := createTable() 97 defer dropTable(table) 98 result, err := db.Table(table).Filter().Data(g.List{ 99 { 100 "id": 2, 101 "uid": 2, 102 "passport": "t2", 103 "password": "25d55ad283aa400af464c76d713c07ad", 104 "nickname": "name_2", 105 "create_time": gtime.Now().String(), 106 }, 107 { 108 "id": 3, 109 "uid": 3, 110 "passport": "t3", 111 "password": "25d55ad283aa400af464c76d713c07ad", 112 "nickname": "name_3", 113 "create_time": gtime.Now().String(), 114 }, 115 }).Batch(1).Insert() 116 if err != nil { 117 gtest.Error(err) 118 } 119 n, _ := result.RowsAffected() 120 gtest.Assert(n, 2) 121 }) 122 123 // batch save 124 gtest.Case(t, func() { 125 table := createInitTable() 126 defer dropTable(table) 127 result, err := db.Table(table).All() 128 gtest.Assert(err, nil) 129 gtest.Assert(len(result), INIT_DATA_SIZE) 130 for _, v := range result { 131 v["nickname"].Set(v["nickname"].String() + v["id"].String()) 132 } 133 r, e := db.Table(table).Data(result).Save() 134 gtest.Assert(e, nil) 135 n, e := r.RowsAffected() 136 gtest.Assert(e, nil) 137 gtest.Assert(n, INIT_DATA_SIZE*2) 138 }) 139 140 // batch replace 141 gtest.Case(t, func() { 142 table := createInitTable() 143 defer dropTable(table) 144 result, err := db.Table(table).All() 145 gtest.Assert(err, nil) 146 gtest.Assert(len(result), INIT_DATA_SIZE) 147 for _, v := range result { 148 v["nickname"].Set(v["nickname"].String() + v["id"].String()) 149 } 150 r, e := db.Table(table).Data(result).Replace() 151 gtest.Assert(e, nil) 152 n, e := r.RowsAffected() 153 gtest.Assert(e, nil) 154 gtest.Assert(n, INIT_DATA_SIZE*2) 155 }) 156 } 157 158 func Test_Model_Replace(t *testing.T) { 159 table := createTable() 160 defer dropTable(table) 161 162 gtest.Case(t, func() { 163 result, err := db.Table(table).Data(g.Map{ 164 "id": 1, 165 "passport": "t11", 166 "password": "25d55ad283aa400af464c76d713c07ad", 167 "nickname": "T11", 168 "create_time": "2018-10-24 10:00:00", 169 }).Replace() 170 gtest.Assert(err, nil) 171 n, _ := result.RowsAffected() 172 gtest.Assert(n, 1) 173 }) 174 } 175 176 func Test_Model_Save(t *testing.T) { 177 table := createTable() 178 defer dropTable(table) 179 gtest.Case(t, func() { 180 result, err := db.Table(table).Data(g.Map{ 181 "id": 1, 182 "passport": "t111", 183 "password": "25d55ad283aa400af464c76d713c07ad", 184 "nickname": "T111", 185 "create_time": "2018-10-24 10:00:00", 186 }).Save() 187 gtest.Assert(err, nil) 188 n, _ := result.RowsAffected() 189 gtest.Assert(n, 1) 190 }) 191 } 192 193 func Test_Model_Update(t *testing.T) { 194 table := createInitTable() 195 defer dropTable(table) 196 // UPDATE...LIMIT 197 gtest.Case(t, func() { 198 result, err := db.Table(table).Data("nickname", "T100").OrderBy("id desc").Limit(2).Update() 199 gtest.Assert(err, nil) 200 n, _ := result.RowsAffected() 201 gtest.Assert(n, 2) 202 203 v1, err := db.Table(table).Fields("nickname").Where("id", 10).Value() 204 gtest.Assert(err, nil) 205 gtest.Assert(v1.String(), "T100") 206 207 v2, err := db.Table(table).Fields("nickname").Where("id", 8).Value() 208 gtest.Assert(err, nil) 209 gtest.Assert(v2.String(), "name_8") 210 }) 211 212 gtest.Case(t, func() { 213 result, err := db.Table(table).Data("passport", "user_22").Where("passport=?", "user_2").Update() 214 gtest.Assert(err, nil) 215 n, _ := result.RowsAffected() 216 gtest.Assert(n, 1) 217 }) 218 219 gtest.Case(t, func() { 220 result, err := db.Table(table).Data("passport", "user_2").Where("passport='user_22'").Update() 221 gtest.Assert(err, nil) 222 n, _ := result.RowsAffected() 223 gtest.Assert(n, 1) 224 }) 225 } 226 227 func Test_Model_Clone(t *testing.T) { 228 table := createInitTable() 229 defer dropTable(table) 230 231 gtest.Case(t, func() { 232 md := db.Table(table).Where("id IN(?)", g.Slice{1, 3}) 233 count, err := md.Count() 234 gtest.Assert(err, nil) 235 236 record, err := md.OrderBy("id DESC").One() 237 gtest.Assert(err, nil) 238 239 result, err := md.OrderBy("id ASC").All() 240 gtest.Assert(err, nil) 241 242 gtest.Assert(count, 2) 243 gtest.Assert(record["id"].Int(), 3) 244 gtest.Assert(len(result), 2) 245 gtest.Assert(result[0]["id"].Int(), 1) 246 gtest.Assert(result[1]["id"].Int(), 3) 247 }) 248 } 249 250 func Test_Model_Safe(t *testing.T) { 251 table := createInitTable() 252 defer dropTable(table) 253 254 gtest.Case(t, func() { 255 md := db.Table(table).Safe(false).Where("id IN(?)", g.Slice{1, 3}) 256 count, err := md.Count() 257 gtest.Assert(err, nil) 258 gtest.Assert(count, 2) 259 260 md.And("id = ?", 1) 261 count, err = md.Count() 262 gtest.Assert(err, nil) 263 gtest.Assert(count, 1) 264 }) 265 gtest.Case(t, func() { 266 md := db.Table(table).Safe(true).Where("id IN(?)", g.Slice{1, 3}) 267 count, err := md.Count() 268 gtest.Assert(err, nil) 269 gtest.Assert(count, 2) 270 271 md.And("id = ?", 1) 272 count, err = md.Count() 273 gtest.Assert(err, nil) 274 gtest.Assert(count, 2) 275 }) 276 } 277 278 func Test_Model_All(t *testing.T) { 279 table := createInitTable() 280 defer dropTable(table) 281 282 gtest.Case(t, func() { 283 result, err := db.Table(table).All() 284 gtest.Assert(err, nil) 285 gtest.Assert(len(result), INIT_DATA_SIZE) 286 }) 287 // sql.ErrNoRows 288 gtest.Case(t, func() { 289 result, err := db.Table(table).Where("id<0").All() 290 gtest.Assert(result, nil) 291 gtest.Assert(err, sql.ErrNoRows) 292 }) 293 } 294 295 func Test_Model_One(t *testing.T) { 296 table := createInitTable() 297 defer dropTable(table) 298 gtest.Case(t, func() { 299 record, err := db.Table(table).Where("id", 1).One() 300 gtest.Assert(err, nil) 301 gtest.Assert(record["nickname"].String(), "name_1") 302 }) 303 // sql.ErrNoRows 304 gtest.Case(t, func() { 305 record, err := db.Table(table).Where("id", 0).One() 306 gtest.Assert(err, sql.ErrNoRows) 307 gtest.Assert(record, nil) 308 }) 309 } 310 311 func Test_Model_Value(t *testing.T) { 312 table := createInitTable() 313 defer dropTable(table) 314 gtest.Case(t, func() { 315 value, err := db.Table(table).Fields("nickname").Where("id", 1).Value() 316 gtest.Assert(err, nil) 317 gtest.Assert(value.String(), "name_1") 318 }) 319 // sql.ErrNoRows 320 gtest.Case(t, func() { 321 value, err := db.Table(table).Fields("nickname").Where("id", 0).Value() 322 gtest.Assert(err, sql.ErrNoRows) 323 gtest.Assert(value, nil) 324 }) 325 } 326 327 func Test_Model_Count(t *testing.T) { 328 table := createInitTable() 329 defer dropTable(table) 330 gtest.Case(t, func() { 331 count, err := db.Table(table).Count() 332 gtest.Assert(err, nil) 333 gtest.Assert(count, INIT_DATA_SIZE) 334 }) 335 } 336 337 func Test_Model_Select(t *testing.T) { 338 table := createInitTable() 339 defer dropTable(table) 340 gtest.Case(t, func() { 341 result, err := db.Table(table).Select() 342 gtest.Assert(err, nil) 343 gtest.Assert(len(result), INIT_DATA_SIZE) 344 }) 345 } 346 347 func Test_Model_Struct(t *testing.T) { 348 table := createInitTable() 349 defer dropTable(table) 350 gtest.Case(t, func() { 351 type User struct { 352 Id int 353 Passport string 354 Password string 355 NickName string 356 CreateTime gtime.Time 357 } 358 user := new(User) 359 err := db.Table(table).Where("id=1").Struct(user) 360 gtest.Assert(err, nil) 361 gtest.Assert(user.NickName, "name_1") 362 gtest.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") 363 }) 364 gtest.Case(t, func() { 365 type User struct { 366 Id int 367 Passport string 368 Password string 369 NickName string 370 CreateTime *gtime.Time 371 } 372 user := new(User) 373 err := db.Table(table).Where("id=1").Struct(user) 374 gtest.Assert(err, nil) 375 gtest.Assert(user.NickName, "name_1") 376 gtest.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") 377 }) 378 // Auto creating struct object. 379 gtest.Case(t, func() { 380 type User struct { 381 Id int 382 Passport string 383 Password string 384 NickName string 385 CreateTime *gtime.Time 386 } 387 user := (*User)(nil) 388 err := db.Table(table).Where("id=1").Struct(&user) 389 gtest.Assert(err, nil) 390 gtest.Assert(user.NickName, "name_1") 391 gtest.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") 392 }) 393 // Just using Scan. 394 gtest.Case(t, func() { 395 type User struct { 396 Id int 397 Passport string 398 Password string 399 NickName string 400 CreateTime *gtime.Time 401 } 402 user := (*User)(nil) 403 err := db.Table(table).Where("id=1").Scan(&user) 404 if err != nil { 405 gtest.Error(err) 406 } 407 gtest.Assert(user.NickName, "name_1") 408 gtest.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") 409 }) 410 // sql.ErrNoRows 411 gtest.Case(t, func() { 412 type User struct { 413 Id int 414 Passport string 415 Password string 416 NickName string 417 CreateTime *gtime.Time 418 } 419 user := new(User) 420 err := db.Table(table).Where("id=-1").Struct(user) 421 gtest.Assert(err, sql.ErrNoRows) 422 }) 423 } 424 425 func Test_Model_Structs(t *testing.T) { 426 table := createInitTable() 427 defer dropTable(table) 428 429 gtest.Case(t, func() { 430 type User struct { 431 Id int 432 Passport string 433 Password string 434 NickName string 435 CreateTime gtime.Time 436 } 437 var users []User 438 err := db.Table(table).OrderBy("id asc").Structs(&users) 439 if err != nil { 440 gtest.Error(err) 441 } 442 gtest.Assert(len(users), INIT_DATA_SIZE) 443 gtest.Assert(users[0].Id, 1) 444 gtest.Assert(users[1].Id, 2) 445 gtest.Assert(users[2].Id, 3) 446 gtest.Assert(users[0].NickName, "name_1") 447 gtest.Assert(users[1].NickName, "name_2") 448 gtest.Assert(users[2].NickName, "name_3") 449 gtest.Assert(users[0].CreateTime.String(), "2018-10-24 10:00:00") 450 }) 451 // Auto create struct slice. 452 gtest.Case(t, func() { 453 type User struct { 454 Id int 455 Passport string 456 Password string 457 NickName string 458 CreateTime *gtime.Time 459 } 460 var users []*User 461 err := db.Table(table).OrderBy("id asc").Structs(&users) 462 if err != nil { 463 gtest.Error(err) 464 } 465 gtest.Assert(len(users), INIT_DATA_SIZE) 466 gtest.Assert(users[0].Id, 1) 467 gtest.Assert(users[1].Id, 2) 468 gtest.Assert(users[2].Id, 3) 469 gtest.Assert(users[0].NickName, "name_1") 470 gtest.Assert(users[1].NickName, "name_2") 471 gtest.Assert(users[2].NickName, "name_3") 472 gtest.Assert(users[0].CreateTime.String(), "2018-10-24 10:00:00") 473 }) 474 // Just using Scan. 475 gtest.Case(t, func() { 476 type User struct { 477 Id int 478 Passport string 479 Password string 480 NickName string 481 CreateTime *gtime.Time 482 } 483 var users []*User 484 err := db.Table(table).OrderBy("id asc").Scan(&users) 485 if err != nil { 486 gtest.Error(err) 487 } 488 gtest.Assert(len(users), INIT_DATA_SIZE) 489 gtest.Assert(users[0].Id, 1) 490 gtest.Assert(users[1].Id, 2) 491 gtest.Assert(users[2].Id, 3) 492 gtest.Assert(users[0].NickName, "name_1") 493 gtest.Assert(users[1].NickName, "name_2") 494 gtest.Assert(users[2].NickName, "name_3") 495 gtest.Assert(users[0].CreateTime.String(), "2018-10-24 10:00:00") 496 }) 497 // sql.ErrNoRows 498 gtest.Case(t, func() { 499 type User struct { 500 Id int 501 Passport string 502 Password string 503 NickName string 504 CreateTime *gtime.Time 505 } 506 var users []*User 507 err := db.Table(table).Where("id<0").Structs(&users) 508 gtest.Assert(err, sql.ErrNoRows) 509 }) 510 } 511 512 func Test_Model_Scan(t *testing.T) { 513 table := createInitTable() 514 defer dropTable(table) 515 516 gtest.Case(t, func() { 517 type User struct { 518 Id int 519 Passport string 520 Password string 521 NickName string 522 CreateTime gtime.Time 523 } 524 user := new(User) 525 err := db.Table(table).Where("id=1").Scan(user) 526 gtest.Assert(err, nil) 527 gtest.Assert(user.NickName, "name_1") 528 gtest.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") 529 }) 530 gtest.Case(t, func() { 531 type User struct { 532 Id int 533 Passport string 534 Password string 535 NickName string 536 CreateTime *gtime.Time 537 } 538 user := new(User) 539 err := db.Table(table).Where("id=1").Scan(user) 540 gtest.Assert(err, nil) 541 gtest.Assert(user.NickName, "name_1") 542 gtest.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") 543 }) 544 gtest.Case(t, func() { 545 type User struct { 546 Id int 547 Passport string 548 Password string 549 NickName string 550 CreateTime gtime.Time 551 } 552 var users []User 553 err := db.Table(table).OrderBy("id asc").Scan(&users) 554 gtest.Assert(err, nil) 555 gtest.Assert(len(users), INIT_DATA_SIZE) 556 gtest.Assert(users[0].Id, 1) 557 gtest.Assert(users[1].Id, 2) 558 gtest.Assert(users[2].Id, 3) 559 gtest.Assert(users[0].NickName, "name_1") 560 gtest.Assert(users[1].NickName, "name_2") 561 gtest.Assert(users[2].NickName, "name_3") 562 gtest.Assert(users[0].CreateTime.String(), "2018-10-24 10:00:00") 563 }) 564 gtest.Case(t, func() { 565 type User struct { 566 Id int 567 Passport string 568 Password string 569 NickName string 570 CreateTime *gtime.Time 571 } 572 var users []*User 573 err := db.Table(table).OrderBy("id asc").Scan(&users) 574 gtest.Assert(err, nil) 575 gtest.Assert(len(users), INIT_DATA_SIZE) 576 gtest.Assert(users[0].Id, 1) 577 gtest.Assert(users[1].Id, 2) 578 gtest.Assert(users[2].Id, 3) 579 gtest.Assert(users[0].NickName, "name_1") 580 gtest.Assert(users[1].NickName, "name_2") 581 gtest.Assert(users[2].NickName, "name_3") 582 gtest.Assert(users[0].CreateTime.String(), "2018-10-24 10:00:00") 583 }) 584 // sql.ErrNoRows 585 gtest.Case(t, func() { 586 type User struct { 587 Id int 588 Passport string 589 Password string 590 NickName string 591 CreateTime *gtime.Time 592 } 593 user := new(User) 594 users := new([]*User) 595 err1 := db.Table(table).Where("id < 0").Scan(user) 596 err2 := db.Table(table).Where("id < 0").Scan(users) 597 gtest.Assert(err1, sql.ErrNoRows) 598 gtest.Assert(err2, sql.ErrNoRows) 599 }) 600 } 601 602 func Test_Model_OrderBy(t *testing.T) { 603 table := createInitTable() 604 defer dropTable(table) 605 606 gtest.Case(t, func() { 607 result, err := db.Table(table).OrderBy("id DESC").Select() 608 gtest.Assert(err, nil) 609 gtest.Assert(len(result), INIT_DATA_SIZE) 610 gtest.Assert(result[0]["nickname"].String(), fmt.Sprintf("name_%d", INIT_DATA_SIZE)) 611 }) 612 } 613 614 func Test_Model_GroupBy(t *testing.T) { 615 table := createInitTable() 616 defer dropTable(table) 617 618 gtest.Case(t, func() { 619 result, err := db.Table(table).GroupBy("id").Select() 620 gtest.Assert(err, nil) 621 gtest.Assert(len(result), INIT_DATA_SIZE) 622 gtest.Assert(result[0]["nickname"].String(), "name_1") 623 }) 624 } 625 626 func Test_Model_Where(t *testing.T) { 627 table := createInitTable() 628 defer dropTable(table) 629 630 // string 631 gtest.Case(t, func() { 632 result, err := db.Table(table).Where("id=? and nickname=?", 3, "name_3").One() 633 gtest.Assert(err, nil) 634 gtest.AssertGT(len(result), 0) 635 gtest.Assert(result["id"].Int(), 3) 636 }) 637 // slice parameter 638 gtest.Case(t, func() { 639 result, err := db.Table(table).Where("id=? and nickname=?", g.Slice{3, "name_3"}).One() 640 gtest.Assert(err, nil) 641 gtest.AssertGT(len(result), 0) 642 gtest.Assert(result["id"].Int(), 3) 643 }) 644 gtest.Case(t, func() { 645 result, err := db.Table(table).Where("id=3", g.Slice{}).One() 646 gtest.Assert(err, nil) 647 gtest.AssertGT(len(result), 0) 648 gtest.Assert(result["id"].Int(), 3) 649 }) 650 gtest.Case(t, func() { 651 result, err := db.Table(table).Where("id=?", g.Slice{3}).One() 652 gtest.Assert(err, nil) 653 gtest.AssertGT(len(result), 0) 654 gtest.Assert(result["id"].Int(), 3) 655 }) 656 gtest.Case(t, func() { 657 result, err := db.Table(table).Where("id", 3).One() 658 gtest.Assert(err, nil) 659 gtest.AssertGT(len(result), 0) 660 gtest.Assert(result["id"].Int(), 3) 661 }) 662 gtest.Case(t, func() { 663 result, err := db.Table(table).Where("id", 3).Where("nickname", "name_3").One() 664 gtest.Assert(err, nil) 665 gtest.Assert(result["id"].Int(), 3) 666 }) 667 gtest.Case(t, func() { 668 result, err := db.Table(table).Where("id", 3).And("nickname", "name_3").One() 669 gtest.Assert(err, nil) 670 gtest.Assert(result["id"].Int(), 3) 671 }) 672 gtest.Case(t, func() { 673 result, err := db.Table(table).Where("id", 30).Or("nickname", "name_3").One() 674 gtest.Assert(err, nil) 675 gtest.Assert(result["id"].Int(), 3) 676 }) 677 gtest.Case(t, func() { 678 result, err := db.Table(table).Where("id", 30).Or("nickname", "name_3").And("id>?", 1).One() 679 gtest.Assert(err, nil) 680 gtest.Assert(result["id"].Int(), 3) 681 }) 682 gtest.Case(t, func() { 683 result, err := db.Table(table).Where("id", 30).Or("nickname", "name_3").And("id>", 1).One() 684 gtest.Assert(err, nil) 685 gtest.Assert(result["id"].Int(), 3) 686 }) 687 // slice 688 gtest.Case(t, func() { 689 result, err := db.Table(table).Where("id=? AND nickname=?", g.Slice{3, "name_3"}...).One() 690 gtest.Assert(err, nil) 691 gtest.Assert(result["id"].Int(), 3) 692 }) 693 gtest.Case(t, func() { 694 result, err := db.Table(table).Where("id=? AND nickname=?", g.Slice{3, "name_3"}).One() 695 gtest.Assert(err, nil) 696 gtest.Assert(result["id"].Int(), 3) 697 }) 698 gtest.Case(t, func() { 699 result, err := db.Table(table).Where("passport like ? and nickname like ?", g.Slice{"user_3", "name_3"}).One() 700 gtest.Assert(err, nil) 701 gtest.Assert(result["id"].Int(), 3) 702 }) 703 // map 704 gtest.Case(t, func() { 705 result, err := db.Table(table).Where(g.Map{"id": 3, "nickname": "name_3"}).One() 706 gtest.Assert(err, nil) 707 gtest.Assert(result["id"].Int(), 3) 708 }) 709 // map key operator 710 gtest.Case(t, func() { 711 result, err := db.Table(table).Where(g.Map{"id>": 1, "id<": 3}).One() 712 gtest.Assert(err, nil) 713 gtest.Assert(result["id"].Int(), 2) 714 }) 715 // complicated where 1 716 gtest.Case(t, func() { 717 //db.SetDebug(true) 718 conditions := g.Map{ 719 "nickname like ?": "%name%", 720 "id between ? and ?": g.Slice{1, 3}, 721 "id > 0": nil, 722 "create_time > 0": nil, 723 "id": g.Slice{1, 2, 3}, 724 } 725 result, err := db.Table(table).Where(conditions).OrderBy("id asc").All() 726 gtest.Assert(err, nil) 727 gtest.Assert(len(result), 3) 728 gtest.Assert(result[0]["id"].Int(), 1) 729 }) 730 // complicated where 2 731 gtest.Case(t, func() { 732 //db.SetDebug(true) 733 conditions := g.Map{ 734 "nickname like ?": "%name%", 735 "id between ? and ?": g.Slice{1, 3}, 736 "id >= ?": 1, 737 "create_time > ?": 0, 738 "id in(?)": g.Slice{1, 2, 3}, 739 } 740 result, err := db.Table(table).Where(conditions).OrderBy("id asc").All() 741 gtest.Assert(err, nil) 742 gtest.Assert(len(result), 3) 743 gtest.Assert(result[0]["id"].Int(), 1) 744 }) 745 // struct 746 gtest.Case(t, func() { 747 type User struct { 748 Id int `json:"id"` 749 Nickname string `gconv:"nickname"` 750 } 751 result, err := db.Table(table).Where(User{3, "name_3"}).One() 752 gtest.Assert(err, nil) 753 gtest.Assert(result["id"].Int(), 3) 754 755 result, err = db.Table(table).Where(&User{3, "name_3"}).One() 756 gtest.Assert(err, nil) 757 gtest.Assert(result["id"].Int(), 3) 758 }) 759 // slice single 760 gtest.Case(t, func() { 761 result, err := db.Table(table).Where("id IN(?)", g.Slice{1, 3}).OrderBy("id ASC").All() 762 gtest.Assert(err, nil) 763 gtest.Assert(len(result), 2) 764 gtest.Assert(result[0]["id"].Int(), 1) 765 gtest.Assert(result[1]["id"].Int(), 3) 766 }) 767 // slice + string 768 gtest.Case(t, func() { 769 result, err := db.Table(table).Where("nickname=? AND id IN(?)", "name_3", g.Slice{1, 3}).OrderBy("id ASC").All() 770 gtest.Assert(err, nil) 771 gtest.Assert(len(result), 1) 772 gtest.Assert(result[0]["id"].Int(), 3) 773 }) 774 // slice + map 775 gtest.Case(t, func() { 776 result, err := db.Table(table).Where(g.Map{ 777 "id": g.Slice{1, 3}, 778 "nickname": "name_3", 779 }).OrderBy("id ASC").All() 780 gtest.Assert(err, nil) 781 gtest.Assert(len(result), 1) 782 gtest.Assert(result[0]["id"].Int(), 3) 783 }) 784 // slice + struct 785 gtest.Case(t, func() { 786 type User struct { 787 Ids []int `json:"id"` 788 Nickname string `gconv:"nickname"` 789 } 790 result, err := db.Table(table).Where(User{ 791 Ids: []int{1, 3}, 792 Nickname: "name_3", 793 }).OrderBy("id ASC").All() 794 gtest.Assert(err, nil) 795 gtest.Assert(len(result), 1) 796 gtest.Assert(result[0]["id"].Int(), 3) 797 }) 798 } 799 800 func Test_Model_Delete(t *testing.T) { 801 table := createInitTable() 802 defer dropTable(table) 803 804 // DELETE...LIMIT 805 gtest.Case(t, func() { 806 result, err := db.Table(table).Limit(2).Delete() 807 gtest.Assert(err, nil) 808 n, _ := result.RowsAffected() 809 gtest.Assert(n, 2) 810 }) 811 812 gtest.Case(t, func() { 813 result, err := db.Table(table).Delete() 814 gtest.Assert(err, nil) 815 n, _ := result.RowsAffected() 816 gtest.Assert(n, INIT_DATA_SIZE-2) 817 }) 818 }