65 lines
2.0 KiB
Go
65 lines
2.0 KiB
Go
// Package consts ...
|
|
//
|
|
// Description : consts ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2025-04-20 17:37
|
|
package consts
|
|
|
|
import (
|
|
"encoding/json"
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidatorRule_String(t *testing.T) {
|
|
Convey("validate rule 字符串值", t, func() {
|
|
byteData, err := json.Marshal(ValidateRuleGt)
|
|
So(err, ShouldBeNil)
|
|
So(ValidateRuleGt.String(), ShouldEqual, "gt")
|
|
So(string(byteData), ShouldEqual, `"gt"`)
|
|
})
|
|
Convey("validate rule MarshalJSON", t, func() {
|
|
str, err := ValidateRuleGt.MarshalJSON()
|
|
So(err, ShouldBeNil)
|
|
So(string(str), ShouldEqual, `"gt"`)
|
|
dataList := []ValidatorRule{ValidateRuleGt}
|
|
jsonData, err := json.Marshal(dataList)
|
|
So(err, ShouldBeNil)
|
|
So(string(jsonData), ShouldEqual, `["gt"]`)
|
|
})
|
|
}
|
|
|
|
func TestValidatorRule_IsValid(t *testing.T) {
|
|
Convey("validator rule 不存在", t, func() {
|
|
So(ValidatorRule("Invalid").IsValid(), ShouldBeFalse)
|
|
})
|
|
Convey("validator rule 配置值错误", t, func() {
|
|
ValidatorRuleSupportDataTypeTable[ValidatorRule("Invalid")] = ValidatorRuleConfig{}
|
|
So(ValidatorRule("Invalid").IsValid(), ShouldBeFalse)
|
|
So(ValidatorRule("gt").IsValid(), ShouldBeTrue)
|
|
})
|
|
Convey("validator rule 配置值", t, func() {
|
|
So(ValidatorRuleGtcsfield.Config().MinParamCnt, ShouldEqual, 1)
|
|
})
|
|
}
|
|
|
|
func TestValidatorRule_IsSupportDataType(t *testing.T) {
|
|
Convey("validator rule 是否支持指定数据类型", t, func() {
|
|
So(ValidatorRuleGtcsfield.IsSupportDataType(DataTypeUint), ShouldBeTrue)
|
|
So(ValidatorRuleGtcsfield.IsSupportDataType(DataTypeString), ShouldBeFalse)
|
|
})
|
|
Convey("validator rule 未指定类型默认支持", t, func() {
|
|
So(ValidatorRuleCommonOmitempty.IsSupportDataType(DataTypeString), ShouldBeTrue)
|
|
})
|
|
}
|
|
|
|
func TestRegisterCustomValidatorRule(t *testing.T) {
|
|
Convey("注册自定义验证规则", t, func() {
|
|
RegisterCustomValidatorRule(ValidatorRule("Invalid"), ValidatorRuleConfig{})
|
|
_, exist := ValidatorRuleSupportDataTypeTable[ValidatorRule("Invalid")]
|
|
So(exist, ShouldBeTrue)
|
|
})
|
|
}
|