Skip to content

Commit

Permalink
Add json.Marshal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eugeneradionov committed Sep 19, 2021
1 parent a0b8565 commit 85db25b
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 17 deletions.
81 changes: 75 additions & 6 deletions xerror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package xerrors

import (
"encoding/json"
"reflect"
"testing"
)
Expand Down Expand Up @@ -219,10 +220,10 @@ func TestXErr_Error(t *testing.T) {
xErr: &XErr{
Message: "test message",
Description: "test description",
Extra: map[string]interface{}{"filed": "user", "user_id": 123},
Extra: map[string]interface{}{"field": "user", "user_id": 123},
InternalExtra: map[string]interface{}{"error_info": "connect to db"},
},
want: "test message: test description; map[filed:user user_id:123]",
want: "test message: test description; map[field:user user_id:123]",
},
}

Expand Down Expand Up @@ -267,14 +268,14 @@ func TestNew(t *testing.T) {
msg: "some error",
opts: []XErrOpt{
WithDescription("error description"),
WithExtra(map[string]interface{}{"filed": "user", "user_id": 123}),
WithExtra(map[string]interface{}{"field": "user", "user_id": 123}),
WithInternalExtra(map[string]interface{}{"error_info": "connect to db"}),
},
},
want: &XErr{
Message: "some error",
Description: "error description",
Extra: map[string]interface{}{"filed": "user", "user_id": 123},
Extra: map[string]interface{}{"field": "user", "user_id": 123},
InternalExtra: map[string]interface{}{"error_info": "connect to db"},
},
},
Expand All @@ -284,7 +285,7 @@ func TestNew(t *testing.T) {
msg: "some error",
opts: []XErrOpt{
WithDescription("error description"),
WithExtra(map[string]interface{}{"filed": "user", "user_id": 123}),
WithExtra(map[string]interface{}{"field": "user", "user_id": 123}),
WithInternalExtra(map[string]interface{}{"error_info": "connect to db"}),
WithMessage("new message"),
WithDescription("new description"),
Expand All @@ -293,7 +294,7 @@ func TestNew(t *testing.T) {
want: &XErr{
Message: "new message",
Description: "new description",
Extra: map[string]interface{}{"filed": "user", "user_id": 123},
Extra: map[string]interface{}{"field": "user", "user_id": 123},
InternalExtra: map[string]interface{}{"error_info": "connect to db"},
},
},
Expand All @@ -311,3 +312,71 @@ func TestNew(t *testing.T) {
})
}
}

func TestXErr_MarshalJSON(t *testing.T) {
t.Parallel()

type args struct {
msg string
opts []XErrOpt
}

tests := []struct {
name string
args args
want string
}{
{
name: "new error without options",
args: args{
msg: "some error",
},
want: `{"message":"some error"}`,
},
{
name: "new error with options",
args: args{
msg: "some error",
opts: []XErrOpt{
WithDescription("error description"),
WithExtra(map[string]interface{}{"field": "user", "user_id": 123}),
WithInternalExtra(map[string]interface{}{"error_info": "connect to db"}),
},
},
want: `{"message":"some error","description":"error description","extra":{"field":"user","user_id":123}}`,
},
{
name: "new error with options overwrite",
args: args{
msg: "some error",
opts: []XErrOpt{
WithDescription("error description"),
WithExtra(map[string]interface{}{"field": "user", "user_id": 123}),
WithInternalExtra(map[string]interface{}{"error_info": "connect to db"}),
WithMessage("new message"),
WithDescription("new description"),
},
},
want: `{"message":"new message","description":"new description","extra":{"field":"user","user_id":123}}`,
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

xErr := New(tt.args.msg, tt.args.opts...)

got, err := json.Marshal(xErr)
if err != nil {
t.Errorf("json.Marshal() error: %v", err)
}

if string(got) != tt.want {
t.Errorf("json.Marshal() = %v, want %v", string(got), tt.want)
}
})
}
}
89 changes: 78 additions & 11 deletions xerrors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package xerrors

import (
"encoding/json"
"reflect"
"testing"
)
Expand Down Expand Up @@ -186,13 +187,13 @@ func TestXErrs_Error(t *testing.T) {
args: args{
xerrs: &XErrs{Errs: []XError{
NewXErr("test msg", "test descr", nil, nil),
NewXErr("test msg 2", "test descr 2", map[string]interface{}{"filed": "user_id"}, nil),
NewXErr("test msg 2", "test descr 2", map[string]interface{}{"field": "user_id"}, nil),
NewXErr("test msg 3", "test descr 3",
map[string]interface{}{"filed": "user_id"}, map[string]interface{}{"error_info": "some error"}),
map[string]interface{}{"field": "user_id"}, map[string]interface{}{"error_info": "some error"}),
}},
},
// nolint:lll
want: "test msg: test descr; map[];test msg 2: test descr 2; map[filed:user_id];test msg 3: test descr 3; map[filed:user_id]",
want: "test msg: test descr; map[];test msg 2: test descr 2; map[field:user_id];test msg 3: test descr 3; map[field:user_id]",
},
}

