85 lines
2.8 KiB
Go
85 lines
2.8 KiB
Go
// 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(define.IsErr(err, define.ErrValidatorRuleIsEmpty.Error()), ShouldBeTrue)
|
|
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(define.IsErr(err, define.ErrValidatorRuleParamCntIsTooLess.Error()), ShouldBeTrue)
|
|
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(define.IsErr(err, define.ErrValidatorRuleParamCntIsTooLess.Error()), ShouldBeTrue)
|
|
So(res, ShouldBeEmpty)
|
|
})
|
|
Convey("传入RequiredIf枚举值 -> 参数奇数个", t, func() {
|
|
res, err := DefaultValidateRuleGenerateFunc(define.GenerateRuleExpressConfig{
|
|
Rule: &consts.ValidatorRuleCommonRequiredIf,
|
|
ParamList: []any{1, 2, 3},
|
|
})
|
|
So(err, ShouldNotBeNil)
|
|
So(define.IsErr(err, define.ErrValidatorRuleParamCntIsNotEven.Error()), ShouldBeTrue)
|
|
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)
|
|
})
|
|
}
|