From e1164e935bade0bc243c3231ddb3655ced03fb7f Mon Sep 17 00:00:00 2001 From: John Date: Tue, 15 Oct 2019 21:20:38 +0800 Subject: [PATCH] improve configuration for gdb; add more unit test case for ghttp.Server --- .example/database/gdb/mysql/config.toml | 3 +- .example/database/gdb/mysql/gdb_value.go | 2 +- .example/database/gdb/mysql/issue364.go | 34 ++++++++++ database/gdb/gdb.go | 4 +- database/gdb/gdb_base.go | 4 +- frame/gins/gins.go | 60 +---------------- ...=> ghttp_unit_router_object_rest1_test.go} | 0 .../ghttp_unit_router_object_rest2_test.go | 64 +++++++++++++++++++ text/gstr/gstr_z_unit_parse_test.go | 12 ++++ 9 files changed, 119 insertions(+), 64 deletions(-) create mode 100644 .example/database/gdb/mysql/issue364.go rename net/ghttp/{ghttp_unit_router_object_rest_test.go => ghttp_unit_router_object_rest1_test.go} (100%) create mode 100644 net/ghttp/ghttp_unit_router_object_rest2_test.go diff --git a/.example/database/gdb/mysql/config.toml b/.example/database/gdb/mysql/config.toml index a7fda8102..45a2e8dd9 100644 --- a/.example/database/gdb/mysql/config.toml +++ b/.example/database/gdb/mysql/config.toml @@ -1,5 +1,6 @@ # MySQL数据库配置 [database] - link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test" + debug = true + link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test" diff --git a/.example/database/gdb/mysql/gdb_value.go b/.example/database/gdb/mysql/gdb_value.go index 15581ec19..fb032f53b 100644 --- a/.example/database/gdb/mysql/gdb_value.go +++ b/.example/database/gdb/mysql/gdb_value.go @@ -7,7 +7,7 @@ import ( func main() { db := g.DB() - db.SetDebug(true) + //db.SetDebug(true) type User struct { Id int diff --git a/.example/database/gdb/mysql/issue364.go b/.example/database/gdb/mysql/issue364.go new file mode 100644 index 000000000..cae5f6ace --- /dev/null +++ b/.example/database/gdb/mysql/issue364.go @@ -0,0 +1,34 @@ +package main + +import ( + "github.com/gogf/gf/frame/g" + "time" +) + +func test1() { + db := g.DB() + db.SetDebug(true) + time.Sleep(1 * time.Minute) + r, e := db.Table("test").Where("id", 10000).Count() + if e != nil { + panic(e) + } + g.Dump(r) +} + +func test2() { + db := g.DB() + db.SetDebug(true) + dao := db.Table("test").Safe() + time.Sleep(1 * time.Minute) + r, e := dao.Where("id", 10000).Count() + if e != nil { + panic(e) + } + g.Dump(r) +} + +func main() { + test1() + test2() +} diff --git a/database/gdb/gdb.go b/database/gdb/gdb.go index 054e1bbba..1d6a415c0 100644 --- a/database/gdb/gdb.go +++ b/database/gdb/gdb.go @@ -344,9 +344,9 @@ func (bs *dbBase) getSqlDb(master bool) (sqlDb *sql.DB, err error) { } if bs.maxConnLifetime > 0 { - sqlDb.SetConnMaxLifetime(time.Duration(bs.maxConnLifetime) * time.Second) + sqlDb.SetConnMaxLifetime(bs.maxConnLifetime * time.Second) } else if node.MaxConnLifetime > 0 { - sqlDb.SetConnMaxLifetime(time.Duration(node.MaxConnLifetime) * time.Second) + sqlDb.SetConnMaxLifetime(node.MaxConnLifetime * time.Second) } return sqlDb }, 0) diff --git a/database/gdb/gdb_base.go b/database/gdb/gdb_base.go index 39b2d5bb8..63eba096f 100644 --- a/database/gdb/gdb_base.go +++ b/database/gdb/gdb_base.go @@ -543,9 +543,7 @@ func (bs *dbBase) doUpdate(link dbLink, table string, data interface{}, conditio } params := []interface{}(nil) switch kind { - case reflect.Map: - fallthrough - case reflect.Struct: + case reflect.Map, reflect.Struct: var fields []string for k, v := range structToMap(data) { fields = append(fields, bs.db.quoteWord(k)+"=?") diff --git a/frame/gins/gins.go b/frame/gins/gins.go index a35f4aa61..c7fca2926 100644 --- a/frame/gins/gins.go +++ b/frame/gins/gins.go @@ -27,7 +27,6 @@ import ( const ( gFRAME_CORE_COMPONENT_NAME_REDIS = "gf.core.component.redis" - gFRAME_CORE_COMPONENT_NAME_GKVDB = "gf.core.component.gkvdb" gFRAME_CORE_COMPONENT_NAME_DATABASE = "gf.core.component.database" ) @@ -160,66 +159,13 @@ func parseDBConfigNode(value interface{}) *gdb.ConfigNode { return nil } node := &gdb.ConfigNode{} - if value, ok := nodeMap["host"]; ok { - node.Host = gconv.String(value) - } - if value, ok := nodeMap["port"]; ok { - node.Port = gconv.String(value) - } - if value, ok := nodeMap["user"]; ok { - node.User = gconv.String(value) - } - if value, ok := nodeMap["pass"]; ok { - node.Pass = gconv.String(value) - } - if value, ok := nodeMap["name"]; ok { - node.Name = gconv.String(value) - } - if value, ok := nodeMap["type"]; ok { - node.Type = gconv.String(value) - } - if value, ok := nodeMap["role"]; ok { - node.Role = gconv.String(value) - } - if value, ok := nodeMap["debug"]; ok { - node.Debug = gconv.Bool(value) - } - if value, ok := nodeMap["charset"]; ok { - node.Charset = gconv.String(value) - } - if value, ok := nodeMap["weight"]; ok { - node.Weight = gconv.Int(value) - } - if value, ok := nodeMap["linkinfo"]; ok { - node.LinkInfo = gconv.String(value) - } - if value, ok := nodeMap["link-info"]; ok { - node.LinkInfo = gconv.String(value) - } - if value, ok := nodeMap["linkInfo"]; ok { - node.LinkInfo = gconv.String(value) + err := gconv.Struct(nodeMap, node) + if err != nil { + glog.Error(err) } if value, ok := nodeMap["link"]; ok { node.LinkInfo = gconv.String(value) } - if value, ok := nodeMap["max-idle"]; ok { - node.MaxIdleConnCount = gconv.Int(value) - } - if value, ok := nodeMap["maxIdle"]; ok { - node.MaxIdleConnCount = gconv.Int(value) - } - if value, ok := nodeMap["max-open"]; ok { - node.MaxOpenConnCount = gconv.Int(value) - } - if value, ok := nodeMap["maxOpen"]; ok { - node.MaxOpenConnCount = gconv.Int(value) - } - if value, ok := nodeMap["max-lifetime"]; ok { - node.MaxConnLifetime = gconv.Duration(value) - } - if value, ok := nodeMap["maxLifetime"]; ok { - node.MaxConnLifetime = gconv.Duration(value) - } // Parse link syntax. if node.LinkInfo != "" && node.Type == "" { match, _ := gregex.MatchString(`([a-z]+):(.+)`, node.LinkInfo) diff --git a/net/ghttp/ghttp_unit_router_object_rest_test.go b/net/ghttp/ghttp_unit_router_object_rest1_test.go similarity index 100% rename from net/ghttp/ghttp_unit_router_object_rest_test.go rename to net/ghttp/ghttp_unit_router_object_rest1_test.go diff --git a/net/ghttp/ghttp_unit_router_object_rest2_test.go b/net/ghttp/ghttp_unit_router_object_rest2_test.go new file mode 100644 index 000000000..99e8b9197 --- /dev/null +++ b/net/ghttp/ghttp_unit_router_object_rest2_test.go @@ -0,0 +1,64 @@ +// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package ghttp_test + +import ( + "fmt" + "testing" + "time" + + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/net/ghttp" + "github.com/gogf/gf/test/gtest" +) + +type ObjectRest2 struct{} + +func (o *ObjectRest2) Init(r *ghttp.Request) { + r.Response.Write("1") +} + +func (o *ObjectRest2) Shut(r *ghttp.Request) { + r.Response.Write("2") +} + +func (o *ObjectRest2) Get(r *ghttp.Request) { + r.Response.Write("Object Get", r.Get("id")) +} + +func (o *ObjectRest2) Put(r *ghttp.Request) { + r.Response.Write("Object Put", r.Get("id")) +} + +func (o *ObjectRest2) Post(r *ghttp.Request) { + r.Response.Write("Object Post", r.Get("id")) +} + +func (o *ObjectRest2) Delete(r *ghttp.Request) { + r.Response.Write("Object Delete", r.Get("id")) +} + +func Test_Router_ObjectRest_Id(t *testing.T) { + p := ports.PopRand() + s := g.Server(p) + s.BindObjectRest("/object/:id", new(ObjectRest2)) + s.SetPort(p) + s.SetDumpRouteMap(false) + s.Start() + defer s.Shutdown() + + time.Sleep(100 * time.Millisecond) + gtest.Case(t, func() { + client := ghttp.NewClient() + client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) + + gtest.Assert(client.GetContent("/object/99"), "1Object Get992") + gtest.Assert(client.PutContent("/object/99"), "1Object Put992") + gtest.Assert(client.PostContent("/object/99"), "1Object Post992") + gtest.Assert(client.DeleteContent("/object/99"), "1Object Delete992") + }) +} diff --git a/text/gstr/gstr_z_unit_parse_test.go b/text/gstr/gstr_z_unit_parse_test.go index ad78493ff..545716799 100644 --- a/text/gstr/gstr_z_unit_parse_test.go +++ b/text/gstr/gstr_z_unit_parse_test.go @@ -9,6 +9,7 @@ package gstr_test import ( + "net/url" "testing" "github.com/gogf/gf/frame/g" @@ -18,6 +19,17 @@ import ( func Test_Parse(t *testing.T) { gtest.Case(t, func() { + // url + gtest.Case(t, func() { + s := "goframe.org/index?name=john&score=100" + u, err := url.Parse(s) + gtest.Assert(err, nil) + m, err := gstr.Parse(u.RawQuery) + gtest.Assert(err, nil) + gtest.Assert(m["name"], "john") + gtest.Assert(m["score"], "100") + }) + // name overwrite m, err := gstr.Parse("a=1&a=2") gtest.Assert(err, nil)