Merge branch 'master' into feature/ormwith

This commit is contained in:
jflyfox 2021-02-07 15:34:50 +08:00
commit 39fb842e9b
18 changed files with 1288 additions and 701 deletions

View File

@ -1,7 +1,7 @@
# GoFrame
[![Go Doc](https://godoc.org/github.com/gogf/gf?status.svg)](https://godoc.org/github.com/gogf/gf)
[![Build Status](https://travis-ci.org/gogf/gf.svg?branch=master)](https://travis-ci.org/gogf/gf)
[![Build Status](https://travis-ci.com/gogf/gf.svg?branch=master)](https://travis-ci.org/gogf/gf)
[![Go Report](https://goreportcard.com/badge/github.com/gogf/gf?v=1)](https://goreportcard.com/report/github.com/gogf/gf)
[![Code Coverage](https://codecov.io/gh/gogf/gf/branch/master/graph/badge.svg)](https://codecov.io/gh/gogf/gf/branch/master)
[![Production Ready](https://img.shields.io/badge/production-ready-blue.svg)](https://github.com/gogf/gf)
@ -33,7 +33,7 @@ golang version >= 1.11
# Architecture
<div align=center>
<img src="https://itician.org/download/attachments/1114119/arch.png"/>
<img src="https://goframe.org/download/attachments/1114119/arch.png"/>
</div>
# Packages
@ -80,7 +80,7 @@ The `Web` component performance of `GoFrame`, please refer to third-party projec
- [XiMaLaYa](https://www.ximalaya.com)
- [ZYBang](https://www.zybang.com/)
> We list part of the users here, if your company or products are using `GoFrame`, please let us know [here](https://itician.org/pages/viewpage.action?pageId=1114415).
> We list part of the users here, if your company or products are using `GoFrame`, please let us know [here](https://goframe.org/pages/viewpage.action?pageId=1114415).
# Contributors
@ -90,7 +90,7 @@ This project exists thanks to all the people who contribute. [[Contributors](htt
# Donators
If you love `GF`, why not [buy developer a cup of coffee](https://itician.org/pages/viewpage.action?pageId=1115633)?
If you love `GF`, why not [buy developer a cup of coffee](https://goframe.org/pages/viewpage.action?pageId=1115633)?
# Sponsors
We appreciate any kind of sponsorship for `GF` development. If you've got some interesting, please contact WeChat `389961817` / Email `john@goframe.org`.
@ -98,7 +98,7 @@ We appreciate any kind of sponsorship for `GF` development. If you've got some i
# Thanks
<a href="https://www.jetbrains.com/?from=GoFrame"><img src="https://itician.org/download/thumbnails/1114119/jetbrains.png" height="120" alt="JetBrains"/></a>
<a href="https://www.jetbrains.com/?from=GoFrame"><img src="https://goframe.org/download/thumbnails/1114119/jetbrains.png" height="120" alt="JetBrains"/></a>
<a href="https://www.atlassian.com/?from=GoFrame"><img src="https://goframe.org/download/attachments/1114119/atlassian.jpg" height="120" alt="Atlassian"/></a>

View File

@ -1,6 +1,6 @@
# GoFrame
[![Go Doc](https://godoc.org/github.com/gogf/gf?status.svg)](https://godoc.org/github.com/gogf/gf)
[![Build Status](https://travis-ci.org/gogf/gf.svg?branch=master)](https://travis-ci.org/gogf/gf)
[![Build Status](https://travis-ci.com/gogf/gf.svg?branch=master)](https://travis-ci.org/gogf/gf)
[![Go Report](https://goreportcard.com/badge/github.com/gogf/gf?v=1)](https://goreportcard.com/report/github.com/gogf/gf)
[![Code Coverage](https://codecov.io/gh/gogf/gf/branch/master/graph/badge.svg)](https://codecov.io/gh/gogf/gf/branch/master)
[![Production Ready](https://img.shields.io/badge/production-ready-blue.svg)](https://github.com/gogf/gf)
@ -49,7 +49,7 @@ golang版本 >= 1.11
# 架构
<div align=center>
<img src="https://itician.org/download/attachments/1114119/arch.png"/>
<img src="https://goframe.org/download/attachments/1114119/arch.png"/>
</div>
# 模块
@ -95,7 +95,7 @@ golang版本 >= 1.11
- [喜马拉雅](https://www.ximalaya.com)
- [作业帮](https://www.zybang.com/)
> 在这里只列举了部分知名的用户,如果您的企业或者产品正在使用`GoFrame`,欢迎到 [这里](https://itician.org/pages/viewpage.action?pageId=1114415) 留言。
> 在这里只列举了部分知名的用户,如果您的企业或者产品正在使用`GoFrame`,欢迎到 [这里](https://goframe.org/pages/viewpage.action?pageId=1114415) 留言。
# 贡献
@ -105,7 +105,7 @@ golang版本 >= 1.11
# 捐赠
如果您喜欢`GF`,要不给开发者 [来杯咖啡](https://itician.org/pages/viewpage.action?pageId=1115633) 吧!
如果您喜欢`GF`,要不给开发者 [来杯咖啡](https://goframe.org/pages/viewpage.action?pageId=1115633) 吧!
请在捐赠时备注您的`github`/`gitee`账号名称。
# 赞助
@ -113,6 +113,6 @@ golang版本 >= 1.11
赞助支持`GF`框架的快速研发,如果您感兴趣,请联系 微信 `389961817` / 邮件 `john@goframe.org`。
# 感谢
<a href="https://www.jetbrains.com/?from=GoFrame"><img src="https://itician.org/download/thumbnails/1114119/jetbrains.png" height="120" alt="JetBrains"/></a>
<a href="https://www.jetbrains.com/?from=GoFrame"><img src="https://goframe.org/download/thumbnails/1114119/jetbrains.png" height="120" alt="JetBrains"/></a>
<a href="https://www.atlassian.com/?from=GoFrame"><img src="https://goframe.org/download/attachments/1114119/atlassian.jpg" height="120" alt="Atlassian"/></a>

View File

@ -11,6 +11,7 @@ import (
"github.com/gogf/gf/errors/gerror"
"github.com/gogf/gf/text/gstr"
"github.com/gogf/gf/util/gconv"
"github.com/gogf/gf/util/gutil"
"reflect"
)
@ -41,6 +42,9 @@ import (
//
// See the example or unit testing cases for clear understanding for this function.
func (r Result) ScanList(listPointer interface{}, bindToAttrName string, relationKV ...string) (err error) {
if r.IsEmpty() {
return nil
}
// Necessary checks for parameters.
if bindToAttrName == "" {
return gerror.New(`bindToAttrName should not be empty`)
@ -112,7 +116,13 @@ func (r Result) ScanList(listPointer interface{}, bindToAttrName string, relatio
relationFromAttrName = relationKV[0]
relationKVStr = relationKV[1]
}
array := gstr.SplitAndTrim(relationKVStr, ":")
// The relation key string of table filed name and attribute name
// can be joined with char '=' or ':'.
array := gstr.SplitAndTrim(relationKVStr, "=")
if len(array) == 1 {
// Compatible with old splitting char ':'.
array = gstr.SplitAndTrim(relationKVStr, ":")
}
if len(array) == 2 {
// Defined table field to relation attribute name.
// Like:
@ -120,6 +130,15 @@ func (r Result) ScanList(listPointer interface{}, bindToAttrName string, relatio
// uid:UserId
relationResultFieldName = array[0]
relationBindToSubAttrName = array[1]
if key, _ := gutil.MapPossibleItemByKey(r[0].Map(), relationResultFieldName); key == "" {
return gerror.Newf(
`cannot find possible related table field name "%s" from given relation key "%s"`,
relationResultFieldName,
relationKVStr,
)
} else {
relationResultFieldName = key
}
} else {
return gerror.New(`parameter relationKV should be format of "ResultFieldName:BindToAttrName"`)
}
@ -152,8 +171,9 @@ func (r Result) ScanList(listPointer interface{}, bindToAttrName string, relatio
// Bind to relation conditions.
var (
relationFromAttrValue reflect.Value
relationFromAttrField reflect.Value
relationFromAttrValue reflect.Value
relationFromAttrField reflect.Value
relationBindToSubAttrNameChecked bool
)
for i := 0; i < arrayValue.Len(); i++ {
arrayElemValue := arrayValue.Index(i)
@ -187,6 +207,30 @@ func (r Result) ScanList(listPointer interface{}, bindToAttrName string, relatio
if len(relationDataMap) > 0 && !relationFromAttrValue.IsValid() {
return gerror.Newf(`invalid relation specified: "%v"`, relationKV)
}
// Check and find possible bind to attribute name.
if relationKVStr != "" && !relationBindToSubAttrNameChecked {
relationFromAttrField = relationFromAttrValue.FieldByName(relationBindToSubAttrName)
if !relationFromAttrField.IsValid() {
var (
relationFromAttrType = relationFromAttrValue.Type()
filedMap = make(map[string]interface{})
)
for i := 0; i < relationFromAttrType.NumField(); i++ {
filedMap[relationFromAttrType.Field(i).Name] = struct{}{}
}
if key, _ := gutil.MapPossibleItemByKey(filedMap, relationBindToSubAttrName); key == "" {
return gerror.Newf(
`cannot find possible related attribute name "%s" from given relation key "%s"`,
relationBindToSubAttrName,
relationKVStr,
)
} else {
relationBindToSubAttrName = key
}
}
relationBindToSubAttrNameChecked = true
}
switch bindToAttrKind {
case reflect.Array, reflect.Slice:
if len(relationDataMap) > 0 {

View File

@ -67,7 +67,7 @@ func Test_Custom_Driver(t *testing.T) {
t.Assert(latestSqlString.Val(), "")
sqlString := "select 10000"
value, err := g.DB("driver-test").GetValue(sqlString)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(value, 10000)
t.Assert(latestSqlString.Val(), sqlString)
})

View File

@ -38,7 +38,7 @@ func init() {
"name": true,
"type": true,
}, false)
gtest.Assert(err, nil)
gtest.AssertNil(err)
configNode = gdb.ConfigNode{
Host: "127.0.0.1",
Port: "3306",
@ -195,7 +195,7 @@ func createInitTableWithDb(db gdb.DB, table ...string) (name string) {
}
result, err := db.BatchInsert(name, array.Slice())
gtest.Assert(err, nil)
gtest.AssertNil(err)
n, e := result.RowsAffected()
gtest.Assert(e, nil)

View File

@ -108,24 +108,24 @@ CREATE TABLE %s (
})
return err
})
t.Assert(err, nil)
t.AssertNil(err)
})
// Data check.
gtest.C(t, func(t *gtest.T) {
r, err := db.Table(tableUser).All()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(r.Len(), 1)
t.Assert(r[0]["uid"].Int(), 1)
t.Assert(r[0]["name"].String(), "john")
r, err = db.Table(tableUserDetail).Where("uid", r[0]["uid"].Int()).All()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(r.Len(), 1)
t.Assert(r[0]["uid"].Int(), 1)
t.Assert(r[0]["address"].String(), `Beijing DongZhiMen #66`)
r, err = db.Table(tableUserScores).Where("uid", r[0]["uid"].Int()).All()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(r.Len(), 2)
t.Assert(r[0]["uid"].Int(), 1)
t.Assert(r[1]["uid"].Int(), 1)
@ -137,15 +137,15 @@ CREATE TABLE %s (
var user Entity
// SELECT * FROM `user` WHERE `name`='john'
err := db.Table(tableUser).Scan(&user.User, "name", "john")
t.Assert(err, nil)
t.AssertNil(err)
// SELECT * FROM `user_detail` WHERE `uid`=1
err = db.Table(tableUserDetail).Scan(&user.UserDetail, "uid", user.User.Uid)
t.Assert(err, nil)
t.AssertNil(err)
// SELECT * FROM `user_scores` WHERE `uid`=1
err = db.Table(tableUserScores).Scan(&user.UserScores, "uid", user.User.Uid)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(user.User, EntityUser{
Uid: 1,
@ -222,39 +222,42 @@ CREATE TABLE %s (
}
// Initialize the data.
var err error
for i := 1; i <= 5; i++ {
// User.
_, err = db.Insert(tableUser, g.Map{
"uid": i,
"name": fmt.Sprintf(`name_%d`, i),
})
gtest.Assert(err, nil)
// Detail.
_, err = db.Insert(tableUserDetail, g.Map{
"uid": i,
"address": fmt.Sprintf(`address_%d`, i),
})
gtest.Assert(err, nil)
// Scores.
for j := 1; j <= 5; j++ {
_, err = db.Insert(tableUserScores, g.Map{
"uid": i,
"score": j,
gtest.C(t, func(t *gtest.T) {
var err error
for i := 1; i <= 5; i++ {
// User.
_, err = db.Insert(tableUser, g.Map{
"uid": i,
"name": fmt.Sprintf(`name_%d`, i),
})
gtest.Assert(err, nil)
t.AssertNil(err)
// Detail.
_, err = db.Insert(tableUserDetail, g.Map{
"uid": i,
"address": fmt.Sprintf(`address_%d`, i),
})
t.AssertNil(err)
// Scores.
for j := 1; j <= 5; j++ {
_, err = db.Insert(tableUserScores, g.Map{
"uid": i,
"score": j,
})
t.AssertNil(err)
}
}
}
})
// MapKeyValue.
gtest.C(t, func(t *gtest.T) {
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(all.Len(), 2)
t.Assert(len(all.MapKeyValue("uid")), 2)
t.Assert(all.MapKeyValue("uid")["3"].Map()["uid"], 3)
t.Assert(all.MapKeyValue("uid")["4"].Map()["uid"], 4)
all, err = db.Table(tableUserScores).Where("uid", g.Slice{3, 4}).Order("id asc").All()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(all.Len(), 10)
t.Assert(len(all.MapKeyValue("uid")), 2)
t.Assert(len(all.MapKeyValue("uid")["3"].Slice()), 5)
@ -269,24 +272,24 @@ CREATE TABLE %s (
var users []Entity
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].User, &EntityUser{3, "name_3"})
t.Assert(users[1].User, &EntityUser{4, "name_4"})
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
gtest.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "uid:Uid")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(users[0].UserDetail, &EntityUserDetail{3, "address_3"})
t.Assert(users[1].UserDetail, &EntityUserDetail{4, "address_4"})
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
gtest.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "uid:Uid")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users[0].UserScores), 5)
t.Assert(len(users[1].UserScores), 5)
t.Assert(users[0].UserScores[0].Uid, 3)
@ -302,24 +305,24 @@ CREATE TABLE %s (
var users []*Entity
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].User, &EntityUser{3, "name_3"})
t.Assert(users[1].User, &EntityUser{4, "name_4"})
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
gtest.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "uid:Uid")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(users[0].UserDetail, &EntityUserDetail{3, "address_3"})
t.Assert(users[1].UserDetail, &EntityUserDetail{4, "address_4"})
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
gtest.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "uid:Uid")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users[0].UserScores), 5)
t.Assert(len(users[1].UserScores), 5)
t.Assert(users[0].UserScores[0].Uid, 3)
@ -353,24 +356,24 @@ CREATE TABLE %s (
var users []Entity
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].User, &EntityUser{3, "name_3"})
t.Assert(users[1].User, &EntityUser{4, "name_4"})
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
gtest.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "uid:Uid")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(users[0].UserDetail, &EntityUserDetail{3, "address_3"})
t.Assert(users[1].UserDetail, &EntityUserDetail{4, "address_4"})
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
gtest.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "uid:Uid")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users[0].UserScores), 5)
t.Assert(len(users[1].UserScores), 5)
t.Assert(users[0].UserScores[0].Uid, 3)
@ -405,24 +408,24 @@ CREATE TABLE %s (
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].User, &EntityUser{3, "name_3"})
t.Assert(users[1].User, &EntityUser{4, "name_4"})
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
gtest.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "uid:Uid")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(users[0].UserDetail, &EntityUserDetail{3, "address_3"})
t.Assert(users[1].UserDetail, &EntityUserDetail{4, "address_4"})
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
gtest.Assert(err, nil)
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "uid:Uid")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users[0].UserScores), 5)
t.Assert(len(users[1].UserScores), 5)
t.Assert(users[0].UserScores[0].Uid, 3)
@ -441,19 +444,19 @@ CREATE TABLE %s (
Where("uid", g.Slice{3, 4}).
Order("uid asc").
ScanList(&users, "User")
t.Assert(err, nil)
t.AssertNil(err)
// Detail
err = db.Table(tableUserDetail).
Where("uid", gdb.ListItemValues(users, "User", "Uid")).
Order("uid asc").
ScanList(&users, "UserDetail", "User", "uid:Uid")
gtest.Assert(err, nil)
t.AssertNil(err)
// Scores
err = db.Table(tableUserScores).
Where("uid", gdb.ListItemValues(users, "User", "Uid")).
Order("id asc").
ScanList(&users, "UserScores", "User", "uid:Uid")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].User, &EntityUser{3, "name_3"})
@ -473,6 +476,532 @@ CREATE TABLE %s (
})
}
func Test_Table_Relation_Many_RelationKeyCaseInsensitive(t *testing.T) {
var (
tableUser = "user_" + gtime.TimestampMicroStr()
tableUserDetail = "user_detail_" + gtime.TimestampMicroStr()
tableUserScores = "user_scores_" + gtime.TimestampMicroStr()
)
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(45) NOT NULL,
PRIMARY KEY (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableUser)); err != nil {
gtest.Error(err)
}
defer dropTable(tableUser)
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
address varchar(45) NOT NULL,
PRIMARY KEY (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableUserDetail)); err != nil {
gtest.Error(err)
}
defer dropTable(tableUserDetail)
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
uid int(10) unsigned NOT NULL,
score int(10) unsigned NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableUserScores)); err != nil {
gtest.Error(err)
}
defer dropTable(tableUserScores)
type EntityUser struct {
Uid int `json:"uid"`
Name string `json:"name"`
}
type EntityUserDetail struct {
Uid int `json:"uid"`
Address string `json:"address"`
}
type EntityUserScores struct {
Id int `json:"id"`
Uid int `json:"uid"`
Score int `json:"score"`
}
type Entity struct {
User *EntityUser
UserDetail *EntityUserDetail
UserScores []*EntityUserScores
}
// Initialize the data.
gtest.C(t, func(t *gtest.T) {
var err error
for i := 1; i <= 5; i++ {
// User.
_, err = db.Insert(tableUser, g.Map{
"uid": i,
"name": fmt.Sprintf(`name_%d`, i),
})
t.AssertNil(err)
// Detail.
_, err = db.Insert(tableUserDetail, g.Map{
"uid": i,
"address": fmt.Sprintf(`address_%d`, i),
})
t.AssertNil(err)
// Scores.
for j := 1; j <= 5; j++ {
_, err = db.Insert(tableUserScores, g.Map{
"uid": i,
"score": j,
})
t.AssertNil(err)
}
}
})
// MapKeyValue.
gtest.C(t, func(t *gtest.T) {
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.AssertNil(err)
t.Assert(all.Len(), 2)
t.Assert(len(all.MapKeyValue("uid")), 2)
t.Assert(all.MapKeyValue("uid")["3"].Map()["uid"], 3)
t.Assert(all.MapKeyValue("uid")["4"].Map()["uid"], 4)
all, err = db.Table(tableUserScores).Where("uid", g.Slice{3, 4}).Order("id asc").All()
t.AssertNil(err)
t.Assert(all.Len(), 10)
t.Assert(len(all.MapKeyValue("uid")), 2)
t.Assert(len(all.MapKeyValue("uid")["3"].Slice()), 5)
t.Assert(len(all.MapKeyValue("uid")["4"].Slice()), 5)
t.Assert(gconv.Map(all.MapKeyValue("uid")["3"].Slice()[0])["uid"], 3)
t.Assert(gconv.Map(all.MapKeyValue("uid")["3"].Slice()[0])["score"], 1)
t.Assert(gconv.Map(all.MapKeyValue("uid")["3"].Slice()[4])["uid"], 3)
t.Assert(gconv.Map(all.MapKeyValue("uid")["3"].Slice()[4])["score"], 5)
})
// Result ScanList with struct elements and pointer attributes.
gtest.C(t, func(t *gtest.T) {
var users []Entity
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].User, &EntityUser{3, "name_3"})
t.Assert(users[1].User, &EntityUser{4, "name_4"})
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "uid:uid")
t.AssertNil(err)
t.Assert(users[0].UserDetail, &EntityUserDetail{3, "address_3"})
t.Assert(users[1].UserDetail, &EntityUserDetail{4, "address_4"})
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "uid:uid")
t.AssertNil(err)
t.Assert(len(users[0].UserScores), 5)
t.Assert(len(users[1].UserScores), 5)
t.Assert(users[0].UserScores[0].Uid, 3)
t.Assert(users[0].UserScores[0].Score, 1)
t.Assert(users[0].UserScores[4].Score, 5)
t.Assert(users[1].UserScores[0].Uid, 4)
t.Assert(users[1].UserScores[0].Score, 1)
t.Assert(users[1].UserScores[4].Score, 5)
})
// Result ScanList with pointer elements and pointer attributes.
gtest.C(t, func(t *gtest.T) {
var users []*Entity
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].User, &EntityUser{3, "name_3"})
t.Assert(users[1].User, &EntityUser{4, "name_4"})
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "Uid:UID")
t.AssertNil(err)
t.Assert(users[0].UserDetail, &EntityUserDetail{3, "address_3"})
t.Assert(users[1].UserDetail, &EntityUserDetail{4, "address_4"})
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "Uid:UID")
t.AssertNil(err)
t.Assert(len(users[0].UserScores), 5)
t.Assert(len(users[1].UserScores), 5)
t.Assert(users[0].UserScores[0].Uid, 3)
t.Assert(users[0].UserScores[0].Score, 1)
t.Assert(users[0].UserScores[4].Score, 5)
t.Assert(users[1].UserScores[0].Uid, 4)
t.Assert(users[1].UserScores[0].Score, 1)
t.Assert(users[1].UserScores[4].Score, 5)
})
// Result ScanList with struct elements and struct attributes.
gtest.C(t, func(t *gtest.T) {
type EntityUser struct {
Uid int `json:"uid"`
Name string `json:"name"`
}
type EntityUserDetail struct {
Uid int `json:"uid"`
Address string `json:"address"`
}
type EntityUserScores struct {
Id int `json:"id"`
Uid int `json:"uid"`
Score int `json:"score"`
}
type Entity struct {
User EntityUser
UserDetail EntityUserDetail
UserScores []EntityUserScores
}
var users []Entity
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].User, &EntityUser{3, "name_3"})
t.Assert(users[1].User, &EntityUser{4, "name_4"})
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "uid:UId")
t.AssertNil(err)
t.Assert(users[0].UserDetail, &EntityUserDetail{3, "address_3"})
t.Assert(users[1].UserDetail, &EntityUserDetail{4, "address_4"})
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "UId:Uid")
t.AssertNil(err)
t.Assert(len(users[0].UserScores), 5)
t.Assert(len(users[1].UserScores), 5)
t.Assert(users[0].UserScores[0].Uid, 3)
t.Assert(users[0].UserScores[0].Score, 1)
t.Assert(users[0].UserScores[4].Score, 5)
t.Assert(users[1].UserScores[0].Uid, 4)
t.Assert(users[1].UserScores[0].Score, 1)
t.Assert(users[1].UserScores[4].Score, 5)
})
// Result ScanList with pointer elements and struct attributes.
gtest.C(t, func(t *gtest.T) {
type EntityUser struct {
Uid int `json:"uid"`
Name string `json:"name"`
}
type EntityUserDetail struct {
Uid int `json:"uid"`
Address string `json:"address"`
}
type EntityUserScores struct {
Id int `json:"id"`
Uid int `json:"uid"`
Score int `json:"score"`
}
type Entity struct {
User EntityUser
UserDetail EntityUserDetail
UserScores []EntityUserScores
}
var users []*Entity
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].User, &EntityUser{3, "name_3"})
t.Assert(users[1].User, &EntityUser{4, "name_4"})
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "uid:Uid")
t.AssertNil(err)
t.Assert(users[0].UserDetail, &EntityUserDetail{3, "address_3"})
t.Assert(users[1].UserDetail, &EntityUserDetail{4, "address_4"})
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "UID:Uid")
t.AssertNil(err)
t.Assert(len(users[0].UserScores), 5)
t.Assert(len(users[1].UserScores), 5)
t.Assert(users[0].UserScores[0].Uid, 3)
t.Assert(users[0].UserScores[0].Score, 1)
t.Assert(users[0].UserScores[4].Score, 5)
t.Assert(users[1].UserScores[0].Uid, 4)
t.Assert(users[1].UserScores[0].Score, 1)
t.Assert(users[1].UserScores[4].Score, 5)
})
// Model ScanList with pointer elements and pointer attributes.
gtest.C(t, func(t *gtest.T) {
var users []*Entity
// User
err := db.Table(tableUser).
Where("uid", g.Slice{3, 4}).
Order("uid asc").
ScanList(&users, "User")
t.AssertNil(err)
// Detail
err = db.Table(tableUserDetail).
Where("uid", gdb.ListItemValues(users, "User", "Uid")).
Order("uid asc").
ScanList(&users, "UserDetail", "User", "uid:Uid")
t.AssertNil(err)
// Scores
err = db.Table(tableUserScores).
Where("uid", gdb.ListItemValues(users, "User", "Uid")).
Order("id asc").
ScanList(&users, "UserScores", "User", "uid:Uid")
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(users[0].User, &EntityUser{3, "name_3"})
t.Assert(users[1].User, &EntityUser{4, "name_4"})
t.Assert(users[0].UserDetail, &EntityUserDetail{3, "address_3"})
t.Assert(users[1].UserDetail, &EntityUserDetail{4, "address_4"})
t.Assert(len(users[0].UserScores), 5)
t.Assert(len(users[1].UserScores), 5)
t.Assert(users[0].UserScores[0].Uid, 3)
t.Assert(users[0].UserScores[0].Score, 1)
t.Assert(users[0].UserScores[4].Score, 5)
t.Assert(users[1].UserScores[0].Uid, 4)
t.Assert(users[1].UserScores[0].Score, 1)
t.Assert(users[1].UserScores[4].Score, 5)
})
}
func Test_Table_Relation_EmptyData(t *testing.T) {
var (
tableUser = "user_" + gtime.TimestampMicroStr()
tableUserDetail = "user_detail_" + gtime.TimestampMicroStr()
tableUserScores = "user_scores_" + gtime.TimestampMicroStr()
)
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(45) NOT NULL,
PRIMARY KEY (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableUser)); err != nil {
gtest.Error(err)
}
defer dropTable(tableUser)
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
uid int(10) unsigned NOT NULL AUTO_INCREMENT,
address varchar(45) NOT NULL,
PRIMARY KEY (uid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableUserDetail)); err != nil {
gtest.Error(err)
}
defer dropTable(tableUserDetail)
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE %s (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
uid int(10) unsigned NOT NULL,
score int(10) unsigned NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, tableUserScores)); err != nil {
gtest.Error(err)
}
defer dropTable(tableUserScores)
type EntityUser struct {
Uid int `json:"uid"`
Name string `json:"name"`
}
type EntityUserDetail struct {
Uid int `json:"uid"`
Address string `json:"address"`
}
type EntityUserScores struct {
Id int `json:"id"`
Uid int `json:"uid"`
Score int `json:"score"`
}
type Entity struct {
User *EntityUser
UserDetail *EntityUserDetail
UserScores []*EntityUserScores
}
// Result ScanList with struct elements and pointer attributes.
gtest.C(t, func(t *gtest.T) {
db.SetDebug(true)
var users []Entity
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.AssertNil(err)
t.Assert(len(users), 0)
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "uid:uid")
t.AssertNil(err)
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "uid:uid")
t.AssertNil(err)
})
return
// Result ScanList with pointer elements and pointer attributes.
gtest.C(t, func(t *gtest.T) {
var users []*Entity
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.AssertNil(err)
t.Assert(len(users), 0)
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "Uid:UID")
t.AssertNil(err)
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "Uid:UID")
t.AssertNil(err)
})
// Result ScanList with struct elements and struct attributes.
gtest.C(t, func(t *gtest.T) {
type EntityUser struct {
Uid int `json:"uid"`
Name string `json:"name"`
}
type EntityUserDetail struct {
Uid int `json:"uid"`
Address string `json:"address"`
}
type EntityUserScores struct {
Id int `json:"id"`
Uid int `json:"uid"`
Score int `json:"score"`
}
type Entity struct {
User EntityUser
UserDetail EntityUserDetail
UserScores []EntityUserScores
}
var users []Entity
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.AssertNil(err)
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "uid:UId")
t.AssertNil(err)
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "UId:Uid")
t.AssertNil(err)
})
// Result ScanList with pointer elements and struct attributes.
gtest.C(t, func(t *gtest.T) {
type EntityUser struct {
Uid int `json:"uid"`
Name string `json:"name"`
}
type EntityUserDetail struct {
Uid int `json:"uid"`
Address string `json:"address"`
}
type EntityUserScores struct {
Id int `json:"id"`
Uid int `json:"uid"`
Score int `json:"score"`
}
type Entity struct {
User EntityUser
UserDetail EntityUserDetail
UserScores []EntityUserScores
}
var users []*Entity
// User
all, err := db.Table(tableUser).Where("uid", g.Slice{3, 4}).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "User")
t.AssertNil(err)
t.Assert(len(users), 0)
// Detail
all, err = db.Table(tableUserDetail).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("uid asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserDetail", "User", "uid:Uid")
t.AssertNil(err)
// Scores
all, err = db.Table(tableUserScores).Where("uid", gdb.ListItemValues(users, "User", "Uid")).Order("id asc").All()
t.AssertNil(err)
err = all.ScanList(&users, "UserScores", "User", "UID:Uid")
t.AssertNil(err)
})
// Model ScanList with pointer elements and pointer attributes.
gtest.C(t, func(t *gtest.T) {
var users []*Entity
// User
err := db.Table(tableUser).
Where("uid", g.Slice{3, 4}).
Order("uid asc").
ScanList(&users, "User")
t.AssertNil(err)
// Detail
err = db.Table(tableUserDetail).
Where("uid", gdb.ListItemValues(users, "User", "Uid")).
Order("uid asc").
ScanList(&users, "UserDetail", "User", "uid:Uid")
t.AssertNil(err)
// Scores
err = db.Table(tableUserScores).
Where("uid", gdb.ListItemValues(users, "User", "Uid")).
Order("id asc").
ScanList(&users, "UserScores", "User", "uid:Uid")
t.AssertNil(err)
t.Assert(len(users), 0)
})
}
func Test_Table_Relation_EmbedStruct(t *testing.T) {
var (
tableUser = "user_" + gtime.TimestampMicroStr()
@ -531,29 +1060,31 @@ CREATE TABLE %s (
}
// Initialize the data.
var err error
for i := 1; i <= 5; i++ {
// User.
_, err = db.Insert(tableUser, g.Map{
"uid": i,
"name": fmt.Sprintf(`name_%d`, i),
})
gtest.Assert(err, nil)
// Detail.
_, err = db.Insert(tableUserDetail, g.Map{
"uid": i,
"address": fmt.Sprintf(`address_%d`, i),
})
gtest.Assert(err, nil)
// Scores.
for j := 1; j <= 5; j++ {
_, err = db.Insert(tableUserScores, g.Map{
"uid": i,
"score": j,
gtest.C(t, func(t *gtest.T) {
var err error
for i := 1; i <= 5; i++ {
// User.
_, err = db.Insert(tableUser, g.Map{
"uid": i,
"name": fmt.Sprintf(`name_%d`, i),
})
gtest.Assert(err, nil)
t.AssertNil(err)
// Detail.
_, err = db.Insert(tableUserDetail, g.Map{
"uid": i,
"address": fmt.Sprintf(`address_%d`, i),
})
t.AssertNil(err)
// Scores.
for j := 1; j <= 5; j++ {
_, err = db.Insert(tableUserScores, g.Map{
"uid": i,
"score": j,
})
t.AssertNil(err)
}
}
}
})
gtest.C(t, func(t *gtest.T) {
var (
@ -562,19 +1093,19 @@ CREATE TABLE %s (
)
// SELECT * FROM `user_scores`
err = db.Table(tableUserScores).Scan(&scores)
t.Assert(err, nil)
t.AssertNil(err)
// SELECT * FROM `user_scores` WHERE `uid` IN(1,2,3,4,5)
err = db.Table(tableUser).
Where("uid", gdb.ListItemValuesUnique(&scores, "Uid")).
ScanList(&scores, "EntityUser", "uid:Uid")
t.Assert(err, nil)
t.AssertNil(err)
// SELECT * FROM `user_detail` WHERE `uid` IN(1,2,3,4,5)
err = db.Table(tableUserDetail).
Where("uid", gdb.ListItemValuesUnique(&scores, "Uid")).
ScanList(&scores, "EntityUserDetail", "uid:Uid")
t.Assert(err, nil)
t.AssertNil(err)
// Assertions.
t.Assert(len(scores), 25)

View File

@ -19,7 +19,7 @@ func Test_Instance(t *testing.T) {
t.AssertNE(err, nil)
db, err := gdb.Instance()
t.Assert(err, nil)
t.AssertNil(err)
err1 := db.PingMaster()
err2 := db.PingSlave()

View File

@ -17,7 +17,7 @@ import (
func Test_Ctx(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
db, err := gdb.Instance()
t.Assert(err, nil)
t.AssertNil(err)
err1 := db.PingMaster()
err2 := db.PingSlave()

View File

@ -32,7 +32,7 @@ func init() {
"name": true,
"type": true,
}, false)
gtest.Assert(err, nil)
gtest.AssertNil(err)
configNode = ConfigNode{
Host: "127.0.0.1",
Port: "3306",
@ -329,7 +329,7 @@ func TestResult_Structs1(t *testing.T) {
}
array := make([]*B, 2)
err := r.Structs(&array)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(array[0].Id, 0)
t.Assert(array[1].Id, 0)
t.Assert(array[0].Name, "john")
@ -366,7 +366,7 @@ func Test_ScanList_NoRecreate_PtrAttribute(t *testing.T) {
},
}
err = r1.ScanList(&s, "One")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s[0].One.Name, "john")
t.Assert(s[0].One.Age, 16)
@ -384,7 +384,7 @@ func Test_ScanList_NoRecreate_PtrAttribute(t *testing.T) {
},
}
err = r2.ScanList(&s, "One", "One", "id:Id")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s[0].One.Name, "john")
t.Assert(s[0].One.Age, 20)
@ -422,7 +422,7 @@ func Test_ScanList_NoRecreate_StructAttribute(t *testing.T) {
},
}
err = r1.ScanList(&s, "One")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s[0].One.Name, "john")
t.Assert(s[0].One.Age, 16)
@ -440,7 +440,7 @@ func Test_ScanList_NoRecreate_StructAttribute(t *testing.T) {
},
}
err = r2.ScanList(&s, "One", "One", "id:Id")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s[0].One.Name, "john")
t.Assert(s[0].One.Age, 20)
@ -486,7 +486,7 @@ func Test_ScanList_NoRecreate_SliceAttribute_Ptr(t *testing.T) {
},
}
err = r1.ScanList(&s, "One")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s[0].One.Name, "john")
t.Assert(s[0].One.Age, 16)
@ -509,7 +509,7 @@ func Test_ScanList_NoRecreate_SliceAttribute_Ptr(t *testing.T) {
}
err = r2.ScanList(&s, "Many", "One", "pid:Id")
//fmt.Printf("%+v", err)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s[0].One.Name, "john")
t.Assert(s[0].One.Age, 16)
@ -537,7 +537,7 @@ func Test_ScanList_NoRecreate_SliceAttribute_Ptr(t *testing.T) {
}
err = r3.ScanList(&s, "Many", "One", "pid:Id")
//fmt.Printf("%+v", err)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s[0].One.Name, "john")
t.Assert(s[0].One.Age, 16)
@ -590,7 +590,7 @@ func Test_ScanList_NoRecreate_SliceAttribute_Struct(t *testing.T) {
},
}
err = r1.ScanList(&s, "One")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s[0].One.Name, "john")
t.Assert(s[0].One.Age, 16)
@ -613,7 +613,7 @@ func Test_ScanList_NoRecreate_SliceAttribute_Struct(t *testing.T) {
}
err = r2.ScanList(&s, "Many", "One", "pid:Id")
//fmt.Printf("%+v", err)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s[0].One.Name, "john")
t.Assert(s[0].One.Age, 16)
@ -641,7 +641,7 @@ func Test_ScanList_NoRecreate_SliceAttribute_Struct(t *testing.T) {
}
err = r3.ScanList(&s, "Many", "One", "pid:Id")
//fmt.Printf("%+v", err)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s[0].One.Name, "john")
t.Assert(s[0].One.Age, 16)

