261 lines
6.6 KiB
Go
261 lines
6.6 KiB
Go
// Package validator ...
|
|
//
|
|
// Description : validator ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 2024-05-01 22:57
|
|
package validator
|
|
|
|
import (
|
|
"git.zhangdeman.cn/gateway/validator/define"
|
|
"git.zhangdeman.cn/zhangdeman/consts"
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
)
|
|
|
|
// Test_handleFloat float转换单元测试
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 23:00 2024/5/1
|
|
func Test_handleFloat(t *testing.T) {
|
|
minVal0 := float64(0)
|
|
minVal1 := float64(1)
|
|
maxVal := float64(100)
|
|
as := assert.New(t)
|
|
checkRule := &define.FieldRule{
|
|
Path: "test_float",
|
|
Type: consts.DataTypeFloat,
|
|
DisableRewrite: false,
|
|
DefaultValue: "",
|
|
IsRequired: false,
|
|
AllowEmpty: false,
|
|
AllowZero: false,
|
|
AllowNil: false,
|
|
DisableAutoConvert: false,
|
|
RequiredConditionGroup: nil,
|
|
ValueLimit: &define.ValueLimit{
|
|
EnumList: nil,
|
|
Min: nil,
|
|
Max: nil,
|
|
String: nil,
|
|
Map: nil,
|
|
},
|
|
SliceConfig: nil,
|
|
}
|
|
int1 := int64(1)
|
|
f1, err := handleFloat(int1, checkRule)
|
|
as.Equal(nil, err)
|
|
as.Equal(float64(1), f1)
|
|
// 不允许零值
|
|
int0 := int64(0)
|
|
f0, err := handleFloat(int0, checkRule)
|
|
as.Equal(float64(0), f0)
|
|
as.NotEqual(nil, err)
|
|
// 允许零值
|
|
checkRule.AllowZero = true
|
|
f0, err = handleFloat(int0, checkRule)
|
|
as.Equal(float64(0), f0)
|
|
as.Equal(nil, err)
|
|
// 取值范围符合
|
|
checkRule.ValueLimit.Min = &minVal0
|
|
checkRule.ValueLimit.Max = &maxVal
|
|
f0, err = handleFloat(int0, checkRule)
|
|
as.Equal(float64(0), f0)
|
|
as.Equal(nil, err)
|
|
// 取值范围不符合
|
|
checkRule.ValueLimit.Min = &minVal1
|
|
f0, err = handleFloat(int0, checkRule)
|
|
as.Equal(float64(0), f0)
|
|
as.NotEqual(nil, err)
|
|
// 验证枚举值(不包含)
|
|
checkRule.ValueLimit.EnumList = []string{"0", "2"}
|
|
f1, err = handleFloat(int1, checkRule)
|
|
as.Equal(float64(0), f1)
|
|
as.NotEqual(nil, err)
|
|
// 验证枚举值(包含)
|
|
checkRule.ValueLimit.EnumList = []string{"0", "1", "2"}
|
|
f1, err = handleFloat(int1, checkRule)
|
|
as.Equal(float64(1), f1)
|
|
as.Equal(nil, err)
|
|
}
|
|
|
|
// Test_handleInt ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 16:34 2024/5/2
|
|
func Test_handleInt(t *testing.T) {
|
|
minVal0 := float64(0)
|
|
minVal1 := float64(1)
|
|
maxVal := float64(100)
|
|
as := assert.New(t)
|
|
checkRule := &define.FieldRule{
|
|
Path: "test_int",
|
|
Type: consts.DataTypeInt,
|
|
DisableRewrite: false,
|
|
DefaultValue: "",
|
|
IsRequired: false,
|
|
AllowEmpty: false,
|
|
AllowZero: false,
|
|
AllowNil: false,
|
|
DisableAutoConvert: false,
|
|
RequiredConditionGroup: nil,
|
|
ValueLimit: &define.ValueLimit{
|
|
EnumList: nil,
|
|
Min: nil,
|
|
Max: nil,
|
|
String: nil,
|
|
Map: nil,
|
|
},
|
|
SliceConfig: nil,
|
|
}
|
|
int1 := int64(1)
|
|
f1, err := handleInt(int1, checkRule)
|
|
as.Equal(nil, err)
|
|
as.Equal(int64(1), f1)
|
|
// 不允许零值
|
|
int0 := int64(0)
|
|
f0, err := handleInt(int0, checkRule)
|
|
as.Equal(int64(0), f0)
|
|
as.NotEqual(nil, err)
|
|
// 允许零值
|
|
checkRule.AllowZero = true
|
|
f0, err = handleInt(int0, checkRule)
|
|
as.Equal(int64(0), f0)
|
|
as.Equal(nil, err)
|
|
// 取值范围符合
|
|
checkRule.ValueLimit.Min = &minVal0
|
|
checkRule.ValueLimit.Max = &maxVal
|
|
f0, err = handleInt(int0, checkRule)
|
|
as.Equal(int64(0), f0)
|
|
as.Equal(nil, err)
|
|
// 取值范围不符合
|
|
checkRule.ValueLimit.Min = &minVal1
|
|
f0, err = handleInt(int0, checkRule)
|
|
as.Equal(int64(0), f0)
|
|
as.NotEqual(nil, err)
|
|
// 验证枚举值(不包含)
|
|
checkRule.ValueLimit.EnumList = []string{"0", "2"}
|
|
f1, err = handleInt(int1, checkRule)
|
|
as.Equal(int64(0), f1)
|
|
as.NotEqual(nil, err)
|
|
// 验证枚举值(包含)
|
|
checkRule.ValueLimit.EnumList = []string{"0", "1", "2"}
|
|
f1, err = handleInt(int1, checkRule)
|
|
as.Equal(int64(1), f1)
|
|
as.Equal(nil, err)
|
|
}
|
|
|
|
// Test_handleUint...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 16:41 2024/5/2
|
|
func Test_handleUint(t *testing.T) {
|
|
minVal0 := float64(0)
|
|
minVal1 := float64(1)
|
|
maxVal := float64(100)
|
|
as := assert.New(t)
|
|
checkRule := &define.FieldRule{
|
|
Path: "test_int",
|
|
Type: consts.DataTypeInt,
|
|
DisableRewrite: false,
|
|
DefaultValue: "",
|
|
IsRequired: false,
|
|
AllowEmpty: false,
|
|
AllowZero: false,
|
|
AllowNil: false,
|
|
DisableAutoConvert: false,
|
|
RequiredConditionGroup: nil,
|
|
ValueLimit: &define.ValueLimit{
|
|
EnumList: nil,
|
|
Min: nil,
|
|
Max: nil,
|
|
String: nil,
|
|
Map: nil,
|
|
},
|
|
SliceConfig: nil,
|
|
}
|
|
int1 := uint64(1)
|
|
f1, err := handleUint(int1, checkRule)
|
|
as.Equal(nil, err)
|
|
as.Equal(uint64(1), f1)
|
|
// 不允许零值
|
|
int0 := uint64(0)
|
|
f0, err := handleUint(int0, checkRule)
|
|
as.Equal(uint64(0), f0)
|
|
as.NotEqual(nil, err)
|
|
// 允许零值
|
|
checkRule.AllowZero = true
|
|
f0, err = handleUint(int0, checkRule)
|
|
as.Equal(uint64(0), f0)
|
|
as.Equal(nil, err)
|
|
// 取值范围符合
|
|
checkRule.ValueLimit.Min = &minVal0
|
|
checkRule.ValueLimit.Max = &maxVal
|
|
f0, err = handleUint(int0, checkRule)
|
|
as.Equal(uint64(0), f0)
|
|
as.Equal(nil, err)
|
|
// 取值范围不符合
|
|
checkRule.ValueLimit.Min = &minVal1
|
|
f0, err = handleUint(int0, checkRule)
|
|
as.Equal(uint64(0), f0)
|
|
as.NotEqual(nil, err)
|
|
// 验证枚举值(不包含)
|
|
checkRule.ValueLimit.EnumList = []string{"0", "2"}
|
|
f1, err = handleUint(int1, checkRule)
|
|
as.Equal(uint64(0), f1)
|
|
as.NotEqual(nil, err)
|
|
// 验证枚举值(包含)
|
|
checkRule.ValueLimit.EnumList = []string{"0", "1", "2"}
|
|
f1, err = handleUint(int1, checkRule)
|
|
as.Equal(uint64(1), f1)
|
|
as.Equal(nil, err)
|
|
}
|
|
|
|
// Test_handleBool ...
|
|
//
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
//
|
|
// Date : 17:31 2024/5/2
|
|
func Test_handleBool(t *testing.T) {
|
|
as := assert.New(t)
|
|
checkRule := &define.FieldRule{
|
|
Path: "test_bool",
|
|
Type: consts.DataTypeBool,
|
|
DisableRewrite: false,
|
|
DefaultValue: "",
|
|
IsRequired: false,
|
|
AllowEmpty: false,
|
|
AllowZero: false,
|
|
AllowNil: false,
|
|
DisableAutoConvert: false,
|
|
RequiredConditionGroup: nil,
|
|
ValueLimit: nil,
|
|
SliceConfig: nil,
|
|
}
|
|
str1 := "1"
|
|
b, err := handleBool(str1, checkRule)
|
|
as.Equal(true, b)
|
|
as.Equal(nil, err)
|
|
|
|
str2 := "false"
|
|
b, err = handleBool(str2, checkRule)
|
|
as.Equal(false, b)
|
|
as.Equal(nil, err)
|
|
|
|
str3 := "true"
|
|
b, err = handleBool(str3, checkRule)
|
|
as.Equal(true, b)
|
|
as.Equal(nil, err)
|
|
|
|
b, err = handleBool(false, checkRule)
|
|
as.Equal(false, b)
|
|
as.Equal(nil, err)
|
|
b, err = handleBool(true, checkRule)
|
|
as.Equal(true, b)
|
|
as.Equal(nil, err)
|
|
}
|