MCPcopy
hub / github.com/gogf/gf / Test_Model_AllAndCount

Function Test_Model_AllAndCount

contrib/drivers/sqlitecgo/sqlitecgo_z_unit_model_test.go:578–675  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

576}
577
578func 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) {

Callers

nothing calls this directly

Calls 15

NowFunction · 0.92
AssertNilFunction · 0.92
AssertFunction · 0.92
CFunction · 0.92
TimestampNanoStrMethod · 0.80
AllAndCountMethod · 0.80
AssertNilMethod · 0.80
AssertMethod · 0.80
PageMethod · 0.80
FieldsMethod · 0.80
LeftJoinMethod · 0.80
AsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…