View File

@ -36,13 +36,13 @@ func Test_DB_Ping(t *testing.T) {
func Test_DB_Query(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
_, err := db.Query("SELECT ?", 1)
t.Assert(err, nil)
t.AssertNil(err)
_, err = db.Query("SELECT ?+?", 1, 2)
t.Assert(err, nil)
t.AssertNil(err)
_, err = db.Query("SELECT ?+?", g.Slice{1, 2})
t.Assert(err, nil)
t.AssertNil(err)
_, err = db.Query("ERROR")
t.AssertNE(err, nil)
@ -53,7 +53,7 @@ func Test_DB_Query(t *testing.T) {
func Test_DB_Exec(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
_, err := db.Exec("SELECT ?", 1)
t.Assert(err, nil)
t.AssertNil(err)
_, err = db.Exec("ERROR")
t.AssertNE(err, nil)
@ -64,17 +64,17 @@ func Test_DB_Exec(t *testing.T) {
func Test_DB_Prepare(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
st, err := db.Prepare("SELECT 100")
t.Assert(err, nil)
t.AssertNil(err)
rows, err := st.Query()
t.Assert(err, nil)
t.AssertNil(err)
array, err := rows.Columns()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(array[0], "100")
err = rows.Close()
t.Assert(err, nil)
t.AssertNil(err)
})
}
@ -90,7 +90,7 @@ func Test_DB_Insert(t *testing.T) {
"nickname": "T1",
"create_time": gtime.Now().String(),
})
t.Assert(err, nil)
t.AssertNil(err)
// normal map
result, err := db.Insert(table, g.Map{
@ -100,7 +100,7 @@ func Test_DB_Insert(t *testing.T) {
"nickname": "name_2",
"create_time": gtime.Now().String(),
})
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
@ -120,12 +120,12 @@ func Test_DB_Insert(t *testing.T) {
Nickname: "name_3",
CreateTime: timeStr,
})
t.Assert(err, nil)
t.AssertNil(err)
n, _ = result.RowsAffected()
t.Assert(n, 1)
one, err := db.Table(table).Where("id", 3).One()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["id"].Int(), 3)
t.Assert(one["passport"].String(), "user_3")
@ -142,12 +142,12 @@ func Test_DB_Insert(t *testing.T) {
Nickname: "name_4",
CreateTime: timeStr,
})
t.Assert(err, nil)
t.AssertNil(err)
n, _ = result.RowsAffected()
t.Assert(n, 1)
one, err = db.Table(table).Where("id", 4).One()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["id"].Int(), 4)
t.Assert(one["passport"].String(), "t4")
t.Assert(one["password"].String(), "25d55ad283aa400af464c76d713c07ad")
@ -172,12 +172,12 @@ func Test_DB_Insert(t *testing.T) {
"create_time": timeStr,
},
})
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
one, err = db.Table(table).Where("id", 200).One()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["id"].Int(), 200)
t.Assert(one["passport"].String(), "t200")
t.Assert(one["password"].String(), "25d55ad283aa400af464c76d71qw07ad")
@ -204,10 +204,10 @@ func Test_DB_Insert_WithStructAndSliceAttribute(t *testing.T) {
"create_time": gtime.Now().String(),
}
_, err := db.Insert(table, data)
t.Assert(err, nil)
t.AssertNil(err)
one, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["passport"], data["passport"])
t.Assert(one["create_time"], data["create_time"])
t.Assert(one["nickname"], gparser.MustToJson(data["nickname"]))
@ -234,10 +234,10 @@ func Test_DB_Insert_KeyFieldNameMapping(t *testing.T) {
CreateTime: "2020-10-10 12:00:01",
}
_, err := db.Insert(table, data)
t.Assert(err, nil)
t.AssertNil(err)
one, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["passport"], data.Passport)
t.Assert(one["create_time"], data.CreateTime)
t.Assert(one["nickname"], data.Nickname)
@ -264,10 +264,10 @@ func Test_DB_Upadte_KeyFieldNameMapping(t *testing.T) {
CreateTime: "2020-10-10 12:00:01",
}
_, err := db.Update(table, data, "id=1")
t.Assert(err, nil)
t.AssertNil(err)
one, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["passport"], data.Passport)
t.Assert(one["create_time"], data.CreateTime)
t.Assert(one["nickname"], data.Nickname)
@ -320,7 +320,7 @@ func Test_DB_InsertIgnore(t *testing.T) {
"nickname": "T1",
"create_time": gtime.Now().String(),
})
t.Assert(err, nil)
t.AssertNil(err)
})
}
@ -344,7 +344,7 @@ func Test_DB_BatchInsert(t *testing.T) {
"create_time": gtime.Now().String(),
},
}, 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 2)
@ -372,7 +372,7 @@ func Test_DB_BatchInsert(t *testing.T) {
"create_time": gtime.Now().String(),
},
}, 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 2)
})
@ -388,7 +388,7 @@ func Test_DB_BatchInsert(t *testing.T) {
"nickname": "T1",
"create_time": gtime.Now().String(),
})
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
})
@ -416,7 +416,7 @@ func Test_DB_BatchInsert_Struct(t *testing.T) {
CreateTime: gtime.Now(),
}
result, err := db.BatchInsert(table, user)
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
})
@ -435,10 +435,10 @@ func Test_DB_Save(t *testing.T) {
"nickname": "T11",
"create_time": timeStr,
})
t.Assert(err, nil)
t.AssertNil(err)
one, err := db.Table(table).Where("id", 1).One()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["id"].Int(), 1)
t.Assert(one["passport"].String(), "t1")
t.Assert(one["password"].String(), "25d55ad283aa400af464c76d713c07ad")
@ -460,10 +460,10 @@ func Test_DB_Replace(t *testing.T) {
"nickname": "T11",
"create_time": timeStr,
})
t.Assert(err, nil)
t.AssertNil(err)
one, err := db.Table(table).Where("id", 1).One()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["id"].Int(), 1)
t.Assert(one["passport"].String(), "t1")
t.Assert(one["password"].String(), "25d55ad283aa400af464c76d713c07ad")
@ -478,12 +478,12 @@ func Test_DB_Update(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
result, err := db.Update(table, "password='987654321'", "id=3")
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
one, err := db.Table(table).Where("id", 3).One()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["id"].Int(), 3)
t.Assert(one["passport"].String(), "user_3")
t.Assert(one["password"].String(), "987654321")
@ -497,19 +497,19 @@ func Test_DB_GetAll(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(result), 1)
t.Assert(result[0]["id"].Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), g.Slice{1})
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(result), 1)
t.Assert(result[0]["id"].Int(), 1)
})
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?)", table), g.Slice{1, 2, 3})
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(result), 3)
t.Assert(result[0]["id"].Int(), 1)
t.Assert(result[1]["id"].Int(), 2)
@ -517,7 +517,7 @@ func Test_DB_GetAll(t *testing.T) {
})
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3})
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(result), 3)
t.Assert(result[0]["id"].Int(), 1)
t.Assert(result[1]["id"].Int(), 2)
@ -525,7 +525,7 @@ func Test_DB_GetAll(t *testing.T) {
})
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id in(?,?,?)", table), g.Slice{1, 2, 3}...)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(result), 3)
t.Assert(result[0]["id"].Int(), 1)
t.Assert(result[1]["id"].Int(), 2)
@ -533,7 +533,7 @@ func Test_DB_GetAll(t *testing.T) {
})
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(fmt.Sprintf("SELECT * FROM %s WHERE id>=? AND id <=?", table), g.Slice{1, 3})
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(result), 3)
t.Assert(result[0]["id"].Int(), 1)
t.Assert(result[1]["id"].Int(), 2)
@ -546,7 +546,7 @@ func Test_DB_GetOne(t *testing.T) {
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
record, err := db.GetOne(fmt.Sprintf("SELECT * FROM %s WHERE passport=?", table), "user_1")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(record["nickname"].String(), "name_1")
})
}
@ -556,7 +556,7 @@ func Test_DB_GetValue(t *testing.T) {
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
value, err := db.GetValue(fmt.Sprintf("SELECT id FROM %s WHERE passport=?", table), "user_3")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(value.Int(), 3)
})
}
@ -566,7 +566,7 @@ func Test_DB_GetCount(t *testing.T) {
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
count, err := db.GetCount(fmt.Sprintf("SELECT * FROM %s", table))
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(count, SIZE)
})
}
@ -584,7 +584,7 @@ func Test_DB_GetStruct(t *testing.T) {
}
user := new(User)
err := db.GetStruct(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(user.NickName, "name_3")
})
gtest.C(t, func(t *gtest.T) {
@ -597,7 +597,7 @@ func Test_DB_GetStruct(t *testing.T) {
}
user := new(User)
err := db.GetStruct(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(user.NickName, "name_3")
})
}
@ -615,7 +615,7 @@ func Test_DB_GetStructs(t *testing.T) {
}
var users []User
err := db.GetStructs(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), SIZE-1)
t.Assert(users[0].Id, 2)
t.Assert(users[1].Id, 3)
@ -635,7 +635,7 @@ func Test_DB_GetStructs(t *testing.T) {
}
var users []User
err := db.GetStructs(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), SIZE-1)
t.Assert(users[0].Id, 2)
t.Assert(users[1].Id, 3)
@ -659,7 +659,7 @@ func Test_DB_GetScan(t *testing.T) {
}
user := new(User)
err := db.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(user.NickName, "name_3")
})
gtest.C(t, func(t *gtest.T) {
@ -672,7 +672,7 @@ func Test_DB_GetScan(t *testing.T) {
}
user := new(User)
err := db.GetScan(user, fmt.Sprintf("SELECT * FROM %s WHERE id=?", table), 3)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(user.NickName, "name_3")
})
@ -686,7 +686,7 @@ func Test_DB_GetScan(t *testing.T) {
}
var users []User
err := db.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), SIZE-1)
t.Assert(users[0].Id, 2)
t.Assert(users[1].Id, 3)
@ -706,7 +706,7 @@ func Test_DB_GetScan(t *testing.T) {
}
var users []User
err := db.GetScan(&users, fmt.Sprintf("SELECT * FROM %s WHERE id>?", table), 1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), SIZE-1)
t.Assert(users[0].Id, 2)
t.Assert(users[1].Id, 3)
@ -722,7 +722,7 @@ func Test_DB_Delete(t *testing.T) {
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.Delete(table, 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, SIZE)
})
@ -746,7 +746,7 @@ func Test_DB_Time(t *testing.T) {
n, _ := result.RowsAffected()
t.Assert(n, 1)
value, err := db.GetValue(fmt.Sprintf("select `passport` from `%s` where id=?", table), 200)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(value.String(), "t200")
})
@ -765,13 +765,13 @@ func Test_DB_Time(t *testing.T) {
n, _ := result.RowsAffected()
t.Assert(n, 1)
value, err := db.GetValue(fmt.Sprintf("select `passport` from `%s` where id=?", table), 300)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(value.String(), "t300")
})
gtest.C(t, func(t *gtest.T) {
result, err := db.Delete(table, 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 2)
})
@ -781,7 +781,7 @@ func Test_DB_ToJson(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
gtest.Assert(err, nil)
gtest.AssertNil(err)
gtest.C(t, func(t *gtest.T) {
result, err := db.Table(table).Fields("*").Where("id =? ", 1).Select()
@ -855,7 +855,7 @@ func Test_DB_ToXml(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
gtest.Assert(err, nil)
gtest.AssertNil(err)
gtest.C(t, func(t *gtest.T) {
record, err := db.Table(table).Fields("*").Where("id = ?", 1).One()
@ -921,7 +921,7 @@ func Test_DB_ToStringMap(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
gtest.Assert(err, nil)
gtest.AssertNil(err)
gtest.C(t, func(t *gtest.T) {
id := "1"
result, err := db.Table(table).Fields("*").Where("id = ?", 1).Select()
@ -957,7 +957,7 @@ func Test_DB_ToIntMap(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
gtest.Assert(err, nil)
gtest.AssertNil(err)
gtest.C(t, func(t *gtest.T) {
id := 1
@ -993,7 +993,7 @@ func Test_DB_ToUintMap(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
gtest.Assert(err, nil)
gtest.AssertNil(err)
gtest.C(t, func(t *gtest.T) {
id := 1
@ -1030,7 +1030,7 @@ func Test_DB_ToStringRecord(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
gtest.Assert(err, nil)
gtest.AssertNil(err)
gtest.C(t, func(t *gtest.T) {
id := 1
@ -1068,7 +1068,7 @@ func Test_DB_ToIntRecord(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
gtest.Assert(err, nil)
gtest.AssertNil(err)
gtest.C(t, func(t *gtest.T) {
id := 1
@ -1105,7 +1105,7 @@ func Test_DB_ToUintRecord(t *testing.T) {
table := createInitTable()
defer dropTable(table)
_, err := db.Update(table, "create_time='2010-10-10 00:00:01'", "id=?", 1)
gtest.Assert(err, nil)
gtest.AssertNil(err)
gtest.C(t, func(t *gtest.T) {
id := 1
@ -1205,7 +1205,7 @@ func Test_DB_Prefix(t *testing.T) {
"nickname": fmt.Sprintf(`name_%d`, id),
"create_time": gtime.NewFromStr("2018-10-24 10:00:00").String(),
})
t.Assert(err, nil)
t.AssertNil(err)
n, e := result.RowsAffected()
t.Assert(e, nil)
@ -1221,7 +1221,7 @@ func Test_DB_Prefix(t *testing.T) {
"nickname": fmt.Sprintf(`name_%d`, id),
"create_time": gtime.NewFromStr("2018-10-24 10:00:01").String(),
})
t.Assert(err, nil)
t.AssertNil(err)
n, e := result.RowsAffected()
t.Assert(e, nil)
@ -1237,7 +1237,7 @@ func Test_DB_Prefix(t *testing.T) {
"nickname": fmt.Sprintf(`name_%d`, id),
"create_time": gtime.NewFromStr("2018-10-24 10:00:02").String(),
})
t.Assert(err, nil)
t.AssertNil(err)
n, e := result.RowsAffected()
t.Assert(e, nil)
@ -1253,7 +1253,7 @@ func Test_DB_Prefix(t *testing.T) {
"nickname": fmt.Sprintf(`name_%d`, id),
"create_time": gtime.NewFromStr("2018-10-24 10:00:03").String(),
}, "id=?", id)
t.Assert(err, nil)
t.AssertNil(err)
n, e := result.RowsAffected()
t.Assert(e, nil)
@ -1263,7 +1263,7 @@ func Test_DB_Prefix(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
id := 10000
result, err := db.Delete(name, "id=?", id)
t.Assert(err, nil)
t.AssertNil(err)
n, e := result.RowsAffected()
t.Assert(e, nil)
@ -1283,7 +1283,7 @@ func Test_DB_Prefix(t *testing.T) {
}
result, err := db.BatchInsert(name, array.Slice())
t.Assert(err, nil)
t.AssertNil(err)
n, e := result.RowsAffected()
t.Assert(e, nil)
@ -1403,7 +1403,7 @@ func Test_Empty_Slice_Argument(t *testing.T) {
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
result, err := db.GetAll(fmt.Sprintf(`select * from %s where id in(?)`, table), g.Slice{})
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(result), 0)
})
}
@ -1430,7 +1430,7 @@ func Test_DB_UpdateCounter(t *testing.T) {
"updated_time": 0,
}
_, err = db.Insert(tableName, insertData)
t.Assert(err, nil)
t.AssertNil(err)
})
gtest.C(t, func(t *gtest.T) {
@ -1442,11 +1442,11 @@ func Test_DB_UpdateCounter(t *testing.T) {
"views": gdbCounter,
}
result, err := db.Update(tableName, updateData, "id", 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
one, err := db.Table(tableName).Where("id", 1).One()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["id"].Int(), 1)
t.Assert(one["views"].Int(), 1)
})
@ -1461,11 +1461,11 @@ func Test_DB_UpdateCounter(t *testing.T) {
"updated_time": gtime.Now().Unix(),
}
result, err := db.Update(tableName, updateData, "id", 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
one, err := db.Table(tableName).Where("id", 1).One()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["id"].Int(), 1)
t.Assert(one["views"].Int(), 0)
})
@ -1486,6 +1486,6 @@ func Test_DB_Ctx_Logger(t *testing.T) {
db.SetDebug(true)
ctx := context.WithValue(context.Background(), "Trace-Id", "123456789")
_, err := db.Ctx(ctx).Query("SELECT 1")
t.Assert(err, nil)
t.AssertNil(err)
})
}

File diff suppressed because it is too large Load Diff

View File

@ -27,7 +27,7 @@ func Test_Insert_Raw(t *testing.T) {
"nickname": "name_1",
"create_time": gdb.Raw("now()"),
}).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.LastInsertId()
t.Assert(n, 2)
})
@ -57,7 +57,7 @@ func Test_BatchInsert_Raw(t *testing.T) {
},
},
).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.LastInsertId()
t.Assert(n, 4)
})
@ -73,14 +73,14 @@ func Test_Update_Raw(t *testing.T) {
"id": gdb.Raw("id+100"),
"create_time": gdb.Raw("now()"),
}).Where("id", 1).Update()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
})
gtest.C(t, func(t *gtest.T) {
user := db.Table(table)
n, err := user.Where("id", 101).Count()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(n, 1)
})
}

View File

@ -41,11 +41,11 @@ func Test_Model_Inherit_Insert(t *testing.T) {
CreateTime: gtime.Now().String(),
},
}).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
value, err := db.Table(table).Fields("passport").Where("id=100").Value()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(value.String(), "john-test")
})
}
@ -78,12 +78,12 @@ func Test_Model_Inherit_MapToStruct(t *testing.T) {
"create_time": gtime.Now().String(),
}
result, err := db.Table(table).Filter().Data(data).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
one, err := db.Table(table).Where("id=100").One()
t.Assert(err, nil)
t.AssertNil(err)
user := new(User)
@ -110,10 +110,10 @@ func Test_Struct_Pointer_Attribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
user := new(User)
err = one.Struct(user)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(*user.Id, 1)
t.Assert(*user.Passport, "user_1")
t.Assert(*user.Password, "pass_1")
@ -122,7 +122,7 @@ func Test_Struct_Pointer_Attribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
user := new(User)
err := db.Table(table).Struct(user, "id=1")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(*user.Id, 1)
t.Assert(*user.Passport, "user_1")
t.Assert(*user.Password, "pass_1")
@ -131,7 +131,7 @@ func Test_Struct_Pointer_Attribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var user *User
err := db.Table(table).Struct(&user, "id=1")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(*user.Id, 1)
t.Assert(*user.Passport, "user_1")
t.Assert(*user.Password, "pass_1")
@ -152,10 +152,10 @@ func Test_Structs_Pointer_Attribute(t *testing.T) {
// All
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).All("id < 3")
t.Assert(err, nil)
t.AssertNil(err)
users := make([]User, 0)
err = one.Structs(&users)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(*users[0].Id, 1)
t.Assert(*users[0].Passport, "user_1")
@ -164,10 +164,10 @@ func Test_Structs_Pointer_Attribute(t *testing.T) {
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).All("id < 3")
t.Assert(err, nil)
t.AssertNil(err)
users := make([]*User, 0)
err = one.Structs(&users)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(*users[0].Id, 1)
t.Assert(*users[0].Passport, "user_1")
@ -177,9 +177,9 @@ func Test_Structs_Pointer_Attribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var users []User
one, err := db.Table(table).All("id < 3")
t.Assert(err, nil)
t.AssertNil(err)
err = one.Structs(&users)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(*users[0].Id, 1)
t.Assert(*users[0].Passport, "user_1")
@ -189,9 +189,9 @@ func Test_Structs_Pointer_Attribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var users []*User
one, err := db.Table(table).All("id < 3")
t.Assert(err, nil)
t.AssertNil(err)
err = one.Structs(&users)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(*users[0].Id, 1)
t.Assert(*users[0].Passport, "user_1")
@ -202,7 +202,7 @@ func Test_Structs_Pointer_Attribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
users := make([]User, 0)
err := db.Table(table).Structs(&users, "id < 3")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(*users[0].Id, 1)
t.Assert(*users[0].Passport, "user_1")
@ -212,7 +212,7 @@ func Test_Structs_Pointer_Attribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
users := make([]*User, 0)
err := db.Table(table).Structs(&users, "id < 3")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(*users[0].Id, 1)
t.Assert(*users[0].Passport, "user_1")
@ -222,7 +222,7 @@ func Test_Structs_Pointer_Attribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var users []User
err := db.Table(table).Structs(&users, "id < 3")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(*users[0].Id, 1)
t.Assert(*users[0].Passport, "user_1")
@ -232,7 +232,7 @@ func Test_Structs_Pointer_Attribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var users []*User
err := db.Table(table).Structs(&users, "id < 3")
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(users), 2)
t.Assert(*users[0].Id, 1)
t.Assert(*users[0].Passport, "user_1")
@ -261,7 +261,7 @@ func Test_Struct_Empty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).Where("id=100").One()
t.Assert(err, nil)
t.AssertNil(err)
var user *User
t.Assert(one.Struct(&user), nil)
t.Assert(user, nil)
@ -270,7 +270,7 @@ func Test_Struct_Empty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var user *User
err := db.Table(table).Where("id=100").Struct(&user)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(user, nil)
})
}
@ -288,38 +288,38 @@ func Test_Structs_Empty(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
all, err := db.Table(table).Where("id>100").All()
t.Assert(err, nil)
t.AssertNil(err)
users := make([]User, 0)
t.Assert(all.Structs(&users), nil)
})
gtest.C(t, func(t *gtest.T) {
all, err := db.Table(table).Where("id>100").All()
t.Assert(err, nil)
t.AssertNil(err)
users := make([]User, 10)
t.AssertNE(all.Structs(&users), nil)
})
gtest.C(t, func(t *gtest.T) {
all, err := db.Table(table).Where("id>100").All()
t.Assert(err, nil)
t.AssertNil(err)
var users []User
t.Assert(all.Structs(&users), nil)
})
gtest.C(t, func(t *gtest.T) {
all, err := db.Table(table).Where("id>100").All()
t.Assert(err, nil)
t.AssertNil(err)
users := make([]*User, 0)
t.Assert(all.Structs(&users), nil)
})
gtest.C(t, func(t *gtest.T) {
all, err := db.Table(table).Where("id>100").All()
t.Assert(err, nil)
t.AssertNil(err)
users := make([]*User, 10)
t.Assert(all.Structs(&users), nil)
})
gtest.C(t, func(t *gtest.T) {
all, err := db.Table(table).Where("id>100").All()
t.Assert(err, nil)
t.AssertNil(err)
var users []*User
t.Assert(all.Structs(&users), nil)
})
@ -349,13 +349,13 @@ func Test_Model_Scan_CustomType(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
st := new(MyTimeSt)
err := db.Table(table).Fields("create_time").Scan(st)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(st.CreateTime.String(), "2018-10-24 10:00:00")
})
gtest.C(t, func(t *gtest.T) {
var stSlice []*MyTimeSt
err := db.Table(table).Fields("create_time").Scan(&stSlice)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(stSlice), SIZE)
t.Assert(stSlice[0].CreateTime.String(), "2018-10-24 10:00:00")
t.Assert(stSlice[9].CreateTime.String(), "2018-10-24 10:00:00")

View File

@ -41,12 +41,12 @@ CREATE TABLE %s (
"name": "name_1",
}
r, err := db.Table(table).Data(dataInsert).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 1)
oneInsert, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneInsert["id"].Int(), 1)
t.Assert(oneInsert["name"].String(), "name_1")
t.Assert(oneInsert["delete_at"].String(), "")
@ -62,12 +62,12 @@ CREATE TABLE %s (
"name": "name_10",
}
r, err = db.Table(table).Data(dataSave).Save()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
oneSave, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneSave["id"].Int(), 1)
t.Assert(oneSave["name"].String(), "name_10")
t.Assert(oneSave["delete_at"].String(), "")
@ -83,12 +83,12 @@ CREATE TABLE %s (
"name": "name_1000",
}
r, err = db.Table(table).Data(dataUpdate).WherePri(1).Update()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
oneUpdate, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneUpdate["id"].Int(), 1)
t.Assert(oneUpdate["name"].String(), "name_1000")
t.Assert(oneUpdate["delete_at"].String(), "")
@ -101,12 +101,12 @@ CREATE TABLE %s (
"name": "name_100",
}
r, err = db.Table(table).Data(dataReplace).Replace()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
oneReplace, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneReplace["id"].Int(), 1)
t.Assert(oneReplace["name"].String(), "name_100")
t.Assert(oneReplace["delete_at"].String(), "")
@ -118,35 +118,35 @@ CREATE TABLE %s (
// Delete
r, err = db.Table(table).Delete("id", 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
// Delete Select
one4, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(one4), 0)
one5, err := db.Table(table).Unscoped().FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one5["id"].Int(), 1)
t.AssertGE(one5["delete_at"].GTime().Timestamp(), gtime.Timestamp()-2)
// Delete Count
i, err := db.Table(table).FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 0)
i, err = db.Table(table).Unscoped().FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 1)
// Delete Unscoped
r, err = db.Table(table).Unscoped().Delete("id", 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
one6, err := db.Table(table).Unscoped().FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(one6), 0)
i, err = db.Table(table).Unscoped().FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 0)
})
}
@ -175,12 +175,12 @@ CREATE TABLE %s (
"name": "name_1",
}
r, err := db.Table(table).Data(dataInsert).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 1)
oneInsert, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneInsert["id"].Int(), 1)
t.Assert(oneInsert["name"].String(), "name_1")
t.Assert(oneInsert["deleted_at"].String(), "")
@ -196,12 +196,12 @@ CREATE TABLE %s (
"name": "name_10",
}
r, err = db.Table(table).Data(dataSave).Save()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
oneSave, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneSave["id"].Int(), 1)
t.Assert(oneSave["name"].String(), "name_10")
t.Assert(oneSave["deleted_at"].String(), "")
@ -217,12 +217,12 @@ CREATE TABLE %s (
"name": "name_1000",
}
r, err = db.Table(table).Data(dataUpdate).WherePri(1).Update()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
oneUpdate, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneUpdate["id"].Int(), 1)
t.Assert(oneUpdate["name"].String(), "name_1000")
t.Assert(oneUpdate["deleted_at"].String(), "")
@ -235,12 +235,12 @@ CREATE TABLE %s (
"name": "name_100",
}
r, err = db.Table(table).Data(dataReplace).Replace()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
oneReplace, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneReplace["id"].Int(), 1)
t.Assert(oneReplace["name"].String(), "name_100")
t.Assert(oneReplace["deleted_at"].String(), "")
@ -252,35 +252,35 @@ CREATE TABLE %s (
// Delete
r, err = db.Table(table).Delete("id", 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
// Delete Select
one4, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(one4), 0)
one5, err := db.Table(table).Unscoped().FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one5["id"].Int(), 1)
t.AssertGE(one5["deleted_at"].GTime().Timestamp(), gtime.Timestamp()-2)
// Delete Count
i, err := db.Table(table).FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 0)
i, err = db.Table(table).Unscoped().FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 1)
// Delete Unscoped
r, err = db.Table(table).Unscoped().Delete("id", 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
one6, err := db.Table(table).Unscoped().FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(one6), 0)
i, err = db.Table(table).Unscoped().FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 0)
})
}
@ -316,12 +316,12 @@ CREATE TABLE %s (
Name: "name_1",
}
r, err := db.Table(table).Data(dataInsert).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 1)
oneInsert, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneInsert["id"].Int(), 1)
t.Assert(oneInsert["name"].String(), "name_1")
t.Assert(oneInsert["deleted_at"].String(), "")
@ -337,12 +337,12 @@ CREATE TABLE %s (
Name: "name_10",
}
r, err = db.Table(table).Data(dataSave).OmitEmpty().Save()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
oneSave, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneSave["id"].Int(), 1)
t.Assert(oneSave["name"].String(), "name_10")
t.Assert(oneSave["deleted_at"].String(), "")
@ -358,12 +358,12 @@ CREATE TABLE %s (
Name: "name_1000",
}
r, err = db.Table(table).Data(dataUpdate).OmitEmpty().WherePri(1).Update()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
oneUpdate, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneUpdate["id"].Int(), 1)
t.Assert(oneUpdate["name"].String(), "name_1000")
t.Assert(oneUpdate["deleted_at"].String(), "")
@ -376,12 +376,12 @@ CREATE TABLE %s (
Name: "name_100",
}
r, err = db.Table(table).Data(dataReplace).OmitEmpty().Replace()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
oneReplace, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneReplace["id"].Int(), 1)
t.Assert(oneReplace["name"].String(), "name_100")
t.Assert(oneReplace["deleted_at"].String(), "")
@ -393,35 +393,35 @@ CREATE TABLE %s (
// Delete
r, err = db.Table(table).Delete("id", 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
// Delete Select
one4, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(one4), 0)
one5, err := db.Table(table).Unscoped().FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one5["id"].Int(), 1)
t.AssertGE(one5["deleted_at"].GTime().Timestamp(), gtime.Timestamp()-2)
// Delete Count
i, err := db.Table(table).FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 0)
i, err = db.Table(table).Unscoped().FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 1)
// Delete Unscoped
r, err = db.Table(table).Unscoped().Delete("id", 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
one6, err := db.Table(table).Unscoped().FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(one6), 0)
i, err = db.Table(table).Unscoped().FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 0)
})
}
@ -449,18 +449,18 @@ CREATE TABLE %s (
"num": 10,
}
r, err := db.Table(table).Data(dataInsert).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 1)
oneInsert, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneInsert["id"].Int(), 1)
t.Assert(oneInsert["num"].Int(), 10)
// Update.
r, err = db.Table(table).Data("num=num+1").Where("id=?", 1).Update()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
})
@ -489,21 +489,21 @@ CREATE TABLE %s (
"name": fmt.Sprintf("name_%d", i),
}
r, err := db.Table(table).Data(data).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 1)
}
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.AssertNE(one["create_at"].String(), "")
t.AssertNE(one["update_at"].String(), "")
t.Assert(one["delete_at"].String(), "")
})
gtest.C(t, func(t *gtest.T) {
one, err := db.Table(table).FindOne(10)
t.Assert(err, nil)
t.AssertNil(err)
t.AssertNE(one["create_at"].String(), "")
t.AssertNE(one["update_at"].String(), "")
t.Assert(one["delete_at"].String(), "")
@ -511,16 +511,16 @@ CREATE TABLE %s (
gtest.C(t, func(t *gtest.T) {
ids := g.SliceInt{1, 3, 5}
r, err := db.Table(table).Where("id", ids).Delete()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 3)
count, err := db.Table(table).FindCount(ids)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(count, 0)
all, err := db.Table(table).Unscoped().FindAll(ids)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(all), 3)
t.AssertNE(all[0]["create_at"].String(), "")
t.AssertNE(all[0]["update_at"].String(), "")
@ -572,7 +572,7 @@ CREATE TABLE %s (
"name": "name_1",
}
r, err := db.Table(table1).Data(dataInsert1).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 1)
@ -581,26 +581,26 @@ CREATE TABLE %s (
"name": "name_2",
}
r, err = db.Table(table2).Data(dataInsert2).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
one, err := db.Table(table1, "t1").LeftJoin(table2, "t2", "t2.id=t1.id").Fields("t1.name").FindOne()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["name"], "name_1")
// Soft deleting.
r, err = db.Table(table1).Delete()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
one, err = db.Table(table1, "t1").LeftJoin(table2, "t2", "t2.id=t1.id").Fields("t1.name").FindOne()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one.IsEmpty(), true)
one, err = db.Table(table2, "t2").LeftJoin(table1, "t1", "t2.id=t1.id").Fields("t2.name").FindOne()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one.IsEmpty(), true)
})
}
@ -629,7 +629,7 @@ CREATE TABLE %s (
"name": fmt.Sprintf("name_%d", i),
}
r, err := db.Table(table).Data(data).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 1)
}
@ -637,12 +637,12 @@ CREATE TABLE %s (
gtest.C(t, func(t *gtest.T) {
ids := g.SliceInt{1, 3, 5}
r, err := db.Table(table).Where("id", ids).Delete()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 3)
count, err := db.Table(table).Where("id", 1).Or("id", 3).Count()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(count, 0)
})
}
@ -680,12 +680,12 @@ CREATE TABLE %s (
DeleteAt: nil,
}
r, err := db.Table(table).Data(dataInsert).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 1)
oneInsert, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneInsert["id"].Int(), 1)
t.Assert(oneInsert["name"].String(), "name_1")
t.Assert(oneInsert["delete_at"].String(), "")
@ -703,12 +703,12 @@ CREATE TABLE %s (
DeleteAt: nil,
}
r, err = db.Table(table).Data(dataSave).Save()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
oneSave, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneSave["id"].Int(), 1)
t.Assert(oneSave["name"].String(), "name_10")
t.Assert(oneSave["delete_at"].String(), "")
@ -727,12 +727,12 @@ CREATE TABLE %s (
DeleteAt: nil,
}
r, err = db.Table(table).Data(dataUpdate).WherePri(1).Update()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
oneUpdate, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneUpdate["id"].Int(), 1)
t.Assert(oneUpdate["name"].String(), "name_1000")
t.Assert(oneUpdate["delete_at"].String(), "")
@ -748,12 +748,12 @@ CREATE TABLE %s (
DeleteAt: nil,
}
r, err = db.Table(table).Data(dataReplace).Replace()
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 2)
oneReplace, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(oneReplace["id"].Int(), 1)
t.Assert(oneReplace["name"].String(), "name_100")
t.Assert(oneReplace["delete_at"].String(), "")
@ -764,35 +764,35 @@ CREATE TABLE %s (
// Delete
r, err = db.Table(table).Delete("id", 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
// Delete Select
one4, err := db.Table(table).FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(one4), 0)
one5, err := db.Table(table).Unscoped().FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one5["id"].Int(), 1)
t.AssertGE(one5["delete_at"].GTime().Timestamp(), gtime.Timestamp()-2)
// Delete Count
i, err := db.Table(table).FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 0)
i, err = db.Table(table).Unscoped().FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 1)
// Delete Unscoped
r, err = db.Table(table).Unscoped().Delete("id", 1)
t.Assert(err, nil)
t.AssertNil(err)
n, _ = r.RowsAffected()
t.Assert(n, 1)
one6, err := db.Table(table).Unscoped().FindOne(1)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(len(one6), 0)
i, err = db.Table(table).Unscoped().FindCount()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(i, 0)
})
}

