Skip to content

Commit

Permalink
Update UT of ValidationError (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiendc authored Nov 18, 2024
1 parent 4afcbca commit 419e09b
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions validation_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ type testVldErr struct {
*defaultAppError
}

type test3rdPartyVldErr struct {
errStr string
}

func (e *test3rdPartyVldErr) Error() string { return e.errStr }

func (e *testVldErr) Build(lang Language, options ...InfoBuilderOption) *InfoBuilderResult {
var message string
buildCfg := e.BuildConfig(lang, options...)
Expand All @@ -38,22 +44,26 @@ func Test_ValidationError(t *testing.T) {
initConfig(okConfig)

infoBuilder := func(e AppError, buildCfg *InfoBuilderConfig) *InfoBuilderResult {
var message string
vldErr := &test3rdPartyVldErr{}
errors.As(e, &vldErr)
message := vldErr.errStr
if buildCfg.TranslationFunc != nil {
message, _ = buildCfg.TranslationFunc(buildCfg.Language, e.Error(), nil)
}
return &InfoBuilderResult{
ErrorInfo: &ErrorInfo{
Status: http.StatusBadRequest,
Code: "ErrValidationItem",
Code: vldErr.errStr,
Message: message,
},
}
}

assert.Nil(t, NewValidationErrorWithInfoBuilder(nil))
vldErr := NewValidationErrorWithInfoBuilder(infoBuilder,
err3rdPartyVld1, err3rdPartyVld2, err3rdPartyVld3)
&test3rdPartyVldErr{errStr: "3rdPartyVldErr1"},
&test3rdPartyVldErr{errStr: "3rdPartyVldErr2"},
&test3rdPartyVldErr{errStr: "3rdPartyVldErr3"})

result := vldErr.Build(LanguageEn)
errInfo := result.ErrorInfo
Expand All @@ -64,13 +74,18 @@ func Test_ValidationError(t *testing.T) {

inErr0 := errInfo.InnerErrors[0]
assert.Equal(t, http.StatusBadRequest, inErr0.Status)
assert.Equal(t, "ErrValidationItem", inErr0.Code)
assert.Equal(t, "(ErrValidation1)-in-en", inErr0.Message)
assert.Equal(t, "3rdPartyVldErr1", inErr0.Code)
assert.Equal(t, "(3rdPartyVldErr1)-in-en", inErr0.Message)

inErr1 := errInfo.InnerErrors[1]
assert.Equal(t, http.StatusBadRequest, inErr1.Status)
assert.Equal(t, "ErrValidationItem", inErr1.Code)
assert.Equal(t, "(ErrValidation2)-in-en", inErr1.Message)
assert.Equal(t, "3rdPartyVldErr2", inErr1.Code)
assert.Equal(t, "(3rdPartyVldErr2)-in-en", inErr1.Message)

inErr2 := errInfo.InnerErrors[2]
assert.Equal(t, http.StatusBadRequest, inErr2.Status)
assert.Equal(t, "3rdPartyVldErr3", inErr2.Code)
assert.Equal(t, "(3rdPartyVldErr3)-in-en", inErr2.Message)
})

t.Run("success: implement AppError interface", func(t *testing.T) {
Expand Down Expand Up @@ -99,5 +114,10 @@ func Test_ValidationError(t *testing.T) {
assert.Equal(t, http.StatusBadRequest, inErr1.Status)
assert.Equal(t, "ErrValidationItem", inErr1.Code)
assert.Equal(t, "(ErrValidation2)-in-en", inErr1.Message)

inErr2 := errInfo.InnerErrors[2]
assert.Equal(t, http.StatusBadRequest, inErr2.Status)
assert.Equal(t, "ErrValidationItem", inErr2.Code)
assert.Equal(t, "(ErrValidation3)-in-en", inErr2.Message)
})
}

0 comments on commit 419e09b

Please sign in to comment.