diff --git a/README.MD b/README.MD index 20a588755..97ccc9d92 100644 --- a/README.MD +++ b/README.MD @@ -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
- +
# 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 -JetBrains +JetBrains Atlassian diff --git a/README_ZH.MD b/README_ZH.MD index f53b254bb..9fdcdf486 100644 --- a/README_ZH.MD +++ b/README_ZH.MD @@ -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 # 架构
- +
# 模块 @@ -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`。 # 感谢 -JetBrains +JetBrains Atlassian diff --git a/database/gdb/gdb_type_result_scanlist.go b/database/gdb/gdb_type_result_scanlist.go index c74815389..b850ec9d1 100644 --- a/database/gdb/gdb_type_result_scanlist.go +++ b/database/gdb/gdb_type_result_scanlist.go @@ -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 { diff --git a/database/gdb/gdb_z_driver_test.go b/database/gdb/gdb_z_driver_test.go index e9f1c8883..3d2d10154 100644 --- a/database/gdb/gdb_z_driver_test.go +++ b/database/gdb/gdb_z_driver_test.go @@ -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) }) diff --git a/database/gdb/gdb_z_init_test.go b/database/gdb/gdb_z_init_test.go index 388c410e6..6208c9577 100644 --- a/database/gdb/gdb_z_init_test.go +++ b/database/gdb/gdb_z_init_test.go @@ -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) diff --git a/database/gdb/gdb_z_mysql_association_test.go b/database/gdb/gdb_z_mysql_association_test.go index 06b462017..4dbbf7db3 100644 --- a/database/gdb/gdb_z_mysql_association_test.go +++ b/database/gdb/gdb_z_mysql_association_test.go @@ -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) diff --git a/database/gdb/gdb_z_mysql_basic_test.go b/database/gdb/gdb_z_mysql_basic_test.go index 74a8ecd2c..b4a1a0083 100644 --- a/database/gdb/gdb_z_mysql_basic_test.go +++ b/database/gdb/gdb_z_mysql_basic_test.go @@ -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() diff --git a/database/gdb/gdb_z_mysql_ctx_test.go b/database/gdb/gdb_z_mysql_ctx_test.go index 6a6a811ab..33eb23125 100644 --- a/database/gdb/gdb_z_mysql_ctx_test.go +++ b/database/gdb/gdb_z_mysql_ctx_test.go @@ -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() diff --git a/database/gdb/gdb_z_mysql_internal_test.go b/database/gdb/gdb_z_mysql_internal_test.go index 45946fc31..a606b3215 100644 --- a/database/gdb/gdb_z_mysql_internal_test.go +++ b/database/gdb/gdb_z_mysql_internal_test.go @@ -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) diff --git a/database/gdb/gdb_z_mysql_method_test.go b/database/gdb/gdb_z_mysql_method_test.go index 295b0d111..56b928a6a 100644 --- a/database/gdb/gdb_z_mysql_method_test.go +++ b/database/gdb/gdb_z_mysql_method_test.go @@ -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) }) } diff --git a/database/gdb/gdb_z_mysql_model_test.go b/database/gdb/gdb_z_mysql_model_test.go index 297eb417c..df40dd8d3 100644 --- a/database/gdb/gdb_z_mysql_model_test.go +++ b/database/gdb/gdb_z_mysql_model_test.go @@ -38,7 +38,7 @@ func Test_Model_Insert(t *testing.T) { "nickname": "name_1", "create_time": gtime.Now().String(), }).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.LastInsertId() t.Assert(n, 1) @@ -50,7 +50,7 @@ func Test_Model_Insert(t *testing.T) { "nickname": "name_2", "create_time": gtime.Now().String(), }).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, _ = result.RowsAffected() t.Assert(n, 1) @@ -70,11 +70,11 @@ func Test_Model_Insert(t *testing.T) { Password: "25d55ad283aa400af464c76d713c07ad", Nickname: "name_3", }).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, _ = result.RowsAffected() t.Assert(n, 1) value, err := db.Table(table).Fields("passport").Where("id=3").Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.String(), "t3") result, err = db.Table(table).Filter().Data(&User{ @@ -85,15 +85,15 @@ func Test_Model_Insert(t *testing.T) { Nickname: "T4", CreateTime: gtime.Now(), }).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, _ = result.RowsAffected() t.Assert(n, 1) value, err = db.Table(table).Fields("passport").Where("id=4").Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.String(), "t4") result, err = db.Table(table).Where("id>?", 1).Delete() - t.Assert(err, nil) + t.AssertNil(err) n, _ = result.RowsAffected() t.Assert(n, 3) }) @@ -114,7 +114,7 @@ func Test_Model_Insert_Filter(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.LastInsertId() t.Assert(n, 1) @@ -144,7 +144,7 @@ func Test_Model_Insert_Filter(t *testing.T) { } result, err := db.Table(table).Filter().Data(data).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.LastInsertId() t.Assert(n, 2) @@ -170,10 +170,10 @@ func Test_Model_Insert_WithStructAndSliceAttribute(t *testing.T) { "create_time": gtime.Now().String(), } _, err := db.Table(table).Data(data).Insert() - t.Assert(err, nil) + t.AssertNil(err) one, err := db.Table(table).One("id", 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"])) @@ -200,10 +200,10 @@ func Test_Model_Insert_KeyFieldNameMapping(t *testing.T) { CreateTime: "2020-10-10 12:00:01", } _, err := db.Model(table).Data(data).Insert() - t.Assert(err, nil) + t.AssertNil(err) one, err := db.Model(table).FindOne(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) @@ -230,10 +230,10 @@ func Test_Model_Update_KeyFieldNameMapping(t *testing.T) { CreateTime: "2020-10-10 12:00:01", } _, err := db.Model(table).Data(data).WherePri(1).Update() - t.Assert(err, nil) + t.AssertNil(err) one, err := db.Model(table).FindOne(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) @@ -277,10 +277,10 @@ func Test_Model_Insert_Time(t *testing.T) { "create_time": "2020-10-10 20:09:18.334", } _, err := db.Table(table).Data(data).Insert() - t.Assert(err, nil) + t.AssertNil(err) one, err := db.Table(table).One("id", 1) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], data["passport"]) t.Assert(one["create_time"], "2020-10-10 20:09:18") t.Assert(one["nickname"], data["nickname"]) @@ -305,7 +305,7 @@ func Test_Model_BatchInsertWithArrayStruct(t *testing.T) { } result, err := user.Filter().Data(array).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.LastInsertId() t.Assert(n, SIZE) }) @@ -334,7 +334,7 @@ func Test_Model_InsertIgnore(t *testing.T) { "nickname": "name_1", "create_time": gtime.Now().String(), }).InsertIgnore() - t.Assert(err, nil) + t.AssertNil(err) }) } @@ -391,7 +391,7 @@ func Test_Model_Batch(t *testing.T) { table := createInitTable() defer dropTable(table) result, err := db.Table(table).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), SIZE) for _, v := range result { v["nickname"].Set(v["nickname"].String() + v["id"].String()) @@ -408,7 +408,7 @@ func Test_Model_Batch(t *testing.T) { table := createInitTable() defer dropTable(table) result, err := db.Table(table).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), SIZE) for _, v := range result { v["nickname"].Set(v["nickname"].String() + v["id"].String()) @@ -433,7 +433,7 @@ func Test_Model_Replace(t *testing.T) { "nickname": "T11", "create_time": "2018-10-24 10:00:00", }).Replace() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 1) }) @@ -450,7 +450,7 @@ func Test_Model_Save(t *testing.T) { "nickname": "T111", "create_time": "2018-10-24 10:00:00", }).Save() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 1) }) @@ -462,29 +462,29 @@ func Test_Model_Update(t *testing.T) { // UPDATE...LIMIT gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Data("nickname", "T100").Where(1).Order("id desc").Limit(2).Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 2) v1, err := db.Table(table).Fields("nickname").Where("id", 10).Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v1.String(), "T100") v2, err := db.Table(table).Fields("nickname").Where("id", 8).Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v2.String(), "name_8") }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Data("passport", "user_22").Where("passport=?", "user_2").Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 1) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Data("passport", "user_2").Where("passport='user_22'").Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 1) }) @@ -492,7 +492,7 @@ func Test_Model_Update(t *testing.T) { // Update + Data(string) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Data("passport='user_33'").Where("passport='user_3'").Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 1) }) @@ -502,7 +502,7 @@ func Test_Model_Update(t *testing.T) { "passport": "user_44", "none": "none", }).Where("passport='user_4'").Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 1) }) @@ -515,13 +515,13 @@ func Test_Model_Clone(t *testing.T) { gtest.C(t, func(t *gtest.T) { md := db.Table(table).Where("id IN(?)", g.Slice{1, 3}) count, err := md.Count() - t.Assert(err, nil) + t.AssertNil(err) record, err := md.Order("id DESC").One() - t.Assert(err, nil) + t.AssertNil(err) result, err := md.Order("id ASC").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 2) t.Assert(record["id"].Int(), 3) @@ -538,50 +538,50 @@ func Test_Model_Safe(t *testing.T) { gtest.C(t, func(t *gtest.T) { md := db.Table(table).Safe(false).Where("id IN(?)", g.Slice{1, 3}) count, err := md.Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 2) md.And("id = ?", 1) count, err = md.Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 1) }) gtest.C(t, func(t *gtest.T) { md := db.Table(table).Safe(true).Where("id IN(?)", g.Slice{1, 3}) count, err := md.Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 2) md.And("id = ?", 1) count, err = md.Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 2) }) gtest.C(t, func(t *gtest.T) { md := db.Table(table).Safe().Where("id IN(?)", g.Slice{1, 3}) count, err := md.Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 2) md.And("id = ?", 1) count, err = md.Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 2) }) gtest.C(t, func(t *gtest.T) { md1 := db.Table(table).Safe() md2 := md1.Where("id in (?)", g.Slice{1, 3}) count, err := md2.Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 2) all, err := md2.All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 2) all, err = md2.Page(1, 10).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 2) }) @@ -595,33 +595,33 @@ func Test_Model_Safe(t *testing.T) { // 1,3 count, err := md2.Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 2) all, err := md2.Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 2) t.Assert(all[0]["id"].Int(), 1) t.Assert(all[1]["id"].Int(), 3) all, err = md2.Page(1, 10).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 2) // 4,5,6 count, err = md3.Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 3) all, err = md3.Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 3) t.Assert(all[0]["id"].Int(), 4) t.Assert(all[1]["id"].Int(), 5) t.Assert(all[2]["id"].Int(), 6) all, err = md3.Page(1, 10).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 3) }) } @@ -632,13 +632,13 @@ func Test_Model_All(t *testing.T) { gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), SIZE) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id<0").All() t.Assert(result, nil) - t.Assert(err, nil) + t.AssertNil(err) }) } @@ -656,7 +656,7 @@ func Test_Model_Fields(t *testing.T) { ) ENGINE=InnoDB DEFAULT CHARSET=utf8; `, tableName2, )); err != nil { - gtest.Assert(err, nil) + gtest.AssertNil(err) } defer dropTable(tableName2) @@ -665,13 +665,13 @@ func Test_Model_Fields(t *testing.T) { "name": "table2_1", "age": 18, }) - gtest.Assert(err, nil) + gtest.AssertNil(err) n, _ := r.RowsAffected() gtest.Assert(n, 1) gtest.C(t, func(t *gtest.T) { all, err := db.Table(tableName1).As("u").Fields("u.passport,u.id").Where("u.id<2").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 1) t.Assert(len(all[0]), 2) }) @@ -681,7 +681,7 @@ func Test_Model_Fields(t *testing.T) { Fields("u1.passport,u1.id,u2.id AS u2id"). Where("u1.id<2"). All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 1) t.Assert(len(all[0]), 3) }) @@ -691,7 +691,7 @@ func Test_Model_Fields(t *testing.T) { Fields("u1.passport,u1.id,u2.name,u2.age"). Where("u1.id<2"). All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 1) t.Assert(len(all[0]), 4) t.Assert(all[0]["id"], 1) @@ -707,21 +707,21 @@ func Test_Model_FindAll(t *testing.T) { gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).FindAll(5) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 1) t.Assert(result[0]["id"].Int(), 5) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Order("id asc").FindAll("id", 8) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 1) t.Assert(result[0]["id"].Int(), 8) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Order("id asc").FindAll(g.Slice{3, 9}) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 2) t.Assert(result[0]["id"].Int(), 3) t.Assert(result[1]["id"].Int(), 9) @@ -729,13 +729,13 @@ func Test_Model_FindAll(t *testing.T) { gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).FindAll() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), SIZE) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id<0").FindAll() t.Assert(result, nil) - t.Assert(err, nil) + t.AssertNil(err) }) } @@ -745,26 +745,26 @@ func Test_Model_FindAll_GTime(t *testing.T) { gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).FindAll("create_time < ?", gtime.NewFromStr("2000-01-01 00:00:00")) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 0) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).FindAll("create_time > ?", gtime.NewFromStr("2000-01-01 00:00:00")) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), SIZE) }) gtest.C(t, func(t *gtest.T) { v := g.NewVar("2000-01-01 00:00:00") result, err := db.Table(table).FindAll("create_time < ?", v) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 0) }) gtest.C(t, func(t *gtest.T) { v := g.NewVar("2000-01-01 00:00:00") result, err := db.Table(table).FindAll("create_time > ?", v) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), SIZE) }) } @@ -774,13 +774,13 @@ func Test_Model_One(t *testing.T) { defer dropTable(table) gtest.C(t, func(t *gtest.T) { record, err := db.Table(table).Where("id", 1).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(record["nickname"].String(), "name_1") }) gtest.C(t, func(t *gtest.T) { record, err := db.Table(table).Where("id", 0).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(record, nil) }) } @@ -791,31 +791,31 @@ func Test_Model_FindOne(t *testing.T) { gtest.C(t, func(t *gtest.T) { record, err := db.Table(table).FindOne(1) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(record["nickname"].String(), "name_1") }) gtest.C(t, func(t *gtest.T) { record, err := db.Table(table).FindOne(3) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(record["nickname"].String(), "name_3") }) gtest.C(t, func(t *gtest.T) { record, err := db.Table(table).Where("id", 1).FindOne() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(record["nickname"].String(), "name_1") }) gtest.C(t, func(t *gtest.T) { record, err := db.Table(table).FindOne("id", 9) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(record["nickname"].String(), "name_9") }) gtest.C(t, func(t *gtest.T) { record, err := db.Table(table).Where("id", 0).FindOne() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(record, nil) }) } @@ -826,13 +826,13 @@ func Test_Model_Value(t *testing.T) { gtest.C(t, func(t *gtest.T) { value, err := db.Table(table).Fields("nickname").Where("id", 1).Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.String(), "name_1") }) gtest.C(t, func(t *gtest.T) { value, err := db.Table(table).Fields("nickname").Where("id", 0).Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value, nil) }) } @@ -843,28 +843,28 @@ func Test_Model_Array(t *testing.T) { gtest.C(t, func(t *gtest.T) { all, err := db.Table(table).Where("id", g.Slice{1, 2, 3}).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(all.Array("id"), g.Slice{1, 2, 3}) t.Assert(all.Array("nickname"), g.Slice{"name_1", "name_2", "name_3"}) }) gtest.C(t, func(t *gtest.T) { array, err := db.Table(table).Fields("nickname").Where("id", g.Slice{1, 2, 3}).Array() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(array, g.Slice{"name_1", "name_2", "name_3"}) }) gtest.C(t, func(t *gtest.T) { array, err := db.Table(table).Array("nickname", "id", g.Slice{1, 2, 3}) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(array, g.Slice{"name_1", "name_2", "name_3"}) }) gtest.C(t, func(t *gtest.T) { array, err := db.Table(table).FindArray("nickname", "id", g.Slice{1, 2, 3}) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(array, g.Slice{"name_1", "name_2", "name_3"}) }) gtest.C(t, func(t *gtest.T) { array, err := db.Table(table).FindArray("nickname", g.Slice{1, 2, 3}) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(array, g.Slice{"name_1", "name_2", "name_3"}) }) } @@ -875,25 +875,25 @@ func Test_Model_FindValue(t *testing.T) { gtest.C(t, func(t *gtest.T) { value, err := db.Table(table).FindValue("nickname", 1) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.String(), "name_1") }) gtest.C(t, func(t *gtest.T) { value, err := db.Table(table).Order("id desc").FindValue("nickname") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.String(), "name_10") }) gtest.C(t, func(t *gtest.T) { value, err := db.Table(table).Fields("nickname").Where("id", 1).FindValue() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.String(), "name_1") }) gtest.C(t, func(t *gtest.T) { value, err := db.Table(table).Fields("nickname").Where("id", 0).FindValue() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value, nil) }) } @@ -903,33 +903,33 @@ func Test_Model_Count(t *testing.T) { defer dropTable(table) gtest.C(t, func(t *gtest.T) { count, err := db.Table(table).Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, SIZE) }) gtest.C(t, func(t *gtest.T) { count, err := db.Table(table).FieldsEx("id").Where("id>8").Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 2) }) gtest.C(t, func(t *gtest.T) { count, err := db.Table(table).Fields("distinct id,nickname").Where("id>8").Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 2) }) // COUNT...LIMIT... gtest.C(t, func(t *gtest.T) { count, err := db.Table(table).Page(1, 2).Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, SIZE) }) //gtest.C(t, func(t *gtest.T) { // count, err := db.Table(table).Fields("id myid").Where("id>8").Count() - // t.Assert(err, nil) + // t.AssertNil(err) // t.Assert(count, 2) //}) //gtest.C(t, func(t *gtest.T) { // count, err := db.Table(table).As("u1").LeftJoin(table, "u2", "u2.id=u1.id").Fields("u2.id u2id").Where("u1.id>8").Count() - // t.Assert(err, nil) + // t.AssertNil(err) // t.Assert(count, 2) //}) } @@ -939,17 +939,17 @@ func Test_Model_FindCount(t *testing.T) { defer dropTable(table) gtest.C(t, func(t *gtest.T) { count, err := db.Table(table).FindCount(g.Slice{1, 3}) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 2) }) gtest.C(t, func(t *gtest.T) { count, err := db.Table(table).FindCount(g.Slice{1, 300000}) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, 1) }) gtest.C(t, func(t *gtest.T) { count, err := db.Table(table).FindCount() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(count, SIZE) }) } @@ -959,7 +959,7 @@ func Test_Model_Select(t *testing.T) { defer dropTable(table) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Select() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), SIZE) }) } @@ -977,7 +977,7 @@ func Test_Model_Struct(t *testing.T) { } user := new(User) err := db.Table(table).Where("id=1").Struct(user) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(user.NickName, "name_1") t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") }) @@ -991,7 +991,7 @@ func Test_Model_Struct(t *testing.T) { } user := new(User) err := db.Table(table).Where("id=1").Struct(user) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(user.NickName, "name_1") t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") }) @@ -1006,7 +1006,7 @@ func Test_Model_Struct(t *testing.T) { } user := (*User)(nil) err := db.Table(table).Where("id=1").Struct(&user) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(user.NickName, "name_1") t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") }) @@ -1050,7 +1050,7 @@ func Test_Model_Struct(t *testing.T) { } var user *User err := db.Table(table).Where("id=-1").Struct(&user) - t.Assert(err, nil) + t.AssertNil(err) }) } @@ -1070,7 +1070,7 @@ func Test_Model_Struct_CustomType(t *testing.T) { } user := new(User) err := db.Table(table).Where("id=1").Struct(user) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(user.NickName, "name_1") t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") }) @@ -1159,7 +1159,7 @@ func Test_Model_Structs(t *testing.T) { } var users []*User err := db.Table(table).Where("id<0").Structs(&users) - t.Assert(err, nil) + t.AssertNil(err) }) } @@ -1205,7 +1205,7 @@ func Test_Model_Scan(t *testing.T) { } user := new(User) err := db.Table(table).Where("id=1").Scan(user) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(user.NickName, "name_1") t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") }) @@ -1219,7 +1219,7 @@ func Test_Model_Scan(t *testing.T) { } user := new(User) err := db.Table(table).Where("id=1").Scan(user) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(user.NickName, "name_1") t.Assert(user.CreateTime.String(), "2018-10-24 10:00:00") }) @@ -1233,7 +1233,7 @@ func Test_Model_Scan(t *testing.T) { } var users []User err := db.Table(table).Order("id asc").Scan(&users) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(users), SIZE) t.Assert(users[0].Id, 1) t.Assert(users[1].Id, 2) @@ -1253,7 +1253,7 @@ func Test_Model_Scan(t *testing.T) { } var users []*User err := db.Table(table).Order("id asc").Scan(&users) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(users), SIZE) t.Assert(users[0].Id, 1) t.Assert(users[1].Id, 2) @@ -1289,7 +1289,7 @@ func Test_Model_OrderBy(t *testing.T) { gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Order("id DESC").Select() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), SIZE) t.Assert(result[0]["nickname"].String(), fmt.Sprintf("name_%d", SIZE)) }) @@ -1301,7 +1301,7 @@ func Test_Model_GroupBy(t *testing.T) { gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).GroupBy("id").Select() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), SIZE) t.Assert(result[0]["nickname"].String(), "name_1") }) @@ -1312,7 +1312,7 @@ func Test_Model_Data(t *testing.T) { table := createInitTable() defer dropTable(table) result, err := db.Table(table).Data("nickname=?", "test").Where("id=?", 3).Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 1) }) @@ -1329,7 +1329,7 @@ func Test_Model_Data(t *testing.T) { }) } result, err := db.Table(table).Data(users).Batch(2).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 10) }) @@ -1346,7 +1346,7 @@ func Test_Model_Data(t *testing.T) { }) } result, err := db.Table(table).Data(users).Batch(2).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 10) }) @@ -1359,7 +1359,7 @@ func Test_Model_Where(t *testing.T) { // string gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id=? and nickname=?", 3, "name_3").One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) @@ -1367,13 +1367,13 @@ func Test_Model_Where(t *testing.T) { // slice gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(g.Slice{"id", 3}).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(g.Slice{"id", 3, "nickname", "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) @@ -1381,7 +1381,7 @@ func Test_Model_Where(t *testing.T) { // slice parameter gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id=? and nickname=?", g.Slice{3, "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) @@ -1390,7 +1390,7 @@ func Test_Model_Where(t *testing.T) { result, err := db.Table(table).Where(g.Map{ "passport like": "user_1%", }).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 2) t.Assert(result[0].GMap().Get("id"), 1) t.Assert(result[1].GMap().Get("id"), 10) @@ -1401,118 +1401,118 @@ func Test_Model_Where(t *testing.T) { "id": g.Slice{1, 2, 3}, "passport": g.Slice{"user_2", "user_3"}, }).And("id=? and nickname=?", g.Slice{3, "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id=3", g.Slice{}).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id=?", g.Slice{3}).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id", 3).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id", 3).Where("nickname", "name_3").One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id", 3).And("nickname", "name_3").One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id", 30).Or("nickname", "name_3").One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id", 30).Or("nickname", "name_3").And("id>?", 1).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id", 30).Or("nickname", "name_3").And("id>", 1).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // slice gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id=? AND nickname=?", g.Slice{3, "name_3"}...).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id=? AND nickname=?", g.Slice{3, "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("passport like ? and nickname like ?", g.Slice{"user_3", "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // map gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(g.Map{"id": 3, "nickname": "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // map key operator gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(g.Map{"id>": 1, "id<": 3}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 2) }) // gmap.Map gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(gmap.NewFrom(g.MapAnyAny{"id": 3, "nickname": "name_3"})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // gmap.Map key operator gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(gmap.NewFrom(g.MapAnyAny{"id>": 1, "id<": 3})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 2) }) // list map gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(gmap.NewListMapFrom(g.MapAnyAny{"id": 3, "nickname": "name_3"})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // list map key operator gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(gmap.NewListMapFrom(g.MapAnyAny{"id>": 1, "id<": 3})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 2) }) // tree map gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(gmap.NewTreeMapFrom(gutil.ComparatorString, g.MapAnyAny{"id": 3, "nickname": "name_3"})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // tree map key operator gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(gmap.NewTreeMapFrom(gutil.ComparatorString, g.MapAnyAny{"id>": 1, "id<": 3})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 2) }) @@ -1527,7 +1527,7 @@ func Test_Model_Where(t *testing.T) { "id": g.Slice{1, 2, 3}, } result, err := db.Table(table).Where(conditions).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 3) t.Assert(result[0]["id"].Int(), 1) }) @@ -1542,7 +1542,7 @@ func Test_Model_Where(t *testing.T) { "id in(?)": g.Slice{1, 2, 3}, } result, err := db.Table(table).Where(conditions).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 3) t.Assert(result[0]["id"].Int(), 1) }) @@ -1553,17 +1553,17 @@ func Test_Model_Where(t *testing.T) { Nickname string `gconv:"nickname"` } result, err := db.Table(table).Where(User{3, "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) result, err = db.Table(table).Where(&User{3, "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // slice single gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("id IN(?)", g.Slice{1, 3}).Order("id ASC").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 2) t.Assert(result[0]["id"].Int(), 1) t.Assert(result[1]["id"].Int(), 3) @@ -1571,7 +1571,7 @@ func Test_Model_Where(t *testing.T) { // slice + string gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("nickname=? AND id IN(?)", "name_3", g.Slice{1, 3}).Order("id ASC").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 1) t.Assert(result[0]["id"].Int(), 3) }) @@ -1581,7 +1581,7 @@ func Test_Model_Where(t *testing.T) { "id": g.Slice{1, 3}, "nickname": "name_3", }).Order("id ASC").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 1) t.Assert(result[0]["id"].Int(), 3) }) @@ -1595,7 +1595,7 @@ func Test_Model_Where(t *testing.T) { Ids: []int{1, 3}, Nickname: "name_3", }).Order("id ASC").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 1) t.Assert(result[0]["id"].Int(), 3) }) @@ -1608,12 +1608,12 @@ func Test_Model_Where_ISNULL_1(t *testing.T) { gtest.C(t, func(t *gtest.T) { //db.SetDebug(true) result, err := db.Table(table).Data("nickname", nil).Where("id", 2).Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 1) one, err := db.Table(table).Where("nickname", nil).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one.IsEmpty(), false) t.Assert(one["id"], 2) }) @@ -1634,7 +1634,7 @@ func Test_Model_Where_ISNULL_2(t *testing.T) { "id": g.Slice{1, 2, 3}, } result, err := db.Table(table).WherePri(conditions).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 3) t.Assert(result[0]["id"].Int(), 1) }) @@ -1648,7 +1648,7 @@ func Test_Model_Where_OmitEmpty(t *testing.T) { "id < 4": "", } result, err := db.Table(table).WherePri(conditions).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 3) t.Assert(result[0]["id"].Int(), 1) }) @@ -1657,7 +1657,7 @@ func Test_Model_Where_OmitEmpty(t *testing.T) { "id < 4": "", } result, err := db.Table(table).WherePri(conditions).OmitEmpty().Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 3) t.Assert(result[0]["id"].Int(), 1) }) @@ -1669,12 +1669,12 @@ func Test_Model_Where_GTime(t *testing.T) { gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("create_time>?", gtime.NewFromStr("2010-09-01")).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 10) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where("create_time>?", *gtime.NewFromStr("2010-09-01")).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 10) }) } @@ -1686,13 +1686,13 @@ func Test_Model_WherePri(t *testing.T) { // primary key gtest.C(t, func(t *gtest.T) { one, err := db.Table(table).WherePri(3).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertNE(one, nil) t.Assert(one["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { all, err := db.Table(table).WherePri(g.Slice{3, 9}).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 2) t.Assert(all[0]["id"].Int(), 3) t.Assert(all[1]["id"].Int(), 9) @@ -1701,14 +1701,14 @@ func Test_Model_WherePri(t *testing.T) { // string gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id=? and nickname=?", 3, "name_3").One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) // slice parameter gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id=? and nickname=?", g.Slice{3, "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) @@ -1717,7 +1717,7 @@ func Test_Model_WherePri(t *testing.T) { result, err := db.Table(table).WherePri(g.Map{ "passport like": "user_1%", }).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 2) t.Assert(result[0].GMap().Get("id"), 1) t.Assert(result[1].GMap().Get("id"), 10) @@ -1728,7 +1728,7 @@ func Test_Model_WherePri(t *testing.T) { "id": g.Slice{1, 2, 3}, "passport": g.Slice{"user_2", "user_3"}, }).And("id=? and nickname=?", g.Slice{3, "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) @@ -1737,118 +1737,118 @@ func Test_Model_WherePri(t *testing.T) { "id": g.Slice{1, 2, 3}, "passport": g.Slice{"user_2", "user_3"}, }).Or("nickname=?", g.Slice{"name_4"}).And("id", 3).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 2) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id=3", g.Slice{}).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id=?", g.Slice{3}).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id", 3).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id", 3).WherePri("nickname", "name_3").One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id", 3).And("nickname", "name_3").One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id", 30).Or("nickname", "name_3").One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id", 30).Or("nickname", "name_3").And("id>?", 1).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id", 30).Or("nickname", "name_3").And("id>", 1).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // slice gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id=? AND nickname=?", g.Slice{3, "name_3"}...).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id=? AND nickname=?", g.Slice{3, "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("passport like ? and nickname like ?", g.Slice{"user_3", "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // map gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri(g.Map{"id": 3, "nickname": "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // map key operator gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri(g.Map{"id>": 1, "id<": 3}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 2) }) // gmap.Map gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri(gmap.NewFrom(g.MapAnyAny{"id": 3, "nickname": "name_3"})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // gmap.Map key operator gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri(gmap.NewFrom(g.MapAnyAny{"id>": 1, "id<": 3})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 2) }) // list map gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri(gmap.NewListMapFrom(g.MapAnyAny{"id": 3, "nickname": "name_3"})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // list map key operator gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri(gmap.NewListMapFrom(g.MapAnyAny{"id>": 1, "id<": 3})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 2) }) // tree map gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri(gmap.NewTreeMapFrom(gutil.ComparatorString, g.MapAnyAny{"id": 3, "nickname": "name_3"})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // tree map key operator gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri(gmap.NewTreeMapFrom(gutil.ComparatorString, g.MapAnyAny{"id>": 1, "id<": 3})).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 2) }) @@ -1863,7 +1863,7 @@ func Test_Model_WherePri(t *testing.T) { "id": g.Slice{1, 2, 3}, } result, err := db.Table(table).WherePri(conditions).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 3) t.Assert(result[0]["id"].Int(), 1) }) @@ -1878,7 +1878,7 @@ func Test_Model_WherePri(t *testing.T) { "id in(?)": g.Slice{1, 2, 3}, } result, err := db.Table(table).WherePri(conditions).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 3) t.Assert(result[0]["id"].Int(), 1) }) @@ -1889,17 +1889,17 @@ func Test_Model_WherePri(t *testing.T) { Nickname string `gconv:"nickname"` } result, err := db.Table(table).WherePri(User{3, "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) result, err = db.Table(table).WherePri(&User{3, "name_3"}).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(result["id"].Int(), 3) }) // slice single gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("id IN(?)", g.Slice{1, 3}).Order("id ASC").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 2) t.Assert(result[0]["id"].Int(), 1) t.Assert(result[1]["id"].Int(), 3) @@ -1907,7 +1907,7 @@ func Test_Model_WherePri(t *testing.T) { // slice + string gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).WherePri("nickname=? AND id IN(?)", "name_3", g.Slice{1, 3}).Order("id ASC").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 1) t.Assert(result[0]["id"].Int(), 3) }) @@ -1917,7 +1917,7 @@ func Test_Model_WherePri(t *testing.T) { "id": g.Slice{1, 3}, "nickname": "name_3", }).Order("id ASC").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 1) t.Assert(result[0]["id"].Int(), 3) }) @@ -1931,7 +1931,7 @@ func Test_Model_WherePri(t *testing.T) { Ids: []int{1, 3}, Nickname: "name_3", }).Order("id ASC").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 1) t.Assert(result[0]["id"].Int(), 3) }) @@ -1944,14 +1944,14 @@ func Test_Model_Delete(t *testing.T) { // DELETE...LIMIT gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(1).Limit(2).Delete() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 2) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Where(1).Delete() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, SIZE-2) }) @@ -1962,7 +1962,7 @@ func Test_Model_Offset(t *testing.T) { defer dropTable(table) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Limit(2).Offset(5).Order("id").Select() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 2) t.Assert(result[0]["id"], 6) t.Assert(result[1]["id"], 7) @@ -1974,7 +1974,7 @@ func Test_Model_Page(t *testing.T) { defer dropTable(table) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).Page(3, 3).Order("id").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 3) t.Assert(result[0]["id"], 7) t.Assert(result[1]["id"], 8) @@ -1983,7 +1983,7 @@ func Test_Model_Page(t *testing.T) { model := db.Table(table).Safe().Order("id") all, err := model.Page(3, 3).All() count, err := model.Count() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 3) t.Assert(all[0]["id"], "7") t.Assert(count, SIZE) @@ -2001,11 +2001,11 @@ func Test_Model_Option_Map(t *testing.T) { "password": "1", "nickname": "1", }).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, 1) one, err := db.Table(table).Where("id", 1).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertNE(one["password"].String(), "1") t.AssertNE(one["nickname"].String(), "1") t.Assert(one["passport"].String(), "1") @@ -2019,11 +2019,11 @@ func Test_Model_Option_Map(t *testing.T) { "password": 0, "nickname": "1", }).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, 1) one, err := db.Table(table).Where("id", 1).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertNE(one["passport"].String(), "0") t.AssertNE(one["password"].String(), "0") t.Assert(one["nickname"].String(), "1") @@ -2039,9 +2039,9 @@ func Test_Model_Option_Map(t *testing.T) { "password": 0, "nickname": "1", }).Replace() - t.Assert(err, nil) + t.AssertNil(err) one, err := db.Table(table).Where("id", 1).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertNE(one["passport"].String(), "0") t.AssertNE(one["password"].String(), "0") t.Assert(one["nickname"].String(), "1") @@ -2057,11 +2057,11 @@ func Test_Model_Option_Map(t *testing.T) { "password": "1", "nickname": "1", }).Save() - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, 1) one, err := db.Table(table).Where("id", 1).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertNE(one["password"].String(), "1") t.AssertNE(one["nickname"].String(), "1") t.Assert(one["passport"].String(), "1") @@ -2075,9 +2075,9 @@ func Test_Model_Option_Map(t *testing.T) { "password": 0, "nickname": "1", }).Save() - t.Assert(err, nil) + t.AssertNil(err) one, err := db.Table(table).Where("id", 1).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertNE(one["passport"].String(), "0") t.AssertNE(one["password"].String(), "0") t.Assert(one["nickname"].String(), "1") @@ -2088,9 +2088,9 @@ func Test_Model_Option_Map(t *testing.T) { "password": 0, "nickname": "1", }).Save() - 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["passport"].String(), "0") t.Assert(one["password"].String(), "0") t.Assert(one["nickname"].String(), "1") @@ -2102,7 +2102,7 @@ func Test_Model_Option_Map(t *testing.T) { defer dropTable(table) r, err := db.Table(table).Data(g.Map{"nickname": ""}).Where("id", 1).Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, 1) @@ -2110,7 +2110,7 @@ func Test_Model_Option_Map(t *testing.T) { t.AssertNE(err, nil) r, err = db.Table(table).OmitEmpty().Data(g.Map{"nickname": "", "password": "123"}).Where("id", 3).Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ = r.RowsAffected() t.Assert(n, 1) @@ -2123,12 +2123,12 @@ func Test_Model_Option_Map(t *testing.T) { "passport": "123", "password": "456", }).Where("id", 5).Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ = r.RowsAffected() t.Assert(n, 1) one, err := db.Table(table).Where("id", 5).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["password"], "456") t.AssertNE(one["passport"].String(), "") t.AssertNE(one["passport"].String(), "123") @@ -2153,11 +2153,11 @@ func Test_Model_Option_List(t *testing.T) { "nickname": "2", }, }).Save() - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, 2) list, err := db.Table(table).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(list), 2) t.Assert(list[0]["id"].String(), "1") t.Assert(list[0]["nickname"].String(), "") @@ -2187,11 +2187,11 @@ func Test_Model_Option_List(t *testing.T) { "nickname": "2", }, }).Save() - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, 2) list, err := db.Table(table).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(list), 2) t.Assert(list[0]["id"].String(), "1") t.Assert(list[0]["nickname"].String(), "") @@ -2232,7 +2232,7 @@ func Test_Model_Option_Where(t *testing.T) { table := createInitTable() defer dropTable(table) r, err := db.Table(table).OmitEmpty().Data("nickname", 1).Where(g.Map{"id": 0, "passport": ""}).And(1).Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, SIZE) }) @@ -2240,12 +2240,12 @@ func Test_Model_Option_Where(t *testing.T) { table := createInitTable() defer dropTable(table) r, err := db.Table(table).OmitEmpty().Data("nickname", 1).Where(g.Map{"id": 1, "passport": ""}).Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, 1) v, err := db.Table(table).Where("id", 1).Fields("nickname").Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "1") }) } @@ -2260,7 +2260,7 @@ func Test_Model_Where_MultiSliceArguments(t *testing.T) { "nickname": g.Slice{"name_2", "name_4"}, "id >= 4": nil, }).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(r), 1) t.Assert(r[0]["id"], 4) }) @@ -2270,7 +2270,7 @@ func Test_Model_Where_MultiSliceArguments(t *testing.T) { "id": g.Slice{1, 2, 3}, "passport": g.Slice{"user_2", "user_3"}, }).Or("nickname=?", g.Slice{"name_4"}).And("id", 3).One() - t.Assert(err, nil) + t.AssertNil(err) t.AssertGT(len(result), 0) t.Assert(result["id"].Int(), 2) }) @@ -2282,7 +2282,7 @@ func Test_Model_FieldsEx(t *testing.T) { // Select. gtest.C(t, func(t *gtest.T) { r, err := db.Table(table).FieldsEx("create_time, id").Where("id in (?)", g.Slice{1, 2}).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(r), 2) t.Assert(len(r[0]), 3) t.Assert(r[0]["id"], "") @@ -2299,12 +2299,12 @@ func Test_Model_FieldsEx(t *testing.T) { // Update. gtest.C(t, func(t *gtest.T) { r, err := db.Table(table).FieldsEx("password").Data(g.Map{"nickname": "123", "password": "456"}).Where("id", 3).Update() - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, 1) one, err := db.Table(table).Where("id", 3).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["nickname"], "123") t.AssertNE(one["password"], "456") }) @@ -2319,7 +2319,7 @@ func Test_Model_FieldsEx_WithReservedWords(t *testing.T) { defer dropTable(table) gtest.C(t, func(t *gtest.T) { _, err := db.Table(table).FieldsEx("content").One() - t.Assert(err, nil) + t.AssertNil(err) }) } @@ -2351,7 +2351,7 @@ func Test_Model_Prefix(t *testing.T) { // Select. gtest.C(t, func(t *gtest.T) { r, err := db.Table(table).Where("id in (?)", g.Slice{1, 2}).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(r), 2) t.Assert(r[0]["id"], "1") t.Assert(r[1]["id"], "2") @@ -2359,7 +2359,7 @@ func Test_Model_Prefix(t *testing.T) { // Select with alias. gtest.C(t, func(t *gtest.T) { r, err := db.Table(table+" as u").Where("u.id in (?)", g.Slice{1, 2}).Order("u.id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(r), 2) t.Assert(r[0]["id"], "1") t.Assert(r[1]["id"], "2") @@ -2367,14 +2367,14 @@ func Test_Model_Prefix(t *testing.T) { // Select with alias and join statement. gtest.C(t, func(t *gtest.T) { r, err := db.Table(table+" as u1").LeftJoin(table+" as u2", "u2.id=u1.id").Where("u1.id in (?)", g.Slice{1, 2}).Order("u1.id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(r), 2) t.Assert(r[0]["id"], "1") t.Assert(r[1]["id"], "2") }) gtest.C(t, func(t *gtest.T) { r, err := db.Table(table).As("u1").LeftJoin(table+" as u2", "u2.id=u1.id").Where("u1.id in (?)", g.Slice{1, 2}).Order("u1.id asc").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(r), 2) t.Assert(r[0]["id"], "1") t.Assert(r[1]["id"], "2") @@ -2401,40 +2401,40 @@ func Test_Model_Schema1(t *testing.T) { gtest.C(t, func(t *gtest.T) { db.SetSchema(SCHEMA1) r, err := db.Table(table).Update(g.Map{"nickname": "name_100"}, "id=1") - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, 1) v, err := db.Table(table).Value("nickname", "id=1") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_100") db.SetSchema(SCHEMA2) v, err = db.Table(table).Value("nickname", "id=1") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_1") }) // Model. gtest.C(t, func(t *gtest.T) { v, err := db.Table(table).Schema(SCHEMA1).Value("nickname", "id=2") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_2") r, err := db.Table(table).Schema(SCHEMA1).Update(g.Map{"nickname": "name_200"}, "id=2") - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, 1) v, err = db.Table(table).Schema(SCHEMA1).Value("nickname", "id=2") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_200") v, err = db.Table(table).Schema(SCHEMA2).Value("nickname", "id=2") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_2") v, err = db.Table(table).Schema(SCHEMA1).Value("nickname", "id=2") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_200") }) // Model. @@ -2448,14 +2448,14 @@ func Test_Model_Schema1(t *testing.T) { "create_time": gtime.NewFromStr("2018-10-24 10:00:00").String(), "none-exist-field": 1, }) - t.Assert(err, nil) + t.AssertNil(err) v, err := db.Table(table).Schema(SCHEMA1).Value("nickname", "id=?", i) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_1000") v, err = db.Table(table).Schema(SCHEMA2).Value("nickname", "id=?", i) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "") }) } @@ -2479,24 +2479,24 @@ func Test_Model_Schema2(t *testing.T) { // Schema. gtest.C(t, func(t *gtest.T) { v, err := db.Schema(SCHEMA1).Table(table).Value("nickname", "id=2") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_2") r, err := db.Schema(SCHEMA1).Table(table).Update(g.Map{"nickname": "name_200"}, "id=2") - t.Assert(err, nil) + t.AssertNil(err) n, _ := r.RowsAffected() t.Assert(n, 1) v, err = db.Schema(SCHEMA1).Table(table).Value("nickname", "id=2") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_200") v, err = db.Schema(SCHEMA2).Table(table).Value("nickname", "id=2") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_2") v, err = db.Schema(SCHEMA1).Table(table).Value("nickname", "id=2") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_200") }) // Schema. @@ -2510,14 +2510,14 @@ func Test_Model_Schema2(t *testing.T) { "create_time": gtime.NewFromStr("2018-10-24 10:00:00").String(), "none-exist-field": 1, }) - t.Assert(err, nil) + t.AssertNil(err) v, err := db.Schema(SCHEMA1).Table(table).Value("nickname", "id=?", i) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "name_1000") v, err = db.Schema(SCHEMA2).Table(table).Value("nickname", "id=?", i) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.String(), "") }) } @@ -2539,9 +2539,9 @@ func Test_Model_FieldsExStruct(t *testing.T) { NickName: "333", } r, err := db.Table(table).FieldsEx("create_time, password").OmitEmpty().Data(user).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, err := r.RowsAffected() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(n, 1) }) gtest.C(t, func(t *gtest.T) { @@ -2565,9 +2565,9 @@ func Test_Model_FieldsExStruct(t *testing.T) { Batch(2). Data(users). Insert() - t.Assert(err, nil) + t.AssertNil(err) n, err := r.RowsAffected() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(n, 10) }) } @@ -2589,9 +2589,9 @@ func Test_Model_OmitEmpty_Time(t *testing.T) { Time: time.Time{}, } r, err := db.Table(table).OmitEmpty().Data(user).WherePri(1).Update() - t.Assert(err, nil) + t.AssertNil(err) n, err := r.RowsAffected() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(n, 1) }) } @@ -2601,7 +2601,7 @@ func Test_Result_Chunk(t *testing.T) { defer dropTable(table) gtest.C(t, func(t *gtest.T) { r, err := db.Table(table).Order("id asc").All() - t.Assert(err, nil) + t.AssertNil(err) chunks := r.Chunk(3) t.Assert(len(chunks), 4) t.Assert(chunks[0][0]["id"].Int(), 1) @@ -2619,14 +2619,14 @@ func Test_Model_DryRun(t *testing.T) { gtest.C(t, func(t *gtest.T) { one, err := db.Table(table).FindOne(1) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["id"], 1) }) gtest.C(t, func(t *gtest.T) { r, err := db.Table(table).Data("passport", "port_1").WherePri(1).Update() - t.Assert(err, nil) + t.AssertNil(err) n, err := r.RowsAffected() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(n, 0) }) } @@ -2637,7 +2637,7 @@ func Test_Model_Join_SubQuery(t *testing.T) { gtest.C(t, func(t *gtest.T) { subQuery := fmt.Sprintf("select * from `%s`", table) r, err := db.Table(table, "t1").Fields("t2.id").LeftJoin(subQuery, "t2", "t2.id=t1.id").Array() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(r), SIZE) t.Assert(r[0], "1") t.Assert(r[SIZE-1], SIZE) @@ -2650,95 +2650,95 @@ func Test_Model_Cache(t *testing.T) { gtest.C(t, func(t *gtest.T) { one, err := db.Table(table).Cache(time.Second, "test1").FindOne(1) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], "user_1") r, err := db.Table(table).Data("passport", "user_100").WherePri(1).Update() - t.Assert(err, nil) + t.AssertNil(err) n, err := r.RowsAffected() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(n, 1) one, err = db.Table(table).Cache(time.Second, "test1").FindOne(1) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], "user_1") time.Sleep(time.Second * 2) one, err = db.Table(table).Cache(time.Second, "test1").FindOne(1) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], "user_100") }) gtest.C(t, func(t *gtest.T) { one, err := db.Table(table).Cache(time.Second, "test2").FindOne(2) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], "user_2") r, err := db.Table(table).Data("passport", "user_200").Cache(-1, "test2").WherePri(2).Update() - t.Assert(err, nil) + t.AssertNil(err) n, err := r.RowsAffected() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(n, 1) one, err = db.Table(table).Cache(time.Second, "test2").FindOne(2) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], "user_200") }) // transaction. gtest.C(t, func(t *gtest.T) { // make cache for id 3 one, err := db.Table(table).Cache(time.Second, "test3").FindOne(3) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], "user_3") r, err := db.Table(table).Data("passport", "user_300").Cache(time.Second, "test3").WherePri(3).Update() - t.Assert(err, nil) + t.AssertNil(err) n, err := r.RowsAffected() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(n, 1) err = db.Transaction(func(tx *gdb.TX) error { one, err := tx.Table(table).Cache(time.Second, "test3").FindOne(3) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], "user_300") return nil }) - t.Assert(err, nil) + t.AssertNil(err) one, err = db.Table(table).Cache(time.Second, "test3").FindOne(3) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], "user_3") }) gtest.C(t, func(t *gtest.T) { // make cache for id 4 one, err := db.Table(table).Cache(time.Second, "test4").FindOne(4) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], "user_4") r, err := db.Table(table).Data("passport", "user_400").Cache(time.Second, "test3").WherePri(4).Update() - t.Assert(err, nil) + t.AssertNil(err) n, err := r.RowsAffected() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(n, 1) err = db.Transaction(func(tx *gdb.TX) error { // Cache feature disabled. one, err := tx.Table(table).Cache(time.Second, "test4").FindOne(4) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], "user_400") // Update the cache. r, err := tx.Table(table).Data("passport", "user_4000"). Cache(-1, "test4").WherePri(4).Update() - t.Assert(err, nil) + t.AssertNil(err) n, err := r.RowsAffected() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(n, 1) return nil }) - t.Assert(err, nil) + t.AssertNil(err) // Read from db. one, err = db.Table(table).Cache(time.Second, "test4").FindOne(4) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(one["passport"], "user_4000") }) } @@ -2749,22 +2749,22 @@ func Test_Model_Having(t *testing.T) { gtest.C(t, func(t *gtest.T) { all, err := db.Table(table).Where("id > 1").Having("id > 8").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 2) }) gtest.C(t, func(t *gtest.T) { all, err := db.Table(table).Where("id > 1").Having("id > ?", 8).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 2) }) gtest.C(t, func(t *gtest.T) { all, err := db.Table(table).Where("id > ?", 1).Having("id > ?", 8).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 2) }) gtest.C(t, func(t *gtest.T) { all, err := db.Table(table).Where("id > ?", 1).Having("id", 8).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 1) }) } @@ -2775,7 +2775,7 @@ func Test_Model_Distinct(t *testing.T) { gtest.C(t, func(t *gtest.T) { all, err := db.Table(table, "t").Fields("distinct t.id").Where("id > 1").Having("id > 8").All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(all), 2) }) } @@ -2786,12 +2786,12 @@ func Test_Model_Min_Max(t *testing.T) { gtest.C(t, func(t *gtest.T) { value, err := db.Table(table, "t").Fields("min(t.id)").Where("id > 1").Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.Int(), 2) }) gtest.C(t, func(t *gtest.T) { value, err := db.Table(table, "t").Fields("max(t.id)").Where("id > 1").Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.Int(), 10) }) } @@ -2802,13 +2802,13 @@ func Test_Model_Fields_AutoMapping(t *testing.T) { gtest.C(t, func(t *gtest.T) { value, err := db.Table(table).Fields("ID").Where("id", 2).Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.Int(), 2) }) gtest.C(t, func(t *gtest.T) { value, err := db.Table(table).Fields("NICK_NAME").Where("id", 2).Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.String(), "name_2") }) // Map @@ -2817,7 +2817,7 @@ func Test_Model_Fields_AutoMapping(t *testing.T) { "ID": 1, "NICK_NAME": 1, }).Where("id", 2).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(one), 2) t.Assert(one["id"], 2) t.Assert(one["nickname"], "name_2") @@ -2832,7 +2832,7 @@ func Test_Model_Fields_AutoMapping(t *testing.T) { ID: 0, NICKNAME: 0, }).Where("id", 2).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(one), 2) t.Assert(one["id"], 2) t.Assert(one["nickname"], "name_2") @@ -2851,13 +2851,13 @@ func Test_Model_FieldsEx_AutoMapping(t *testing.T) { gtest.C(t, func(t *gtest.T) { value, err := db.Table(table).FieldsEx("Passport, Password, NickName, CreateTime").Where("id", 2).Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.Int(), 2) }) gtest.C(t, func(t *gtest.T) { value, err := db.Table(table).FieldsEx("ID, Passport, Password, CreateTime").Where("id", 2).Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(value.String(), "name_2") }) // Map @@ -2867,7 +2867,7 @@ func Test_Model_FieldsEx_AutoMapping(t *testing.T) { "Password": 1, "CreateTime": 1, }).Where("id", 2).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(one), 2) t.Assert(one["id"], 2) t.Assert(one["nickname"], "name_2") @@ -2884,7 +2884,7 @@ func Test_Model_FieldsEx_AutoMapping(t *testing.T) { Password: 0, CreateTime: 0, }).Where("id", 2).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(one), 2) t.Assert(one["id"], 2) t.Assert(one["nickname"], "name_2") @@ -2905,21 +2905,21 @@ func Test_Model_Fields_Struct(t *testing.T) { } gtest.C(t, func(t *gtest.T) { one, err := db.Table(table).Fields(A{}).Where("id", 2).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(one), 2) t.Assert(one["passport"], "user_2") t.Assert(one["password"], "pass_2") }) gtest.C(t, func(t *gtest.T) { one, err := db.Table(table).Fields(&A{}).Where("id", 2).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(one), 2) t.Assert(one["passport"], "user_2") t.Assert(one["password"], "pass_2") }) gtest.C(t, func(t *gtest.T) { one, err := db.Table(table).Fields(B{}).Where("id", 2).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(one), 3) t.Assert(one["passport"], "user_2") t.Assert(one["password"], "pass_2") @@ -2927,7 +2927,7 @@ func Test_Model_Fields_Struct(t *testing.T) { }) gtest.C(t, func(t *gtest.T) { one, err := db.Table(table).Fields(&B{}).Where("id", 2).One() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(one), 3) t.Assert(one["passport"], "user_2") t.Assert(one["password"], "pass_2") @@ -2948,15 +2948,15 @@ func Test_Model_NullField(t *testing.T) { "passport": nil, } result, err := db.Table(table).Data(data).Insert() - t.Assert(err, nil) + t.AssertNil(err) n, _ := result.RowsAffected() t.Assert(n, 1) one, err := db.Table(table).FindOne(1) - t.Assert(err, nil) + t.AssertNil(err) var user *User err = one.Struct(&user) - t.Assert(err, nil) + t.AssertNil(err) t.Assert(user.Id, data["id"]) t.Assert(user.Passport, data["passport"]) }) @@ -2967,12 +2967,12 @@ func Test_Model_Empty_Slice_Argument(t *testing.T) { defer dropTable(table) gtest.C(t, func(t *gtest.T) { result, err := db.Model(table).Where(`id`, g.Slice{}).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 0) }) gtest.C(t, func(t *gtest.T) { result, err := db.Model(table).Where(`id in(?)`, g.Slice{}).All() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(len(result), 0) }) } @@ -2984,13 +2984,13 @@ func Test_Model_HasTable(t *testing.T) { gtest.C(t, func(t *gtest.T) { result, err := db.HasTable(table) t.Assert(result, true) - t.Assert(err, nil) + t.AssertNil(err) }) gtest.C(t, func(t *gtest.T) { result, err := db.HasTable("table12321") t.Assert(result, false) - t.Assert(err, nil) + t.AssertNil(err) }) } @@ -3001,13 +3001,13 @@ func Test_Model_HasField(t *testing.T) { gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).HasField("id") t.Assert(result, true) - t.Assert(err, nil) + t.AssertNil(err) }) gtest.C(t, func(t *gtest.T) { result, err := db.Table(table).HasField("id123") t.Assert(result, false) - t.Assert(err, nil) + t.AssertNil(err) }) } @@ -3023,56 +3023,56 @@ func Test_Model_Issue1002(t *testing.T) { "nickname": "name_2", "create_time": "2020-10-27 19:03:33", }).Insert() - gtest.Assert(err, nil) + gtest.AssertNil(err) n, _ := result.RowsAffected() gtest.Assert(n, 1) // where + string. gtest.C(t, func(t *gtest.T) { v, err := db.Table(table).Fields("id").Where("create_time>'2020-10-27 19:03:32' and create_time<'2020-10-27 19:03:34'").Value() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.Int(), 1) }) gtest.C(t, func(t *gtest.T) { v, err := db.Table(table).Fields("id").Where("create_time>'2020-10-27 19:03:32' and create_time<'2020-10-27 19:03:34'").FindValue() - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.Int(), 1) }) gtest.C(t, func(t *gtest.T) { v, err := db.Table(table).Where("create_time>'2020-10-27 19:03:32' and create_time<'2020-10-27 19:03:34'").FindValue("id") - t.Assert(err, nil) + t.AssertNil(err) t.Assert(v.Int(), 1) }) // where + string arguments. gtest.C(t, func(t *gtest.T) { v, err := db.Table(table).Fields("id").Where("create_time>? and create_time? and create_time? and create_time? and create_time? and create_time? and create_time? and create_time? and create_time? and create_time? and create_time? and create_time? and create_time100").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") diff --git a/database/gdb/gdb_z_mysql_time_maintain_test.go b/database/gdb/gdb_z_mysql_time_maintain_test.go index 74bce2b97..3133b4c1d 100644 --- a/database/gdb/gdb_z_mysql_time_maintain_test.go +++ b/database/gdb/gdb_z_mysql_time_maintain_test.go @@ -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) }) } diff --git a/database/gdb/gdb_z_mysql_transaction_test.go b/database/gdb/gdb_z_mysql_transaction_test.go index be10ab716..38ea6f4a3 100644 --- a/database/gdb/gdb_z_mysql_transaction_test.go +++ b/database/gdb/gdb_z_mysql_transaction_test.go @@ -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) diff --git a/database/gdb/gdb_z_mysql_types_test.go b/database/gdb/gdb_z_mysql_types_test.go index 792af82a2..bf0b1efb4 100644 --- a/database/gdb/gdb_z_mysql_types_test.go +++ b/database/gdb/gdb_z_mysql_types_test.go @@ -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"]) diff --git a/test/gtest/gtest_t.go b/test/gtest/gtest_t.go index a0854a852..4ce3dae8e 100644 --- a/test/gtest/gtest_t.go +++ b/test/gtest/gtest_t.go @@ -15,74 +15,79 @@ type T struct { *testing.T } -// Assert checks and EQUAL. +// Assert checks `value` and `expect` EQUAL. func (t *T) Assert(value, expect interface{}) { Assert(value, expect) } -// AssertEQ checks and 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 and NOT EQUAL. +// AssertNE checks `value` and `expect` NOT EQUAL. func (t *T) AssertNE(value, expect interface{}) { AssertNE(value, expect) } -// AssertNQ checks and 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 is GREATER THAN . +// 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 is GREATER OR EQUAL THAN . +// 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 is LESS EQUAL THAN . +// 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 is LESS OR EQUAL THAN . +// 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 is IN . -// The should be a slice, -// but the 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 is NOT IN . -// The should be a slice, -// but the 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 . +// 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 to stderr and exit the process. +// Fatal prints `message` to stderr and exit the process. func (t *T) Fatal(message ...interface{}) { Fatal(message...) } diff --git a/test/gtest/gtest_util.go b/test/gtest/gtest_util.go index 8749478be..60b90d8de 100644 --- a/test/gtest/gtest_util.go +++ b/test/gtest/gtest_util.go @@ -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 is the pointer to testing.T of stdlib (*testing.T). -// The parameter 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 is the pointer to testing.T of stdlib (*testing.T). -// The parameter 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 and 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 and 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 and 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 and 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 is GREATER THAN . +// 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 is GREATER OR EQUAL THAN . +// 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 is LESS EQUAL THAN . +// 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 is LESS OR EQUAL THAN . +// 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 is IN . -// The should be a slice, -// but the 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 is NOT IN . -// The should be a slice, -// but the 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 . +// Error panics with given `message`. func Error(message ...interface{}) { panic(fmt.Sprintf("[ERROR] %s", fmt.Sprint(message...))) } -// Fatal prints 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 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) }