修复相关BUG + 增加单元测试, 覆盖率100%
This commit is contained in:
64
validator_test.go
Normal file
64
validator_test.go
Normal file
@ -0,0 +1,64 @@
|
||||
// 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)
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user