mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
add more unit testing cases,remove function Filter usage in unit testing cases for package gdb
This commit is contained in:
218
database/gdb/gdb_z_mysql_filter_test.go
Normal file
218
database/gdb/gdb_z_mysql_filter_test.go
Normal file
@ -0,0 +1,218 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gdb_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
"github.com/gogf/gf/os/gtime"
|
||||
"github.com/gogf/gf/test/gtest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Using filter dose not affect the outside value inside function.
|
||||
func Test_Model_Insert_Filter(t *testing.T) {
|
||||
// map
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
data := g.Map{
|
||||
"id": 1,
|
||||
"uid": 1,
|
||||
"passport": "t1",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname": "name_1",
|
||||
"create_time": gtime.Now().String(),
|
||||
}
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 1)
|
||||
|
||||
t.Assert(data["uid"], 1)
|
||||
})
|
||||
// slice
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
data := g.List{
|
||||
g.Map{
|
||||
"id": 1,
|
||||
"uid": 1,
|
||||
"passport": "t1",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname": "name_1",
|
||||
"create_time": gtime.Now().String(),
|
||||
},
|
||||
g.Map{
|
||||
"id": 2,
|
||||
"uid": 2,
|
||||
"passport": "t1",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname": "name_1",
|
||||
"create_time": gtime.Now().String(),
|
||||
},
|
||||
}
|
||||
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 2)
|
||||
|
||||
t.Assert(data[0]["uid"], 1)
|
||||
t.Assert(data[1]["uid"], 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Embedded_Filter(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Base struct {
|
||||
Id int
|
||||
Uid int
|
||||
CreateTime string
|
||||
NoneExist string
|
||||
}
|
||||
type User struct {
|
||||
Base
|
||||
Passport string
|
||||
Password string
|
||||
Nickname string
|
||||
}
|
||||
result, err := db.Model(table).Data(User{
|
||||
Passport: "john-test",
|
||||
Password: "123456",
|
||||
Nickname: "John",
|
||||
Base: Base{
|
||||
Id: 100,
|
||||
Uid: 100,
|
||||
CreateTime: gtime.Now().String(),
|
||||
},
|
||||
}).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
var user *User
|
||||
err = db.Model(table).Fields(user).Where("id=100").Scan(&user)
|
||||
t.AssertNil(err)
|
||||
t.Assert(user.Passport, "john-test")
|
||||
t.Assert(user.Id, 100)
|
||||
})
|
||||
}
|
||||
|
||||
// This is no longer used as the filter feature is automatically enabled from GoFrame v1.16.0.
|
||||
//func Test_Model_Insert_KeyFieldNameMapping_Error(t *testing.T) {
|
||||
// table := createTable()
|
||||
// defer dropTable(table)
|
||||
//
|
||||
// gtest.C(t, func(t *gtest.T) {
|
||||
// type User struct {
|
||||
// Id int
|
||||
// Passport string
|
||||
// Password string
|
||||
// Nickname string
|
||||
// CreateTime string
|
||||
// NoneExistFiled string
|
||||
// }
|
||||
// data := User{
|
||||
// Id: 1,
|
||||
// Passport: "user_1",
|
||||
// Password: "pass_1",
|
||||
// Nickname: "name_1",
|
||||
// CreateTime: "2020-10-10 12:00:01",
|
||||
// }
|
||||
// _, err := db.Model(table).Data(data).Insert()
|
||||
// t.AssertNE(err, nil)
|
||||
// })
|
||||
//}
|
||||
|
||||
func Test_Model_Fields_AutoFilterInJoinStatement(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var err error
|
||||
table1 := "user"
|
||||
table2 := "score"
|
||||
table3 := "info"
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(500) NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
|
||||
`, table1,
|
||||
)); err != nil {
|
||||
t.AssertNil(err)
|
||||
}
|
||||
defer dropTable(table1)
|
||||
_, err = db.Model(table1).Insert(g.Map{
|
||||
"id": 1,
|
||||
"name": "john",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
user_id int(11) NOT NULL DEFAULT 0,
|
||||
number varchar(500) NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
|
||||
`, table2,
|
||||
)); err != nil {
|
||||
t.AssertNil(err)
|
||||
}
|
||||
defer dropTable(table2)
|
||||
_, err = db.Model(table2).Insert(g.Map{
|
||||
"id": 1,
|
||||
"user_id": 1,
|
||||
"number": "n",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
user_id int(11) NOT NULL DEFAULT 0,
|
||||
description varchar(500) NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
|
||||
`, table3,
|
||||
)); err != nil {
|
||||
t.AssertNil(err)
|
||||
}
|
||||
defer dropTable(table3)
|
||||
_, err = db.Model(table3).Insert(g.Map{
|
||||
"id": 1,
|
||||
"user_id": 1,
|
||||
"description": "brief",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.Model("user").
|
||||
Where("user.id", 1).
|
||||
Fields("score.number,user.name").
|
||||
LeftJoin("score", "user.id=score.user_id").
|
||||
LeftJoin("info", "info.id=info.user_id").
|
||||
Order("user.id asc").
|
||||
One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one), 2)
|
||||
t.Assert(one["name"].String(), "john")
|
||||
t.Assert(one["number"].String(), "n")
|
||||
|
||||
one, err = db.Model("user").
|
||||
LeftJoin("score", "user.id=score.user_id").
|
||||
LeftJoin("info", "info.id=info.user_id").
|
||||
Fields("score.number,user.name").
|
||||
One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one), 2)
|
||||
t.Assert(one["name"].String(), "john")
|
||||
t.Assert(one["number"].String(), "n")
|
||||
})
|
||||
}
|
||||
@ -30,7 +30,7 @@ func Test_Model_Insert(t *testing.T) {
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := db.Model(table)
|
||||
result, err := user.Filter().Data(g.Map{
|
||||
result, err := user.Data(g.Map{
|
||||
"id": 1,
|
||||
"uid": 1,
|
||||
"passport": "t1",
|
||||
@ -42,7 +42,7 @@ func Test_Model_Insert(t *testing.T) {
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 1)
|
||||
|
||||
result, err = db.Model(table).Filter().Data(g.Map{
|
||||
result, err = db.Model(table).Data(g.Map{
|
||||
"id": "2",
|
||||
"uid": "2",
|
||||
"passport": "t2",
|
||||
@ -63,7 +63,7 @@ func Test_Model_Insert(t *testing.T) {
|
||||
CreateTime *gtime.Time `json:"create_time"`
|
||||
}
|
||||
// Model inserting.
|
||||
result, err = db.Model(table).Filter().Data(User{
|
||||
result, err = db.Model(table).Data(User{
|
||||
Id: 3,
|
||||
Uid: 3,
|
||||
Passport: "t3",
|
||||
@ -77,7 +77,7 @@ func Test_Model_Insert(t *testing.T) {
|
||||
t.AssertNil(err)
|
||||
t.Assert(value.String(), "t3")
|
||||
|
||||
result, err = db.Model(table).Filter().Data(&User{
|
||||
result, err = db.Model(table).Data(&User{
|
||||
Id: 4,
|
||||
Uid: 4,
|
||||
Passport: "t4",
|
||||
@ -99,60 +99,6 @@ func Test_Model_Insert(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// Using filter dose not affect the outside value inside function.
|
||||
func Test_Model_Insert_Filter(t *testing.T) {
|
||||
// map
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
data := g.Map{
|
||||
"id": 1,
|
||||
"uid": 1,
|
||||
"passport": "t1",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname": "name_1",
|
||||
"create_time": gtime.Now().String(),
|
||||
}
|
||||
result, err := db.Model(table).Filter().Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 1)
|
||||
|
||||
t.Assert(data["uid"], 1)
|
||||
})
|
||||
// slice
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
data := g.List{
|
||||
g.Map{
|
||||
"id": 1,
|
||||
"uid": 1,
|
||||
"passport": "t1",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname": "name_1",
|
||||
"create_time": gtime.Now().String(),
|
||||
},
|
||||
g.Map{
|
||||
"id": 2,
|
||||
"uid": 2,
|
||||
"passport": "t1",
|
||||
"password": "25d55ad283aa400af464c76d713c07ad",
|
||||
"nickname": "name_1",
|
||||
"create_time": gtime.Now().String(),
|
||||
},
|
||||
}
|
||||
|
||||
result, err := db.Model(table).Filter().Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, 2)
|
||||
|
||||
t.Assert(data[0]["uid"], 1)
|
||||
t.Assert(data[1]["uid"], 2)
|
||||
})
|
||||
}
|
||||
|
||||
// Fix issue: https://github.com/gogf/gf/issues/819
|
||||
func Test_Model_Insert_WithStructAndSliceAttribute(t *testing.T) {
|
||||
table := createTable()
|
||||
@ -240,32 +186,6 @@ func Test_Model_Update_KeyFieldNameMapping(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// This is no longer used as the filter feature is automatically enabled from GoFrame v1.16.0.
|
||||
//func Test_Model_Insert_KeyFieldNameMapping_Error(t *testing.T) {
|
||||
// table := createTable()
|
||||
// defer dropTable(table)
|
||||
//
|
||||
// gtest.C(t, func(t *gtest.T) {
|
||||
// type User struct {
|
||||
// Id int
|
||||
// Passport string
|
||||
// Password string
|
||||
// Nickname string
|
||||
// CreateTime string
|
||||
// NoneExistFiled string
|
||||
// }
|
||||
// data := User{
|
||||
// Id: 1,
|
||||
// Passport: "user_1",
|
||||
// Password: "pass_1",
|
||||
// Nickname: "name_1",
|
||||
// CreateTime: "2020-10-10 12:00:01",
|
||||
// }
|
||||
// _, err := db.Model(table).Data(data).Insert()
|
||||
// t.AssertNE(err, nil)
|
||||
// })
|
||||
//}
|
||||
|
||||
func Test_Model_Insert_Time(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
@ -305,7 +225,7 @@ func Test_Model_BatchInsertWithArrayStruct(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
result, err := user.Filter().Data(array).Insert()
|
||||
result, err := user.Data(array).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.LastInsertId()
|
||||
t.Assert(n, TableSize)
|
||||
@ -316,7 +236,7 @@ func Test_Model_InsertIgnore(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Model(table).Filter().Data(g.Map{
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"id": 1,
|
||||
"uid": 1,
|
||||
"passport": "t1",
|
||||
@ -327,7 +247,7 @@ func Test_Model_InsertIgnore(t *testing.T) {
|
||||
t.AssertNE(err, nil)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
_, err := db.Model(table).Filter().Data(g.Map{
|
||||
_, err := db.Model(table).Data(g.Map{
|
||||
"id": 1,
|
||||
"uid": 1,
|
||||
"passport": "t1",
|
||||
@ -344,7 +264,7 @@ func Test_Model_Batch(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
result, err := db.Model(table).Filter().Data(g.List{
|
||||
result, err := db.Model(table).Data(g.List{
|
||||
{
|
||||
"id": 2,
|
||||
"uid": 2,
|
||||
@ -2441,7 +2361,7 @@ func Test_Model_Schema1(t *testing.T) {
|
||||
// Model.
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := 1000
|
||||
_, err := db.Model(table).Schema(TestSchema1).Filter().Insert(g.Map{
|
||||
_, err := db.Model(table).Schema(TestSchema1).Insert(g.Map{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf(`user_%d`, i),
|
||||
"password": fmt.Sprintf(`pass_%d`, i),
|
||||
@ -2503,7 +2423,7 @@ func Test_Model_Schema2(t *testing.T) {
|
||||
// Schema.
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
i := 1000
|
||||
_, err := db.Schema(TestSchema1).Table(table).Filter().Insert(g.Map{
|
||||
_, err := db.Schema(TestSchema1).Table(table).Insert(g.Map{
|
||||
"id": i,
|
||||
"passport": fmt.Sprintf(`user_%d`, i),
|
||||
"password": fmt.Sprintf(`pass_%d`, i),
|
||||
@ -3243,91 +3163,6 @@ func Test_Model_Fields_Map_Struct(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Fields_AutoFilterInJoinStatement(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var err error
|
||||
table1 := "user"
|
||||
table2 := "score"
|
||||
table3 := "info"
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
name varchar(500) NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
|
||||
`, table1,
|
||||
)); err != nil {
|
||||
t.AssertNil(err)
|
||||
}
|
||||
defer dropTable(table1)
|
||||
_, err = db.Model(table1).Insert(g.Map{
|
||||
"id": 1,
|
||||
"name": "john",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
user_id int(11) NOT NULL DEFAULT 0,
|
||||
number varchar(500) NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
|
||||
`, table2,
|
||||
)); err != nil {
|
||||
t.AssertNil(err)
|
||||
}
|
||||
defer dropTable(table2)
|
||||
_, err = db.Model(table2).Insert(g.Map{
|
||||
"id": 1,
|
||||
"user_id": 1,
|
||||
"number": "n",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
if _, err := db.Exec(fmt.Sprintf(`
|
||||
CREATE TABLE IF NOT EXISTS %s (
|
||||
id int(11) NOT NULL AUTO_INCREMENT,
|
||||
user_id int(11) NOT NULL DEFAULT 0,
|
||||
description varchar(500) NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
|
||||
`, table3,
|
||||
)); err != nil {
|
||||
t.AssertNil(err)
|
||||
}
|
||||
defer dropTable(table3)
|
||||
_, err = db.Model(table3).Insert(g.Map{
|
||||
"id": 1,
|
||||
"user_id": 1,
|
||||
"description": "brief",
|
||||
})
|
||||
t.AssertNil(err)
|
||||
|
||||
one, err := db.Model("user").
|
||||
Where("user.id", 1).
|
||||
Fields("score.number,user.name").
|
||||
LeftJoin("score", "user.id=score.user_id").
|
||||
LeftJoin("info", "info.id=info.user_id").
|
||||
Order("user.id asc").
|
||||
One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one), 2)
|
||||
t.Assert(one["name"].String(), "john")
|
||||
t.Assert(one["number"].String(), "n")
|
||||
|
||||
one, err = db.Model("user").
|
||||
LeftJoin("score", "user.id=score.user_id").
|
||||
LeftJoin("info", "info.id=info.user_id").
|
||||
Fields("score.number,user.name").
|
||||
One()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(one), 2)
|
||||
t.Assert(one["name"].String(), "john")
|
||||
t.Assert(one["number"].String(), "n")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_WhereIn(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
@ -20,7 +20,7 @@ func Test_Insert_Raw(t *testing.T) {
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := db.Model(table)
|
||||
result, err := user.Filter().Data(g.Map{
|
||||
result, err := user.Data(g.Map{
|
||||
"id": gdb.Raw("id+2"),
|
||||
"passport": "port_1",
|
||||
"password": "pass_1",
|
||||
@ -39,7 +39,7 @@ func Test_BatchInsert_Raw(t *testing.T) {
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
user := db.Model(table)
|
||||
result, err := user.Filter().Data(
|
||||
result, err := user.Data(
|
||||
g.List{
|
||||
g.Map{
|
||||
"id": gdb.Raw("id+2"),
|
||||
|
||||
@ -15,7 +15,7 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_Model_Inherit_Insert(t *testing.T) {
|
||||
func Test_Model_Embedded_Insert(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
@ -31,7 +31,7 @@ func Test_Model_Inherit_Insert(t *testing.T) {
|
||||
Password string `json:"password"`
|
||||
Nickname string `json:"nickname"`
|
||||
}
|
||||
result, err := db.Model(table).Filter().Data(User{
|
||||
result, err := db.Model(table).Data(User{
|
||||
Passport: "john-test",
|
||||
Password: "123456",
|
||||
Nickname: "John",
|
||||
@ -50,7 +50,7 @@ func Test_Model_Inherit_Insert(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Inherit_MapToStruct(t *testing.T) {
|
||||
func Test_Model_Embedded_MapToStruct(t *testing.T) {
|
||||
table := createTable()
|
||||
defer dropTable(table)
|
||||
|
||||
@ -77,7 +77,7 @@ func Test_Model_Inherit_MapToStruct(t *testing.T) {
|
||||
"nickname": "T1",
|
||||
"create_time": gtime.Now().String(),
|
||||
}
|
||||
result, err := db.Model(table).Filter().Data(data).Insert()
|
||||
result, err := db.Model(table).Data(data).Insert()
|
||||
t.AssertNil(err)
|
||||
n, _ := result.RowsAffected()
|
||||
t.Assert(n, 1)
|
||||
|
||||
Reference in New Issue
Block a user