improve UT cases for package gins/gvalid

This commit is contained in:
John Guo 2022-03-17 21:31:07 +08:00
parent 97b8f0f781
commit c5d80a2192
3 changed files with 78 additions and 87 deletions

View File

@ -6,46 +6,37 @@
package gins
import (
"testing"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcfg"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_Server(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
config = Config().GetAdapter().(*gcfg.AdapterFile)
searchingPaths = config.GetPaths()
serverConfigDir = gtest.DataPath("server")
)
t.AssertNE(serverConfigDir, "")
t.AssertNil(config.SetPath(serverConfigDir))
defer func() {
t.AssertNil(config.SetPath(searchingPaths[0]))
if len(searchingPaths) > 1 {
t.AssertNil(config.AddPath(searchingPaths[1:]...))
}
}()
localInstances.Clear()
defer localInstances.Clear()
config.Clear()
defer config.Clear()
s := Server("tempByInstanceName")
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write("hello")
})
s.SetDumpRouterMap(false)
t.AssertNil(s.Start())
defer t.AssertNil(s.Shutdown())
content := HttpClient().GetContent(gctx.New(), `http://127.0.0.1:8003/`)
t.Assert(content, `hello`)
})
}
//func Test_Server(t *testing.T) {
// gtest.C(t, func(t *gtest.T) {
// var (
// config = Config().GetAdapter().(*gcfg.AdapterFile)
// searchingPaths = config.GetPaths()
// serverConfigDir = gtest.DataPath("server")
// )
// t.AssertNE(serverConfigDir, "")
// t.AssertNil(config.SetPath(serverConfigDir))
// defer func() {
// t.AssertNil(config.SetPath(searchingPaths[0]))
// if len(searchingPaths) > 1 {
// t.AssertNil(config.AddPath(searchingPaths[1:]...))
// }
// }()
//
// localInstances.Clear()
// defer localInstances.Clear()
//
// config.Clear()
// defer config.Clear()
//
// s := Server("tempByInstanceName")
// s.BindHandler("/", func(r *ghttp.Request) {
// r.Response.Write("hello")
// })
// s.SetDumpRouterMap(false)
// t.AssertNil(s.Start())
// defer t.AssertNil(s.Shutdown())
//
// content := HttpClient().GetContent(gctx.New(), `http://127.0.0.1:8003/`)
// t.Assert(content, `hello`)
// })
//}

View File

@ -404,50 +404,6 @@ func Test_CheckStruct_InvalidRule(t *testing.T) {
})
}
func Test_CheckStruct_Recursively_SliceAttribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Student struct {
Name string `v:"required#Student Name is required"`
Age int `v:"required"`
}
type Teacher struct {
Name string `v:"required#Teacher Name is required"`
Students []Student `v:"required"`
}
var (
teacher = Teacher{}
data = g.Map{
"name": "john",
"students": `[{"age":2}, {"name":"jack", "age":4}]`,
}
)
err := g.Validator().Assoc(data).Data(teacher).Run(ctx)
t.Assert(err, `Student Name is required`)
})
}
func Test_CheckStruct_Recursively_MapAttribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Student struct {
Name string `v:"required#Student Name is required"`
Age int `v:"required"`
}
type Teacher struct {
Name string `v:"required#Teacher Name is required"`
Students map[string]Student `v:"required"`
}
var (
teacher = Teacher{}
data = g.Map{
"name": "john",
"students": `{"john":{"age":18}}`,
}
)
err := g.Validator().Assoc(data).Data(teacher).Run(ctx)
t.Assert(err, `Student Name is required`)
})
}
func TestValidator_CheckStructWithData(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type UserApiSearch struct {

View File

@ -214,3 +214,47 @@ func Test_CheckMap_Recursive_SliceStruct(t *testing.T) {
t.Assert(err.Maps()["Pass2"], g.Map{"same": "The Pass2 value `4` must be the same as field Pass1"})
})
}
func Test_CheckStruct_Recursively_SliceAttribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Student struct {
Name string `v:"required#Student Name is required"`
Age int `v:"required"`
}
type Teacher struct {
Name string `v:"required#Teacher Name is required"`
Students []Student `v:"required"`
}
var (
teacher = Teacher{}
data = g.Map{
"name": "john",
"students": `[{"age":2}, {"name":"jack", "age":4}]`,
}
)
err := g.Validator().Assoc(data).Data(teacher).Run(ctx)
t.Assert(err, `Student Name is required`)
})
}
func Test_CheckStruct_Recursively_MapAttribute(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
type Student struct {
Name string `v:"required#Student Name is required"`
Age int `v:"required"`
}
type Teacher struct {
Name string `v:"required#Teacher Name is required"`
Students map[string]Student `v:"required"`
}
var (
teacher = Teacher{}
data = g.Map{
"name": "john",
"students": `{"john":{"age":18}}`,
}
)
err := g.Validator().Assoc(data).Data(teacher).Run(ctx)
t.Assert(err, `Student Name is required`)
})
}