validator/run_test.go

131 lines
3.0 KiB
Go
Raw Normal View History

2024-04-29 17:29:46 +08:00
// Package validator ...
//
// Description : validator ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-04-29 14:14
package validator
import (
"encoding/json"
"fmt"
"git.zhangdeman.cn/gateway/validator/define"
2024-09-05 17:49:50 +08:00
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
2024-04-29 17:29:46 +08:00
"testing"
)
func TestRun(t *testing.T) {
sourceData := map[string]interface{}{
"name": "白茶清欢",
}
_ = Run(sourceData, nil, nil)
2024-04-29 17:29:46 +08:00
byteData, _ := json.Marshal(sourceData)
fmt.Println(string(byteData))
}
2024-09-05 17:49:50 +08:00
func Test_getDataStatus(t *testing.T) {
type args struct {
val gjson.Result
dataType string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, getDataStatus(tt.args.val, tt.args.dataType), "getDataStatus(%v, %v)", tt.args.val, tt.args.dataType)
})
}
}
// TestRunRequired 测试字段必传
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:48 2024/11/1
func TestRunRequired(t *testing.T) {
sourceData := map[string]interface{}{
"name": "白茶清欢",
}
ruleListForNotFound := []*define.FieldRule{
&define.FieldRule{
Path: "name1",
Type: "string",
DisableRewrite: false,
DefaultValue: nil,
IsRequired: true,
AllowEmpty: false,
AllowZero: false,
AllowNil: false,
DisableAutoTrimSpace: false,
DisableAutoConvert: false,
RequiredConditionGroup: nil,
ValueLimit: nil,
SliceConfig: nil,
MapConfig: nil,
},
}
err := Run(sourceData, ruleListForNotFound, nil)
if nil != err {
fmt.Println(err.Error())
}
assert.Error(t, err)
sourceData = map[string]interface{}{
"name": "",
}
ruleListForEmpty := []*define.FieldRule{
&define.FieldRule{
Path: "name",
Type: "string",
DisableRewrite: false,
DefaultValue: nil,
IsRequired: true,
AllowEmpty: false,
AllowZero: false,
AllowNil: false,
DisableAutoTrimSpace: false,
DisableAutoConvert: false,
},
}
err = Run(sourceData, ruleListForEmpty, nil)
if nil != err {
fmt.Println(err.Error())
}
assert.Error(t, err)
sourceData = map[string]interface{}{
"name": 18,
}
ruleListForDisableAutoConvert := []*define.FieldRule{
&define.FieldRule{
Path: "name",
Type: "string",
DisableRewrite: false,
DefaultValue: nil,
IsRequired: true,
AllowEmpty: false,
AllowZero: false,
AllowNil: false,
DisableAutoTrimSpace: false,
DisableAutoConvert: true,
RequiredConditionGroup: nil,
ValueLimit: nil,
SliceConfig: nil,
MapConfig: nil,
},
}
err = Run(sourceData, ruleListForDisableAutoConvert, nil)
if nil != err {
fmt.Println(err.Error())
}
assert.Error(t, err)
}