-
Notifications
You must be signed in to change notification settings - Fork 0
/
status_test.go
53 lines (41 loc) · 1.23 KB
/
status_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
package metacode
import (
"testing"
"time"
"github.com/aluka-7/metacode/types"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/stretchr/testify/assert"
)
func TestEqual(t *testing.T) {
err1, err2 := Error(RequestErr, "test"), Errorf(RequestErr, "test")
assert.Equal(t, err1, err2)
assert.True(t, Equal(nil, nil))
}
func TestDetail(t *testing.T) {
m := ×tamp.Timestamp{Seconds: time.Now().Unix()}
st, _ := Error(RequestErr, "RequestErr").WithDetails(m)
assert.Equal(t, "RequestErr", st.Message(""))
assert.Equal(t, int(RequestErr), st.Code())
assert.IsType(t, m, st.Details()[0])
}
func TestFromCode(t *testing.T) {
err := FromCode(RequestErr)
assert.Equal(t, int(RequestErr), err.Code())
assert.Equal(t, "-400", err.Message(""))
}
func TestFromProto(t *testing.T) {
msg := &types.Status{Code: 2233, Message: "error"}
err := FromProto(msg)
assert.Equal(t, 2233, err.Code())
assert.Equal(t, "error", err.Message(""))
m := ×tamp.Timestamp{Seconds: time.Now().Unix()}
err = FromProto(m)
assert.Equal(t, -500, err.Code())
assert.Contains(t, err.Message(""), "invalid proto message get")
}
func TestEmpty(t *testing.T) {
st := &Status{}
assert.Len(t, st.Details(), 0)
st = nil
assert.Len(t, st.Details(), 0)
}