add unit test case for types

This commit is contained in:
John
2019-07-22 23:03:55 +08:00
parent 86f98f3710
commit 697dbdc604
2 changed files with 44 additions and 14 deletions

View File

@ -44,6 +44,11 @@ func formatQuery(query string, args []interface{}) (newQuery string, newArgs []i
switch kind {
// '?'占位符支持slice类型, 这里会将slice参数拆散并更新原有占位符'?'为多个'?',使用','符号连接。
case reflect.Slice, reflect.Array:
// 不拆分[]byte类型
if _, ok := arg.([]byte); ok {
newArgs = append(newArgs, arg)
continue
}
for i := 0; i < rv.Len(); i++ {
newArgs = append(newArgs, rv.Index(i).Interface())
}

View File

@ -10,26 +10,51 @@ import (
"fmt"
"testing"
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/test/gtest"
)
func Test_Types(t *testing.T) {
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE types (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
blob blob NOT NULL,
binary binary(8) NOT NULL ,
date varchar(45) NOT NULL ,
decimal decimal(5,2) NOT NULL ',
double double NOT NULL ',
bit bit(2) NOT NULL ',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`)); err != nil {
gtest.Error(err)
}
gtest.Case(t, func() {
if _, err := db.Exec(fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS types (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
%s blob NOT NULL,
%s binary(8) NOT NULL,
%s date NOT NULL,
%s decimal(5,2) NOT NULL,
%s double NOT NULL,
%s bit(2) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`, "`blob`", "`binary`", "`date`", "`decimal`", "`double`", "`bit`")); err != nil {
gtest.Error(err)
}
defer dropTable("types")
data := g.Map{
"id": 1,
"blob": "i love gf",
"binary": []byte("abcdefgh"),
"date": "2018-10-24",
"decimal": 123.456,
"double": 123.456,
"bit": 2,
}
r, err := db.Table("types").Data(data).Insert()
gtest.Assert(err, nil)
n, _ := r.RowsAffected()
gtest.Assert(n, 1)
one, err := db.Table("types").One()
gtest.Assert(err, nil)
gtest.Assert(one["id"].Int(), 1)
gtest.Assert(one["blob"].String(), data["blob"])
gtest.Assert(one["binary"].String(), data["binary"])
gtest.Assert(one["date"].String(), data["date"])
gtest.Assert(one["decimal"].String(), 123.46)
gtest.Assert(one["double"].String(), data["double"])
gtest.Assert(one["bit"].Int(), data["bit"])
})
}