Skip to content

Commit 419e09b

Browse files
authored
Update UT of ValidationError (#13)
1 parent 4afcbca commit 419e09b

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

validation_error_test.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ type testVldErr struct {
1818
*defaultAppError
1919
}
2020

21+
type test3rdPartyVldErr struct {
22+
errStr string
23+
}
24+
25+
func (e *test3rdPartyVldErr) Error() string { return e.errStr }
26+
2127
func (e *testVldErr) Build(lang Language, options ...InfoBuilderOption) *InfoBuilderResult {
2228
var message string
2329
buildCfg := e.BuildConfig(lang, options...)
@@ -38,22 +44,26 @@ func Test_ValidationError(t *testing.T) {
3844
initConfig(okConfig)
3945

4046
infoBuilder := func(e AppError, buildCfg *InfoBuilderConfig) *InfoBuilderResult {
41-
var message string
47+
vldErr := &test3rdPartyVldErr{}
48+
errors.As(e, &vldErr)
49+
message := vldErr.errStr
4250
if buildCfg.TranslationFunc != nil {
4351
message, _ = buildCfg.TranslationFunc(buildCfg.Language, e.Error(), nil)
4452
}
4553
return &InfoBuilderResult{
4654
ErrorInfo: &ErrorInfo{
4755
Status: http.StatusBadRequest,
48-
Code: "ErrValidationItem",
56+
Code: vldErr.errStr,
4957
Message: message,
5058
},
5159
}
5260
}
5361

5462
assert.Nil(t, NewValidationErrorWithInfoBuilder(nil))
5563
vldErr := NewValidationErrorWithInfoBuilder(infoBuilder,
56-
err3rdPartyVld1, err3rdPartyVld2, err3rdPartyVld3)
64+
&test3rdPartyVldErr{errStr: "3rdPartyVldErr1"},
65+
&test3rdPartyVldErr{errStr: "3rdPartyVldErr2"},
66+
&test3rdPartyVldErr{errStr: "3rdPartyVldErr3"})
5767

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

6575
inErr0 := errInfo.InnerErrors[0]
6676
assert.Equal(t, http.StatusBadRequest, inErr0.Status)
67-
assert.Equal(t, "ErrValidationItem", inErr0.Code)
68-
assert.Equal(t, "(ErrValidation1)-in-en", inErr0.Message)
77+
assert.Equal(t, "3rdPartyVldErr1", inErr0.Code)
78+
assert.Equal(t, "(3rdPartyVldErr1)-in-en", inErr0.Message)
6979

7080
inErr1 := errInfo.InnerErrors[1]
7181
assert.Equal(t, http.StatusBadRequest, inErr1.Status)
72-
assert.Equal(t, "ErrValidationItem", inErr1.Code)
73-
assert.Equal(t, "(ErrValidation2)-in-en", inErr1.Message)
82+
assert.Equal(t, "3rdPartyVldErr2", inErr1.Code)
83+
assert.Equal(t, "(3rdPartyVldErr2)-in-en", inErr1.Message)
84+
85+
inErr2 := errInfo.InnerErrors[2]
86+
assert.Equal(t, http.StatusBadRequest, inErr2.Status)
87+
assert.Equal(t, "3rdPartyVldErr3", inErr2.Code)
88+
assert.Equal(t, "(3rdPartyVldErr3)-in-en", inErr2.Message)
7489
})
7590

7691
t.Run("success: implement AppError interface", func(t *testing.T) {
@@ -99,5 +114,10 @@ func Test_ValidationError(t *testing.T) {
99114
assert.Equal(t, http.StatusBadRequest, inErr1.Status)
100115
assert.Equal(t, "ErrValidationItem", inErr1.Code)
101116
assert.Equal(t, "(ErrValidation2)-in-en", inErr1.Message)
117+
118+
inErr2 := errInfo.InnerErrors[2]
119+
assert.Equal(t, http.StatusBadRequest, inErr2.Status)
120+
assert.Equal(t, "ErrValidationItem", inErr2.Code)
121+
assert.Equal(t, "(ErrValidation3)-in-en", inErr2.Message)
102122
})
103123
}

0 commit comments

Comments
 (0)