Skip to content

Commit

Permalink
test(database/gdb): add more unit testing cases for Raw feature (#3962
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gqcn authored Nov 23, 2024
1 parent e56371e commit 455830b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
49 changes: 46 additions & 3 deletions contrib/drivers/mysql/mysql_z_unit_feature_raw_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
package mysql_test

import (
"context"
"testing"

"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/test/gtest"
)

func Test_Insert_Raw(t *testing.T) {
func Test_Raw_Insert(t *testing.T) {
table := createTable()
defer dropTable(table)

Expand All @@ -33,7 +34,7 @@ func Test_Insert_Raw(t *testing.T) {
})
}

func Test_BatchInsert_Raw(t *testing.T) {
func Test_Raw_BatchInsert(t *testing.T) {
table := createTable()
defer dropTable(table)

Expand Down Expand Up @@ -63,7 +64,7 @@ func Test_BatchInsert_Raw(t *testing.T) {
})
}

func Test_Update_Raw(t *testing.T) {
func Test_Raw_Update(t *testing.T) {
table := createInitTable()
defer dropTable(table)

Expand All @@ -84,3 +85,45 @@ func Test_Update_Raw(t *testing.T) {
t.Assert(n, 1)
})
}

func Test_Raw_Where(t *testing.T) {
table1 := createTable("Test_Raw_Where_Table1")
table2 := createTable("Test_Raw_Where_Table2")
defer dropTable(table1)
defer dropTable(table2)

// https://github.com/gogf/gf/issues/3922
gtest.C(t, func(t *gtest.T) {
expectSql := "SELECT * FROM `Test_Raw_Where_Table1` AS A WHERE NOT EXISTS (SELECT B.id FROM `Test_Raw_Where_Table2` AS B WHERE `B`.`id`=A.id) LIMIT 1"
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
s := db.Model(table2).As("B").Ctx(ctx).Fields("B.id").Where("B.id", gdb.Raw("A.id"))
m := db.Model(table1).As("A").Ctx(ctx).Where("NOT EXISTS ?", s).Limit(1)
_, err := m.All()
return err
})
t.AssertNil(err)
t.Assert(expectSql, sql)
})
gtest.C(t, func(t *gtest.T) {
expectSql := "SELECT * FROM `Test_Raw_Where_Table1` AS A WHERE NOT EXISTS (SELECT B.id FROM `Test_Raw_Where_Table2` AS B WHERE B.id=A.id) LIMIT 1"
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
s := db.Model(table2).As("B").Ctx(ctx).Fields("B.id").Where(gdb.Raw("B.id=A.id"))
m := db.Model(table1).As("A").Ctx(ctx).Where("NOT EXISTS ?", s).Limit(1)
_, err := m.All()
return err
})
t.AssertNil(err)
t.Assert(expectSql, sql)
})
// https://github.com/gogf/gf/issues/3915
gtest.C(t, func(t *gtest.T) {
expectSql := "SELECT * FROM `Test_Raw_Where_Table1` WHERE `passport` < `nickname`"
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
m := db.Model(table1).Ctx(ctx).WhereLT("passport", gdb.Raw("`nickname`"))
_, err := m.All()
return err
})
t.AssertNil(err)
t.Assert(expectSql, sql)
})
}
10 changes: 6 additions & 4 deletions database/gdb/gdb_model_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,12 @@ func (m *Model) Scan(pointer interface{}, where ...interface{}) error {
}
}

// ScanAndCount scans a single record or record array that matches the given conditions and counts the total number of records that match those conditions.
// If useFieldForCount is true, it will use the fields specified in the model for counting;
// The pointer parameter is a pointer to a struct that the scanned data will be stored in.
// The pointerCount parameter is a pointer to an integer that will be set to the total number of records that match the given conditions.
// ScanAndCount scans a single record or record array that matches the given conditions and counts the total number
// of records that match those conditions.
//
// If `useFieldForCount` is true, it will use the fields specified in the model for counting;
// The `pointer` parameter is a pointer to a struct that the scanned data will be stored in.
// The `totalCount` parameter is a pointer to an integer that will be set to the total number of records that match the given conditions.
// The where parameter is an optional list of conditions to use when retrieving records.
//
// Example:
Expand Down

0 comments on commit 455830b

Please sign in to comment.