Skip to content

Commit

Permalink
add some unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoUtopia committed Apr 23, 2021
1 parent fb4d82d commit 71a031b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
35 changes: 20 additions & 15 deletions basic_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package zerror
type defMapT map[string]*Def

var (
defMap = defMapT{}
defMap = defMapT{}
)

func init() {
defMap.init()
}

const (
CodeInternal = `zerror:internal`
codeBadRequest = `zerror:bad_request`
codeForbidden = `zerror:forbidden`
codeNofFound = `zerror:not_found`
CodeInternal = `zerror:internal`
codeBadRequest = `zerror:bad_request`
codeForbidden = `zerror:forbidden`
codeNofFound = `zerror:not_found`
codeUnauthenticated = `zerror:unauthenticated`
codeAlreadyExists = `zerror:already_exists`
codeAlreadyExists = `zerror:already_exists`
)

var Internal = &Def{
Expand All @@ -29,14 +34,14 @@ var BadRequest = &Def{
}

var Forbidden = &Def{
Code: codeForbidden ,
Code: codeForbidden,
Status: StatusPermissionDenied,
Msg: `forbidden`,
Description: `you are forbidden to access`,
}

var NotFound = &Def{
Code: codeNofFound ,
Code: codeNofFound,
Msg: `not found`,
Status: StatusNotFound,
Description: `resource not found`,
Expand All @@ -50,20 +55,20 @@ var Unauthenticated = &Def{
}

var AlreadyExists = &Def{
Code: codeAlreadyExists,
Code: codeAlreadyExists,
Msg: `already exists`,
Status: StatusAlreadyExists,
Description: `already exists`,
}

func (m *defMapT)init(){
func (m *defMapT) init() {
inited := defMapT{
CodeInternal: Internal,
codeBadRequest: BadRequest,
codeForbidden: Forbidden,
codeNofFound: NotFound,
CodeInternal: Internal,
codeBadRequest: BadRequest,
codeForbidden: Forbidden,
codeNofFound: NotFound,
codeUnauthenticated: Unauthenticated,
codeAlreadyExists:AlreadyExists,
codeAlreadyExists: AlreadyExists,
}
*m = inited
}
32 changes: 30 additions & 2 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/stretchr/testify/require"
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

type TestErr struct {
Expand Down Expand Up @@ -51,7 +53,7 @@ func TestGenerateCode(t *testing.T) {
data1.Err.Code = ``
initErrGroup(data1)
require.Equal(t, `custom-prefix:err`, data1.Err.Code)
require.Equal(t, Status(200), data1.Err.Status)
require.Equal(t, Status(500), data1.Err.Status)

}

Expand Down Expand Up @@ -164,3 +166,29 @@ func BenchmarkDef_Wrap(b *testing.B) {
def.wrapf(originalError, 1, ``)
}
}

func TestData(t *testing.T) {
zerr := Internal.New().
WithData(map[string]interface{}{
`a1`: `a2`,
}).
WithData(map[string]interface{}{
`b1`: `b2`,
}).
WithKVs(`c1`, `c2`, `d1`, `d2`)
expected := Data{
`a1`: `a2`,
`b1`: `b2`,
`c1`: `c2`,
`d1`: `d2`,
}
if !reflect.DeepEqual(zerr.Data, expected) {
t.Fatal()
}
}

func TestFromCode(t *testing.T) {
zerr, ok := FromCode(BadRequest.Code)
require.Equal(t, true, ok)
require.Equal(t, zerr.Code, BadRequest.Code)
}

0 comments on commit 71a031b

Please sign in to comment.