View File

@ -90,23 +90,23 @@ func Test_TX_Rollback(t *testing.T) {
func Test_TX_Prepare(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
tx, err := db.Begin()
t.Assert(err, nil)
t.AssertNil(err)
st, err := tx.Prepare("SELECT 100")
t.Assert(err, nil)
t.AssertNil(err)
rows, err := st.Query()
t.Assert(err, nil)
t.AssertNil(err)
array, err := rows.Columns()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(array[0], "100")
rows.Close()
t.Assert(err, nil)
t.AssertNil(err)
tx.Commit()
t.Assert(err, nil)
t.AssertNil(err)
})
}
@ -752,7 +752,7 @@ func Test_Transaction(t *testing.T) {
}
return nil
})
t.Assert(err, nil)
t.AssertNil(err)
if value, err := db.Table(table).Fields("nickname").Where("id", 1).Value(); err != nil {
gtest.Error(err)

View File

@ -59,12 +59,12 @@ func Test_Types(t *testing.T) {
"bool": false,
}
r, err := db.Table("types").Data(data).Insert()
t.Assert(err, nil)
t.AssertNil(err)
n, _ := r.RowsAffected()
t.Assert(n, 1)
one, err := db.Table("types").One()
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(one["id"].Int(), 1)
t.Assert(one["blob"].String(), data["blob"])
t.Assert(one["binary"].String(), data["binary"])
@ -88,7 +88,7 @@ func Test_Types(t *testing.T) {
}
var obj *T
err = db.Table("types").Struct(&obj)
t.Assert(err, nil)
t.AssertNil(err)
t.Assert(obj.Id, 1)
t.Assert(obj.Blob, data["blob"])
t.Assert(obj.Binary, data["binary"])

View File

@ -15,74 +15,79 @@ type T struct {
*testing.T
}
// Assert checks <value> and <expect> EQUAL.
// Assert checks `value` and `expect` EQUAL.
func (t *T) Assert(value, expect interface{}) {
Assert(value, expect)
}
// AssertEQ checks <value> and <expect> EQUAL, including their TYPES.
// AssertEQ checks `value` and `expect` EQUAL, including their TYPES.
func (t *T) AssertEQ(value, expect interface{}) {
AssertEQ(value, expect)
}
// AssertNE checks <value> and <expect> NOT EQUAL.
// AssertNE checks `value` and `expect` NOT EQUAL.
func (t *T) AssertNE(value, expect interface{}) {
AssertNE(value, expect)
}
// AssertNQ checks <value> and <expect> NOT EQUAL, including their TYPES.
// AssertNQ checks `value` and `expect` NOT EQUAL, including their TYPES.
func (t *T) AssertNQ(value, expect interface{}) {
AssertNQ(value, expect)
}
// AssertGT checks <value> is GREATER THAN <expect>.
// AssertGT checks `value` is GREATER THAN `expect`.
// Notice that, only string, integer and float types can be compared by AssertGT,
// others are invalid.
func (t *T) AssertGT(value, expect interface{}) {
AssertGT(value, expect)
}
// AssertGE checks <value> is GREATER OR EQUAL THAN <expect>.
// AssertGE checks `value` is GREATER OR EQUAL THAN `expect`.
// Notice that, only string, integer and float types can be compared by AssertGTE,
// others are invalid.
func (t *T) AssertGE(value, expect interface{}) {
AssertGE(value, expect)
}
// AssertLT checks <value> is LESS EQUAL THAN <expect>.
// AssertLT checks `value` is LESS EQUAL THAN `expect`.
// Notice that, only string, integer and float types can be compared by AssertLT,
// others are invalid.
func (t *T) AssertLT(value, expect interface{}) {
AssertLT(value, expect)
}
// AssertLE checks <value> is LESS OR EQUAL THAN <expect>.
// AssertLE checks `value` is LESS OR EQUAL THAN `expect`.
// Notice that, only string, integer and float types can be compared by AssertLTE,
// others are invalid.
func (t *T) AssertLE(value, expect interface{}) {
AssertLE(value, expect)
}
// AssertIN checks <value> is IN <expect>.
// The <expect> should be a slice,
// but the <value> can be a slice or a basic type variable.
// AssertIN checks `value` is IN `expect`.
// The `expect` should be a slice,
// but the `value` can be a slice or a basic type variable.
func (t *T) AssertIN(value, expect interface{}) {
AssertIN(value, expect)
}
// AssertNI checks <value> is NOT IN <expect>.
// The <expect> should be a slice,
// but the <value> can be a slice or a basic type variable.
// AssertNI checks `value` is NOT IN `expect`.
// The `expect` should be a slice,
// but the `value` can be a slice or a basic type variable.
func (t *T) AssertNI(value, expect interface{}) {
AssertNI(value, expect)
}
// Error panics with given <message>.
// AssertNil asserts `value` is nil.
func (t *T) AssertNil(value interface{}) {
AssertNil(value)
}
// Error panics with given `message`.
func (t *T) Error(message ...interface{}) {
Error(message...)
}
// Fatal prints <message> to stderr and exit the process.
// Fatal prints `message` to stderr and exit the process.
func (t *T) Fatal(message ...interface{}) {
Fatal(message...)
}

View File

@ -8,6 +8,7 @@ package gtest
import (
"fmt"
"github.com/gogf/gf/internal/empty"
"os"
"reflect"
"testing"
@ -18,16 +19,16 @@ import (
)
const (
gPATH_FILTER_KEY = "/test/gtest/gtest"
pathFilterKey = "/test/gtest/gtest"
)
// C creates an unit testing case.
// The parameter <t> is the pointer to testing.T of stdlib (*testing.T).
// The parameter <f> is the closure function for unit testing case.
// The parameter `t` is the pointer to testing.T of stdlib (*testing.T).
// The parameter `f` is the closure function for unit testing case.
func C(t *testing.T, f func(t *T)) {
defer func() {
if err := recover(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n%s", err, gdebug.StackWithFilter(gPATH_FILTER_KEY))
fmt.Fprintf(os.Stderr, "%v\n%s", err, gdebug.StackWithFilter(pathFilterKey))
t.Fail()
}
}()
@ -35,23 +36,23 @@ func C(t *testing.T, f func(t *T)) {
}
// Case creates an unit testing case.
// The parameter <t> is the pointer to testing.T of stdlib (*testing.T).
// The parameter <f> is the closure function for unit testing case.
// The parameter `t` is the pointer to testing.T of stdlib (*testing.T).
// The parameter `f` is the closure function for unit testing case.
// Deprecated.
func Case(t *testing.T, f func()) {
defer func() {
if err := recover(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n%s", err, gdebug.StackWithFilter(gPATH_FILTER_KEY))
fmt.Fprintf(os.Stderr, "%v\n%s", err, gdebug.StackWithFilter(pathFilterKey))
t.Fail()
}
}()
f()
}
// Assert checks <value> and <expect> EQUAL.
// Assert checks `value` and `expect` EQUAL.
func Assert(value, expect interface{}) {
rvExpect := reflect.ValueOf(expect)
if isNil(value) {
if empty.IsNil(value) {
value = nil
}
if rvExpect.Kind() == reflect.Map {
@ -69,11 +70,11 @@ func Assert(value, expect interface{}) {
}
}
// AssertEQ checks <value> and <expect> EQUAL, including their TYPES.
// AssertEQ checks `value` and `expect` EQUAL, including their TYPES.
func AssertEQ(value, expect interface{}) {
// Value assert.
rvExpect := reflect.ValueOf(expect)
if isNil(value) {
if empty.IsNil(value) {
value = nil
}
if rvExpect.Kind() == reflect.Map {
@ -95,10 +96,10 @@ func AssertEQ(value, expect interface{}) {
}
}
// AssertNE checks <value> and <expect> NOT EQUAL.
// AssertNE checks `value` and `expect` NOT EQUAL.
func AssertNE(value, expect interface{}) {
rvExpect := reflect.ValueOf(expect)
if isNil(value) {
if empty.IsNil(value) {
value = nil
}
if rvExpect.Kind() == reflect.Map {
@ -116,7 +117,7 @@ func AssertNE(value, expect interface{}) {
}
}
// AssertNQ checks <value> and <expect> NOT EQUAL, including their TYPES.
// AssertNQ checks `value` and `expect` NOT EQUAL, including their TYPES.
func AssertNQ(value, expect interface{}) {
// Type assert.
t1 := reflect.TypeOf(value)
@ -133,7 +134,7 @@ func AssertNQ(value, expect interface{}) {
AssertNE(value, expect)
}
// AssertGT checks <value> is GREATER THAN <expect>.
// AssertGT checks `value` is GREATER THAN `expect`.
// Notice that, only string, integer and float types can be compared by AssertGT,
// others are invalid.
func AssertGT(value, expect interface{}) {
@ -156,7 +157,7 @@ func AssertGT(value, expect interface{}) {
}
}
// AssertGE checks <value> is GREATER OR EQUAL THAN <expect>.
// AssertGE checks `value` is GREATER OR EQUAL THAN `expect`.
// Notice that, only string, integer and float types can be compared by AssertGTE,
// others are invalid.
func AssertGE(value, expect interface{}) {
@ -183,7 +184,7 @@ func AssertGE(value, expect interface{}) {
}
}
// AssertLT checks <value> is LESS EQUAL THAN <expect>.
// AssertLT checks `value` is LESS EQUAL THAN `expect`.
// Notice that, only string, integer and float types can be compared by AssertLT,
// others are invalid.
func AssertLT(value, expect interface{}) {
@ -206,7 +207,7 @@ func AssertLT(value, expect interface{}) {
}
}
// AssertLE checks <value> is LESS OR EQUAL THAN <expect>.
// AssertLE checks `value` is LESS OR EQUAL THAN `expect`.
// Notice that, only string, integer and float types can be compared by AssertLTE,
// others are invalid.
func AssertLE(value, expect interface{}) {
@ -229,13 +230,15 @@ func AssertLE(value, expect interface{}) {
}
}
// AssertIN checks <value> is IN <expect>.
// The <expect> should be a slice,
// but the <value> can be a slice or a basic type variable.
// AssertIN checks `value` is IN `expect`.
// The `expect` should be a slice,
// but the `value` can be a slice or a basic type variable.
// TODO map support.
func AssertIN(value, expect interface{}) {
passed := true
expectKind := reflect.ValueOf(expect).Kind()
var (
passed = true
expectKind = reflect.ValueOf(expect).Kind()
)
switch expectKind {
case reflect.Slice, reflect.Array:
expectSlice := gconv.Strings(expect)
@ -260,13 +263,15 @@ func AssertIN(value, expect interface{}) {
}
}
// AssertNI checks <value> is NOT IN <expect>.
// The <expect> should be a slice,
// but the <value> can be a slice or a basic type variable.
// AssertNI checks `value` is NOT IN `expect`.
// The `expect` should be a slice,
// but the `value` can be a slice or a basic type variable.
// TODO map support.
func AssertNI(value, expect interface{}) {
passed := true
expectKind := reflect.ValueOf(expect).Kind()
var (
passed = true
expectKind = reflect.ValueOf(expect).Kind()
)
switch expectKind {
case reflect.Slice, reflect.Array:
for _, v1 := range gconv.Strings(value) {
@ -290,22 +295,24 @@ func AssertNI(value, expect interface{}) {
}
}
// Error panics with given <message>.
// Error panics with given `message`.
func Error(message ...interface{}) {
panic(fmt.Sprintf("[ERROR] %s", fmt.Sprint(message...)))
}
// Fatal prints <message> to stderr and exit the process.
// Fatal prints `message` to stderr and exit the process.
func Fatal(message ...interface{}) {
fmt.Fprintf(os.Stderr, "[FATAL] %s\n%s", fmt.Sprint(message...), gdebug.StackWithFilter(gPATH_FILTER_KEY))
fmt.Fprintf(os.Stderr, "[FATAL] %s\n%s", fmt.Sprint(message...), gdebug.StackWithFilter(pathFilterKey))
os.Exit(1)
}
// compareMap compares two maps, returns nil if they are equal, or else returns error.
func compareMap(value, expect interface{}) error {
rvValue := reflect.ValueOf(value)
rvExpect := reflect.ValueOf(expect)
if isNil(value) {
var (
rvValue = reflect.ValueOf(value)
rvExpect = reflect.ValueOf(expect)
)
if empty.IsNil(value) {
value = nil
}
if rvExpect.Kind() == reflect.Map {
@ -340,13 +347,13 @@ func compareMap(value, expect interface{}) error {
return nil
}
// isNil checks whether <value> is nil.
func isNil(value interface{}) bool {
rv := reflect.ValueOf(value)
switch rv.Kind() {
case reflect.Slice, reflect.Array, reflect.Map, reflect.Ptr, reflect.Func:
return rv.IsNil()
default:
return value == nil
// AssertNil asserts `value` is nil.
func AssertNil(value interface{}) {
if empty.IsNil(value) {
return
}
if err, ok := value.(error); ok {
panic(fmt.Sprintf(`%+v`, err))
}
AssertNE(value, nil)
}