test: add test for error.go

This commit is contained in:
dapeng 2024-06-27 09:12:05 +08:00
parent 2d0c5a2048
commit 5977e64e90
2 changed files with 74 additions and 18 deletions

View File

@ -32,19 +32,23 @@ func GetGoneDefaultId(goner Goner) GonerId {
return GonerId(fmt.Sprintf("%s#%d", pkgName, reflect.ValueOf(goner).Elem().UnsafeAddr()))
}
func (c *cemetery) bury(goner Goner, options ...GonerOption) Tomb {
theTomb := NewTomb(goner)
func buildTomb(goner Goner, options ...GonerOption) Tomb {
newTomb := NewTomb(goner)
for _, option := range options {
switch option.(type) {
case GonerId:
theTomb.SetId(option.(GonerId))
newTomb.SetId(option.(GonerId))
case defaultType:
theTomb.SetDefault(option.(defaultType).t)
newTomb.SetDefault(option.(defaultType).t)
case Order:
theTomb.SetOrder(option.(Order))
newTomb.SetOrder(option.(Order))
}
}
return newTomb
}
func (c *cemetery) bury(goner Goner, options ...GonerOption) Tomb {
theTomb := buildTomb(goner, options...)
if theTomb.GetId() == "" {
theTomb.SetId(GetGoneDefaultId(goner))
@ -90,18 +94,7 @@ func (c *cemetery) BuryOnce(goner Goner, options ...GonerOption) Cemetery {
}
func (c *cemetery) ReplaceBury(goner Goner, options ...GonerOption) (err error) {
newTomb := NewTomb(goner)
for _, option := range options {
switch option.(type) {
case GonerId:
newTomb.SetId(option.(GonerId))
case defaultType:
newTomb.SetDefault(option.(defaultType).t)
case Order:
newTomb.SetOrder(option.(Order))
}
}
newTomb := buildTomb(goner, options...)
if newTomb.GetId() == "" {
err = ReplaceBuryIdParamEmptyError()
return

View File

@ -166,3 +166,66 @@ func TestToError(t *testing.T) {
assert.Equal(t, "100", toError.Msg())
})
}
func TestBError_GetStatusCode(t *testing.T) {
type fields struct {
err Error
data any
}
tests := []struct {
name string
fields fields
want int
}{
{
name: "default",
fields: fields{
err: NewBusinessError("error", 100),
data: nil,
},
want: http.StatusOK,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &BError{
err: tt.fields.err,
data: tt.fields.data,
}
assert.Equalf(t, tt.want, e.GetStatusCode(), "GetStatusCode()")
})
}
}
func Test_defaultErr_GetStatusCode(t *testing.T) {
type fields struct {
code int
msg string
statusCode int
}
tests := []struct {
name string
fields fields
want int
}{
{
name: "default",
fields: fields{
code: 500,
msg: "error",
statusCode: http.StatusOK,
},
want: http.StatusOK,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &defaultErr{
code: tt.fields.code,
msg: tt.fields.msg,
statusCode: tt.fields.statusCode,
}
assert.Equalf(t, tt.want, e.GetStatusCode(), "GetStatusCode()")
})
}
}