Skip to content

Commit 455830b

Browse files
authored
test(database/gdb): add more unit testing cases for Raw feature (#3962)
1 parent e56371e commit 455830b

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

contrib/drivers/mysql/mysql_z_unit_feature_raw_type_test.go

+46-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
package mysql_test
88

99
import (
10+
"context"
1011
"testing"
1112

1213
"github.com/gogf/gf/v2/database/gdb"
1314
"github.com/gogf/gf/v2/frame/g"
1415
"github.com/gogf/gf/v2/test/gtest"
1516
)
1617

17-
func Test_Insert_Raw(t *testing.T) {
18+
func Test_Raw_Insert(t *testing.T) {
1819
table := createTable()
1920
defer dropTable(table)
2021

@@ -33,7 +34,7 @@ func Test_Insert_Raw(t *testing.T) {
3334
})
3435
}
3536

36-
func Test_BatchInsert_Raw(t *testing.T) {
37+
func Test_Raw_BatchInsert(t *testing.T) {
3738
table := createTable()
3839
defer dropTable(table)
3940

@@ -63,7 +64,7 @@ func Test_BatchInsert_Raw(t *testing.T) {
6364
})
6465
}
6566

66-
func Test_Update_Raw(t *testing.T) {
67+
func Test_Raw_Update(t *testing.T) {
6768
table := createInitTable()
6869
defer dropTable(table)
6970

@@ -84,3 +85,45 @@ func Test_Update_Raw(t *testing.T) {
8485
t.Assert(n, 1)
8586
})
8687
}
88+
89+
func Test_Raw_Where(t *testing.T) {
90+
table1 := createTable("Test_Raw_Where_Table1")
91+
table2 := createTable("Test_Raw_Where_Table2")
92+
defer dropTable(table1)
93+
defer dropTable(table2)
94+
95+
// https://github.com/gogf/gf/issues/3922
96+
gtest.C(t, func(t *gtest.T) {
97+
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"
98+
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
99+
s := db.Model(table2).As("B").Ctx(ctx).Fields("B.id").Where("B.id", gdb.Raw("A.id"))
100+
m := db.Model(table1).As("A").Ctx(ctx).Where("NOT EXISTS ?", s).Limit(1)
101+
_, err := m.All()
102+
return err
103+
})
104+
t.AssertNil(err)
105+
t.Assert(expectSql, sql)
106+
})
107+
gtest.C(t, func(t *gtest.T) {
108+
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"
109+
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
110+
s := db.Model(table2).As("B").Ctx(ctx).Fields("B.id").Where(gdb.Raw("B.id=A.id"))
111+
m := db.Model(table1).As("A").Ctx(ctx).Where("NOT EXISTS ?", s).Limit(1)
112+
_, err := m.All()
113+
return err
114+
})
115+
t.AssertNil(err)
116+
t.Assert(expectSql, sql)
117+
})
118+
// https://github.com/gogf/gf/issues/3915
119+
gtest.C(t, func(t *gtest.T) {
120+
expectSql := "SELECT * FROM `Test_Raw_Where_Table1` WHERE `passport` < `nickname`"
121+
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
122+
m := db.Model(table1).Ctx(ctx).WhereLT("passport", gdb.Raw("`nickname`"))
123+
_, err := m.All()
124+
return err
125+
})
126+
t.AssertNil(err)
127+
t.Assert(expectSql, sql)
128+
})
129+
}

database/gdb/gdb_model_select.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,12 @@ func (m *Model) Scan(pointer interface{}, where ...interface{}) error {
283283
}
284284
}
285285

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

0 commit comments

Comments
 (0)