status_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package ecode
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/golang/protobuf/ptypes/timestamp"
  6. "github.com/smartystreets/goconvey/convey"
  7. "github.com/stretchr/testify/assert"
  8. "go-common/library/ecode/internal/types"
  9. )
  10. func TestEqual(t *testing.T) {
  11. convey.Convey("Equal", t, func(ctx convey.C) {
  12. ctx.Convey("When err1=Error(RequestErr, 'test') and err2=Errorf(RequestErr, 'test')", func(ctx convey.C) {
  13. err1 := Error(RequestErr, "test")
  14. err2 := Errorf(RequestErr, "test")
  15. ctx.Convey("Then err1=err2, err1 != nil", func(ctx convey.C) {
  16. ctx.So(err1, convey.ShouldResemble, err2)
  17. ctx.So(err1, convey.ShouldNotBeNil)
  18. })
  19. })
  20. })
  21. // assert.True(t, OK.Equal(nil))
  22. // assert.True(t, err1.Equal(err2))
  23. // assert.False(t, err1.Equal(nil))
  24. // assert.True(t, Equal(nil, nil))
  25. }
  26. func TestDetail(t *testing.T) {
  27. m := &timestamp.Timestamp{Seconds: time.Now().Unix()}
  28. st, _ := Error(RequestErr, "RequestErr").WithDetails(m)
  29. assert.Equal(t, "RequestErr", st.Message())
  30. assert.Equal(t, int(RequestErr), st.Code())
  31. assert.IsType(t, m, st.Details()[0])
  32. }
  33. func TestFromCode(t *testing.T) {
  34. err := FromCode(RequestErr)
  35. assert.Equal(t, int(RequestErr), err.Code())
  36. assert.Equal(t, "-400", err.Message())
  37. }
  38. func TestFromProto(t *testing.T) {
  39. msg := &types.Status{Code: 2233, Message: "error"}
  40. err := FromProto(msg)
  41. assert.Equal(t, 2233, err.Code())
  42. assert.Equal(t, "error", err.Message())
  43. m := &timestamp.Timestamp{Seconds: time.Now().Unix()}
  44. err = FromProto(m)
  45. assert.Equal(t, -500, err.Code())
  46. assert.Contains(t, err.Message(), "invalid proto message get")
  47. }
  48. func TestEmpty(t *testing.T) {
  49. st := &Status{}
  50. assert.Len(t, st.Details(), 0)
  51. st = nil
  52. assert.Len(t, st.Details(), 0)
  53. }