-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_test.go
104 lines (100 loc) · 2.04 KB
/
code_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package errorsx
import (
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestSetCode(t *testing.T) {
type args struct {
err error
code int
}
tests := []struct {
name string
args args
wantErr error
}{
{
name: "error nil",
args: args{
err: nil,
code: 200,
},
wantErr: errors.New("--200--"),
},
{
name: "error not nil",
args: args{
err: errors.New("haha:: hehe"),
code: 200,
},
wantErr: errors.New("--200-- haha:: hehe"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := SetCode(tt.args.err, tt.args.code)
assert.Equal(t, tt.wantErr.Error(), err.Error())
})
}
}
func TestGetCode(t *testing.T) {
type args struct {
err error
}
tests := []struct {
name string
args args
want int
}{
{
name: "error nil",
args: args{
err: nil,
},
want: http.StatusInternalServerError,
},
{
name: "error not contain code",
args: args{
err: errors.New("haha:: hehe"),
},
want: http.StatusInternalServerError,
},
{
name: "error contain invalid code",
args: args{
err: errors.New("--asdf-- haha:: hehe"),
},
want: http.StatusInternalServerError,
},
{
name: "error contain code",
args: args{
err: errors.New("--404-- haha:: hehe"),
},
want: 404,
},
{
name: "error contain multiple code",
args: args{
err: errors.New("--404-- haha:: --400-- hehe"),
},
want: 404,
},
{
name: "",
args: args{
err: errors.New("transporthttp.(*Quote).Checkout:: usecase.(*Quote).Checkout:: extapi.(*B2bcartGRPC).GetCartsToCheckout:: --400-- !!fsefs!! error get list cart checkout:: rpc error: code = NotFound desc = transportgrpc.(*Cart).GetCartsToCheckout:: usecase.(*Cart).GetCartByCustIDForCheckout:: repo.(*Cart).GetCartListByIDListAndDealerCodePreloadAll:: ~~error get cart~~:: record not found"),
},
want: 400,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GetCode(tt.args.err)
assert.Equal(t, tt.want, got)
})
}
}