pkg/validator/validator_test.go

62 lines
1.3 KiB
Go

package validator
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type testStruct struct {
EnvID string `json:"uid" validate:"required"`
Force bool `json:"-" validate:"required"`
Operator string `validate:"required"`
}
type testStruct2 struct {
EnvID string `json:"uid" validate:"required"`
Force bool `json:"-"`
Operator string `validate:"required"`
}
type testStruct3 struct {
Name string `json:"name" validate:"required,gt=1"`
Count int `json:"count" validate:"required,gt=0"`
}
func (s *testStruct2) Validate() error {
return fmt.Errorf("hit validate")
}
func TestValidate(t *testing.T) {
d := testStruct{}
err := Struct(context.Background(), d)
assert.Equal(t, err.Error(), "uid is a required field")
t2 := &testStruct2{
EnvID: "abc",
Force: false,
Operator: "ab",
}
err = Struct(context.Background(), t2)
if assert.Error(t, err) {
assert.Equal(t, err.Error(), "hit validate")
}
t3 := testStruct3{
Name: "dd_dabc",
Count: 1,
}
err = Struct(context.Background(), t3)
assert.NoError(t, err)
name := "testValidate"
err = Struct(context.Background(), name)
assert.Equal(t, err.Error(), "validator: (nil string)")
// assert.Equal(t, err.Error(), "Force is required")
// assert.Equal(t, err.Error(), "Operator is required")
}