From c5d80a2192f2238bc4a46a08afd730f27757632d Mon Sep 17 00:00:00 2001 From: John Guo Date: Thu, 17 Mar 2022 21:31:07 +0800 Subject: [PATCH] improve UT cases for package gins/gvalid --- frame/gins/gins_z_unit_server_test.go | 77 ++++++++----------- .../gvalid_z_unit_feature_checkstruct_test.go | 44 ----------- .../gvalid_z_unit_feature_recursive_test.go | 44 +++++++++++ 3 files changed, 78 insertions(+), 87 deletions(-) diff --git a/frame/gins/gins_z_unit_server_test.go b/frame/gins/gins_z_unit_server_test.go index 40e98435c..9cbf1d2c6 100644 --- a/frame/gins/gins_z_unit_server_test.go +++ b/frame/gins/gins_z_unit_server_test.go @@ -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`) +// }) +//} diff --git a/util/gvalid/gvalid_z_unit_feature_checkstruct_test.go b/util/gvalid/gvalid_z_unit_feature_checkstruct_test.go index fbd6aa18f..7e182d84c 100755 --- a/util/gvalid/gvalid_z_unit_feature_checkstruct_test.go +++ b/util/gvalid/gvalid_z_unit_feature_checkstruct_test.go @@ -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 { diff --git a/util/gvalid/gvalid_z_unit_feature_recursive_test.go b/util/gvalid/gvalid_z_unit_feature_recursive_test.go index b441f7c70..269d2eab4 100755 --- a/util/gvalid/gvalid_z_unit_feature_recursive_test.go +++ b/util/gvalid/gvalid_z_unit_feature_recursive_test.go @@ -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`) + }) +}