Expand Down Expand Up @@ -248,16 +249,16 @@ func TestXErrs_GetErrors(t *testing.T) {
args: args{
xerrs: &XErrs{Errs: []XError{
NewXErr("test msg", "test descr", nil, nil),
NewXErr("test msg 2", "test descr 2", map[string]interface{}{"filed": "user_id"}, nil),
NewXErr("test msg 2", "test descr 2", map[string]interface{}{"field": "user_id"}, nil),
NewXErr("test msg 3", "test descr 3",
map[string]interface{}{"filed": "user_id"}, map[string]interface{}{"error_info": "some error"}),
map[string]interface{}{"field": "user_id"}, map[string]interface{}{"error_info": "some error"}),
}},
},
want: []XError{
NewXErr("test msg", "test descr", nil, nil),
NewXErr("test msg 2", "test descr 2", map[string]interface{}{"filed": "user_id"}, nil),
NewXErr("test msg 2", "test descr 2", map[string]interface{}{"field": "user_id"}, nil),
NewXErr("test msg 3", "test descr 3",
map[string]interface{}{"filed": "user_id"}, map[string]interface{}{"error_info": "some error"}),
map[string]interface{}{"field": "user_id"}, map[string]interface{}{"error_info": "some error"}),
},
},
}
Expand Down Expand Up @@ -314,9 +315,9 @@ func TestXErrs_Len(t *testing.T) {
args: args{
xerrs: &XErrs{Errs: []XError{
NewXErr("test msg", "test descr", nil, nil),
NewXErr("test msg 2", "test descr 2", map[string]interface{}{"filed": "user_id"}, nil),
NewXErr("test msg 2", "test descr 2", map[string]interface{}{"field": "user_id"}, nil),
NewXErr("test msg 3", "test descr 3",
map[string]interface{}{"filed": "user_id"}, map[string]interface{}{"error_info": "some error"}),
map[string]interface{}{"field": "user_id"}, map[string]interface{}{"error_info": "some error"}),
}},
},
want: 3,
Expand Down Expand Up @@ -371,9 +372,9 @@ func TestXErrs_Sanitize(t *testing.T) {
args: args{
xerrs: &XErrs{Errs: []XError{
NewXErr("test msg", "test descr", nil, nil),
NewXErr("test msg 2", "test descr 2", map[string]interface{}{"filed": "user_id"}, nil),
NewXErr("test msg 2", "test descr 2", map[string]interface{}{"field": "user_id"}, nil),
NewXErr("test msg 3", "test descr 3",
map[string]interface{}{"filed": "user_id"}, map[string]interface{}{"error_info": "some error"}),
map[string]interface{}{"field": "user_id"}, map[string]interface{}{"error_info": "some error"}),
}},
},
},
Expand All @@ -395,3 +396,69 @@ func TestXErrs_Sanitize(t *testing.T) {
})
}
}

func TestXErrs_MarshalJSON(t *testing.T) {
t.Parallel()

type args struct {
xerrs *XErrs
}

tests := []struct {
name string
args args
want string
}{
{
name: "nil XErrs",
args: args{
xerrs: nil,
},
want: "null",
},
{
name: "no errors",
args: args{
xerrs: &XErrs{},
},
want: `{"errors":null}`,
},
{
name: "1 error",
args: args{
xerrs: &XErrs{Errs: []XError{NewXErr("test msg", "test descr", nil, nil)}},
},
want: `{"errors":[{"message":"test msg","description":"test descr"}]}`,
},
{
name: "multiple errors",
args: args{
xerrs: &XErrs{Errs: []XError{
NewXErr("test msg", "test descr", nil, nil),
NewXErr("test msg 2", "test descr 2", map[string]interface{}{"field": "user_id"}, nil),
NewXErr("test msg 3", "test descr 3",
map[string]interface{}{"field": "user_id"}, map[string]interface{}{"error_info": "some error"}),
}},
},
// nolint:lll
want: `{"errors":[{"message":"test msg","description":"test descr"},{"message":"test msg 2","description":"test descr 2","extra":{"field":"user_id"}},{"message":"test msg 3","description":"test descr 3","extra":{"field":"user_id"}}]}`,
},
}

for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

got, err := json.Marshal(tt.args.xerrs)
if err != nil {
t.Errorf("json.Marshal() error: %v", err)
}

if string(got) != tt.want {
t.Errorf("json.Marshal() = %v, want %v", string(got), tt.want)
}
})
}
}

0 comments on commit 85db25b

Please sign in to comment.