mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
add unit test case for types
This commit is contained in:
@ -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())
|
||||
}
|
||||
|
||||
@ -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"])
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user