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

Function Test_Model_Fields

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

Source from the content-addressed store, hash-verified

675}
676
677func Test_Model_Fields(t *testing.T) {
678 tableName1 := createInitTable()
679 defer dropTable(tableName1)
680
681 tableName2 := "user_" + gtime.Now().TimestampNanoStr()
682 if _, err := db.Exec(ctx, fmt.Sprintf(`
683 CREATE TABLE %s (
684 id INTEGER PRIMARY KEY AUTOINCREMENT
685 UNIQUE
686 NOT NULL,
687 name varchar(45) NULL,
688 age int(10)
689 );
690 `, tableName2,
691 )); err != nil {
692 gtest.AssertNil(err)
693 }
694 defer dropTable(tableName2)
695
696 r, err := db.Insert(ctx, tableName2, g.Map{
697 "id": 1,
698 "name": "table2_1",
699 "age": 18,
700 })
701 gtest.AssertNil(err)
702 n, _ := r.RowsAffected()
703 gtest.Assert(n, 1)
704
705 gtest.C(t, func(t *gtest.T) {
706 all, err := db.Model(tableName1).As("u").Fields("u.passport,u.id").Where("u.id<2").All()
707 t.AssertNil(err)
708 t.Assert(len(all), 1)
709 t.Assert(len(all[0]), 2)
710 })
711 gtest.C(t, func(t *gtest.T) {
712 all, err := db.Model(tableName1).As("u1").
713 LeftJoin(tableName1, "u2", "u2.id=u1.id").
714 Fields("u1.passport,u1.id,u2.id AS u2id").
715 Where("u1.id<2").
716 All()
717 t.AssertNil(err)
718 t.Assert(len(all), 1)
719 t.Assert(len(all[0]), 3)
720 })
721 gtest.C(t, func(t *gtest.T) {
722 all, err := db.Model(tableName1).As("u1").
723 LeftJoin(tableName2, "u2", "u2.id=u1.id").
724 Fields("u1.passport,u1.id,u2.name,u2.age").
725 Where("u1.id<2").
726 All()
727 t.AssertNil(err)
728 t.Assert(len(all), 1)
729 t.Assert(len(all[0]), 4)
730 t.Assert(all[0]["id"], 1)
731 t.Assert(all[0]["age"], 18)
732 t.Assert(all[0]["name"], "table2_1")
733 t.Assert(all[0]["passport"], "user_1")
734 })

Callers

nothing calls this directly

Calls 15

NowFunction · 0.92
AssertNilFunction · 0.92
AssertFunction · 0.92
CFunction · 0.92
TimestampNanoStrMethod · 0.80
AllMethod · 0.80
FieldsMethod · 0.80
AsMethod · 0.80
AssertNilMethod · 0.80
AssertMethod · 0.80
LeftJoinMethod · 0.80
createInitTableFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…