(t *testing.T)
| 576 | } |
| 577 | |
| 578 | func Test_Model_AllAndCount(t *testing.T) { |
| 579 | table := createInitTable() |
| 580 | defer dropTable(table) |
| 581 | tableName2 := "user_" + gtime.Now().TimestampNanoStr() |
| 582 | if _, err := db.Exec(ctx, fmt.Sprintf(` |
| 583 | CREATE TABLE %s ( |
| 584 | id INTEGER PRIMARY KEY AUTOINCREMENT |
| 585 | UNIQUE |
| 586 | NOT NULL, |
| 587 | name varchar(45) NULL, |
| 588 | age int(10) |
| 589 | ); |
| 590 | `, tableName2, |
| 591 | )); err != nil { |
| 592 | gtest.AssertNil(err) |
| 593 | } |
| 594 | defer dropTable(tableName2) |
| 595 | r, err := db.Insert(ctx, tableName2, g.Map{ |
| 596 | "id": 1, |
| 597 | "name": "table2_1", |
| 598 | "age": 18, |
| 599 | }) |
| 600 | gtest.AssertNil(err) |
| 601 | n, _ := r.RowsAffected() |
| 602 | gtest.Assert(n, 1) |
| 603 | |
| 604 | // AllAndCount with all data |
| 605 | gtest.C(t, func(t *gtest.T) { |
| 606 | result, count, err := db.Model(table).AllAndCount(false) |
| 607 | t.AssertNil(err) |
| 608 | t.Assert(len(result), TableSize) |
| 609 | t.Assert(count, TableSize) |
| 610 | }) |
| 611 | // AllAndCount with no data |
| 612 | gtest.C(t, func(t *gtest.T) { |
| 613 | result, count, err := db.Model(table).Where("id<0").AllAndCount(false) |
| 614 | t.Assert(result, nil) |
| 615 | t.AssertNil(err) |
| 616 | t.Assert(count, 0) |
| 617 | }) |
| 618 | // AllAndCount with page |
| 619 | gtest.C(t, func(t *gtest.T) { |
| 620 | result, count, err := db.Model(table).Page(1, 5).AllAndCount(false) |
| 621 | t.AssertNil(err) |
| 622 | t.Assert(len(result), 5) |
| 623 | t.Assert(count, TableSize) |
| 624 | }) |
| 625 | // AllAndCount with normal result |
| 626 | gtest.C(t, func(t *gtest.T) { |
| 627 | result, count, err := db.Model(table).Where("id=?", 1).AllAndCount(false) |
| 628 | t.AssertNil(err) |
| 629 | t.Assert(count, 1) |
| 630 | t.Assert(result[0]["id"], 1) |
| 631 | t.Assert(result[0]["nickname"], "name_1") |
| 632 | t.Assert(result[0]["passport"], "user_1") |
| 633 | }) |
| 634 | // AllAndCount with distinct |
| 635 | gtest.C(t, func(t *gtest.T) { |
nothing calls this directly
no test coverage detected
searching dependent graphs…