error_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package valid
  2. import (
  3. "errors"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestNewInternalError(t *testing.T) {
  8. err := NewInternalError(errors.New("abc"))
  9. if assert.NotNil(t, err.InternalError()) {
  10. assert.Equal(t, "abc", err.InternalError().Error())
  11. }
  12. }
  13. func TestErrors_Error(t *testing.T) {
  14. errs := Errors{
  15. "B": errors.New("B1"),
  16. "C": errors.New("C1"),
  17. "A": errors.New("A1"),
  18. }
  19. assert.Equal(t, "A: A1; B: B1; C: C1.", errs.Error())
  20. errs = Errors{
  21. "B": errors.New("B1"),
  22. }
  23. assert.Equal(t, "B: B1.", errs.Error())
  24. errs = Errors{}
  25. assert.Equal(t, "", errs.Error())
  26. }
  27. func TestErrors_MarshalMessage(t *testing.T) {
  28. errs := Errors{
  29. "A": errors.New("A1"),
  30. "B": Errors{
  31. "2": errors.New("B1"),
  32. },
  33. }
  34. errsJSON, err := errs.MarshalJSON()
  35. assert.Nil(t, err)
  36. assert.Equal(t, "{\"A\":\"A1\",\"B\":{\"2\":\"B1\"}}", string(errsJSON))
  37. }
  38. func TestErrors_Filter(t *testing.T) {
  39. errs := Errors{
  40. "B": errors.New("B1"),
  41. "C": nil,
  42. "A": errors.New("A1"),
  43. }
  44. err := errs.Filter()
  45. assert.Equal(t, 2, len(errs))
  46. if assert.NotNil(t, err) {
  47. assert.Equal(t, "A: A1; B: B1.", err.Error())
  48. }
  49. errs = Errors{}
  50. assert.Nil(t, errs.Filter())
  51. errs = Errors{
  52. "B": nil,
  53. "C": nil,
  54. }
  55. assert.Nil(t, errs.Filter())
  56. }
  57. func TestErrorObject_SetCode(t *testing.T) {
  58. err := NewError("A", "msg").(ErrorObject)
  59. assert.Equal(t, err.code, "A")
  60. assert.Equal(t, err.Code(), "A")
  61. err = err.SetCode("B").(ErrorObject)
  62. assert.Equal(t, "B", err.code)
  63. }
  64. func TestErrorObject_Code(t *testing.T) {
  65. err := NewError("A", "msg").(ErrorObject)
  66. assert.Equal(t, err.Code(), "A")
  67. }
  68. func TestErrorObject_SetMessage(t *testing.T) {
  69. err := NewError("code", "A").(ErrorObject)
  70. assert.Equal(t, err.message, "A")
  71. assert.Equal(t, err.Message(), "A")
  72. err = err.SetMessage("abc").(ErrorObject)
  73. assert.Equal(t, err.message, "abc")
  74. assert.Equal(t, err.Message(), "abc")
  75. }
  76. func TestErrorObject_Message(t *testing.T) {
  77. err := NewError("code", "A").(ErrorObject)
  78. assert.Equal(t, err.message, "A")
  79. assert.Equal(t, err.Message(), "A")
  80. }
  81. func TestErrorObject_Params(t *testing.T) {
  82. p := map[string]interface{}{"A": "val1", "AA": "val2"}
  83. err := NewError("code", "A").(ErrorObject)
  84. err = err.SetParams(p).(ErrorObject)
  85. err = err.SetMessage("B").(ErrorObject)
  86. assert.Equal(t, err.params, p)
  87. assert.Equal(t, err.Params(), p)
  88. }
  89. func TestErrorObject_AddParam(t *testing.T) {
  90. t.Run("case1", func(t *testing.T) {
  91. p := map[string]interface{}{"A": "val1", "B": "val2"}
  92. err := NewError("code", "A").(ErrorObject)
  93. err = err.SetParams(p).(ErrorObject)
  94. err = err.AddParam("C", "val3").(ErrorObject)
  95. p["C"] = "val3"
  96. assert.Equal(t, err.params, p)
  97. assert.Equal(t, err.Params(), p)
  98. })
  99. t.Run("case2", func(t *testing.T) {
  100. p := map[string]interface{}{"key": "val"}
  101. err := NewError("code", "A").(ErrorObject)
  102. err = err.AddParam("key", "val").(ErrorObject)
  103. assert.Equal(t, err.params, p)
  104. assert.Equal(t, err.Params(), p)
  105. })
  106. }
  107. func TestError_Code(t *testing.T) {
  108. err := NewError("A", "msg")
  109. assert.Equal(t, err.Code(), "A")
  110. }
  111. func TestError_SetMessage(t *testing.T) {
  112. err := NewError("code", "A")
  113. assert.Equal(t, err.Message(), "A")
  114. err = err.SetMessage("abc")
  115. assert.Equal(t, err.Message(), "abc")
  116. }
  117. func TestError_Message(t *testing.T) {
  118. err := NewError("code", "A")
  119. assert.Equal(t, err.Message(), "A")
  120. }
  121. func TestError_Params(t *testing.T) {
  122. p := map[string]interface{}{"A": "val1", "AA": "val2"}
  123. err := NewError("code", "A")
  124. err = err.SetParams(p)
  125. err = err.SetMessage("B")
  126. assert.Equal(t, err.Params(), p)
  127. }
  128. func TestValidationError(t *testing.T) {
  129. params := map[string]interface{}{
  130. "A": "B",
  131. }
  132. err := NewError("code", "msg")
  133. err = err.SetParams(params)
  134. assert.Equal(t, err.Code(), "code")
  135. assert.Equal(t, err.Message(), "msg")
  136. assert.Equal(t, err.Params(), params)
  137. params = map[string]interface{}{"min": 1}
  138. err = err.SetParams(params)
  139. assert.Equal(t, err.Params(), params)
  140. }