DefaultValidateRuleGenerateFunc 增加单元测试
This commit is contained in:
@ -32,7 +32,7 @@ func DefaultValidateRuleGenerateFunc(validateRule define.GenerateRuleExpressConf
|
||||
if ruleConfig.MinParamCnt > 0 && len(validateRule.ParamList) < ruleConfig.MinParamCnt {
|
||||
return "", errors.New(ruleConfig.ValidatorRule.String() + " : validate rule param count is less than min param cnt -> " + fmt.Sprintf("%v", ruleConfig.MinParamCnt))
|
||||
}
|
||||
if ruleConfig.MaxParamCnt > 0 && len(validateRule.ParamList) > ruleConfig.MinParamCnt {
|
||||
if ruleConfig.MaxParamCnt > 0 && len(validateRule.ParamList) > ruleConfig.MaxParamCnt {
|
||||
return "", errors.New(ruleConfig.ValidatorRule.String() + " : validate rule param count is more than max param cnt -> " + fmt.Sprintf("%v", ruleConfig.MaxParamCnt))
|
||||
}
|
||||
if ruleConfig.ParamCntMustEven && len(validateRule.ParamList)%2 != 0 {
|
||||
|
80
v10/default_test.go
Normal file
80
v10/default_test.go
Normal file
@ -0,0 +1,80 @@
|
||||
// Package v10 ...
|
||||
//
|
||||
// Description : v10 ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2025-01-24 12:01
|
||||
package v10
|
||||
|
||||
import (
|
||||
"git.zhangdeman.cn/gateway/validator/v10/define"
|
||||
"git.zhangdeman.cn/zhangdeman/consts"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// TestDefaultValidateRuleGenerateFunc 表达式生成函数测试
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 12:02 2025/1/24
|
||||
func TestDefaultValidateRuleGenerateFunc(t *testing.T) {
|
||||
Convey("传入的验证规则未注册", t, func() {
|
||||
testRule := consts.ValidatorRule("invalid_rule")
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &testRule,
|
||||
ParamList: nil,
|
||||
})
|
||||
So(err, ShouldBeError)
|
||||
So(res, ShouldBeEmpty)
|
||||
})
|
||||
Convey("传入规则无参数", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonRequired,
|
||||
ParamList: nil,
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldEqual, consts.ValidatorRuleCommonRequired.String())
|
||||
})
|
||||
Convey("传入oneof枚举值缺少枚举值列表", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonOneOf,
|
||||
ParamList: nil,
|
||||
})
|
||||
So(err, ShouldBeError)
|
||||
So(res, ShouldEqual, "")
|
||||
})
|
||||
Convey("传入oneof枚举值构建成功", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonOneOf,
|
||||
ParamList: []any{"name", "1", 2, 3},
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeEmpty)
|
||||
})
|
||||
Convey("传入RequiredIf枚举值 -> 参数少于2", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonRequiredIf,
|
||||
ParamList: []any{},
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
So(res, ShouldBeEmpty)
|
||||
})
|
||||
Convey("传入RequiredIf枚举值 -> 参数奇数个", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonRequiredIf,
|
||||
ParamList: []any{1, 2, 3},
|
||||
})
|
||||
So(err, ShouldNotBeNil)
|
||||
So(res, ShouldBeEmpty)
|
||||
})
|
||||
Convey("传入RequiredIf枚举值 -> 构建成功", t, func() {
|
||||
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
||||
Rule: &consts.ValidatorRuleCommonRequiredIf,
|
||||
ParamList: []any{1, 2, 3, 4},
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(res, ShouldNotBeEmpty)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user