From 71a031b518598f28f84f71d5dc1343ddf7200211 Mon Sep 17 00:00:00 2001 From: EchoUtopia Date: Fri, 23 Apr 2021 17:34:41 +0800 Subject: [PATCH] add some unit test --- basic_def.go | 35 ++++++++++++++++++++--------------- error_test.go | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 17 deletions(-) diff --git a/basic_def.go b/basic_def.go index 0c304de..2ebd167 100644 --- a/basic_def.go +++ b/basic_def.go @@ -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{ @@ -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`, @@ -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 } diff --git a/error_test.go b/error_test.go index d484943..dfdb6d3 100644 --- a/error_test.go +++ b/error_test.go @@ -4,8 +4,10 @@ import ( "encoding/json" "errors" "fmt" - "github.com/stretchr/testify/require" + "reflect" "testing" + + "github.com/stretchr/testify/require" ) type TestErr struct { @@ -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) } @@ -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) +}