chat_back_go/internal/domain/validate_test.go

59 lines
1.5 KiB
Go

package domain
import (
"github.com/go-playground/validator/v10"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func Test_dateOfBirthValidation(t *testing.T) {
testCases := []struct {
name string
date struct {
Date CustomDate `validate:"date_of_birth"`
}
expectedErrText string
}{
{
name: "ok",
date: struct {
Date CustomDate `validate:"date_of_birth"`
}{Date: CustomDate{Time: time.Now().AddDate(-18, 0, 0)}},
},
{
name: "exactly_on_boundary",
date: struct {
Date CustomDate `validate:"date_of_birth"`
}{Date: CustomDate{Time: time.Date(1924, time.January, 1, 0, 0, 0, 0, time.UTC)}},
},
{
name: "too_young",
date: struct {
Date CustomDate `validate:"date_of_birth"`
}{Date: CustomDate{Time: time.Now().AddDate(-15, 11, 30)}},
expectedErrText: "Key: 'Date' Error:Field validation for 'Date' failed on the 'date_of_birth' tag",
},
{
name: "too_old",
date: struct {
Date CustomDate `validate:"date_of_birth"`
}{Date: CustomDate{Time: time.Date(1923, time.December, 31, 23, 59, 59, 0, time.UTC)}},
expectedErrText: "Key: 'Date' Error:Field validation for 'Date' failed on the 'date_of_birth' tag",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
validate := validator.New()
_ = validate.RegisterValidation("date_of_birth", dateOfBirthValidation)
err := validate.Struct(tc.date)
if tc.expectedErrText != "" {
assert.EqualError(t, err, tc.expectedErrText)
} else {
assert.NoError(t, err)
}
})
}
}