From 697dbdc604505a4836c3dd38c44d7a698078d9fe Mon Sep 17 00:00:00 2001 From: John Date: Mon, 22 Jul 2019 23:03:55 +0800 Subject: [PATCH] add unit test case for types --- g/database/gdb/gdb_func.go | 5 +++ g/database/gdb/gdb_unit_types_test.go | 53 ++++++++++++++++++++------- 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/g/database/gdb/gdb_func.go b/g/database/gdb/gdb_func.go index 1dd68c58a..4bbfaa002 100644 --- a/g/database/gdb/gdb_func.go +++ b/g/database/gdb/gdb_func.go @@ -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()) } diff --git a/g/database/gdb/gdb_unit_types_test.go b/g/database/gdb/gdb_unit_types_test.go index 65555c432..df9b8d4e6 100644 --- a/g/database/gdb/gdb_unit_types_test.go +++ b/g/database/gdb/gdb_unit_types_test.go @@ -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"]) }) }