-
Notifications
You must be signed in to change notification settings - Fork 1
/
ez_test.go
99 lines (75 loc) · 2.26 KB
/
ez_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
package ez
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
const op = "TestNew"
err := New(op, ECONFLICT, "An error message", nil)
assert.NotNil(t, err)
assert.Equal(t, err.Code, "conflict")
assert.Equal(t, err.Message, "An error message")
assert.Equal(t, err.Op, op)
assert.Equal(t, err.Err, nil)
}
func TestWrap(t *testing.T) {
const op = "TestNew"
wrappedErr := New(op, ECONFLICT, "An error message", nil)
const newOp = "TestWrap"
err := Wrap(newOp, wrappedErr)
assert.NotNil(t, err)
assert.Equal(t, err.Code, "conflict")
assert.Equal(t, err.Message, "An error message")
assert.Equal(t, err.Op, newOp)
assert.Equal(t, err.Err, wrappedErr)
}
func TestError(t *testing.T) {
const op = "TestError"
err := New(op, EINTERNAL, "An internal error", nil)
msg := err.Error()
assert.NotNil(t, err)
assert.Equal(t, msg, "TestError: <internal> An internal error")
}
func TestErrorCode(t *testing.T) {
const op = "TestErrorCode"
err := New(op, EINVALID, "An invalid error", nil)
code := ErrorCode(err)
assert.NotNil(t, err)
assert.Equal(t, code, "invalid")
}
func TestErrorMessage(t *testing.T) {
const op = "TestErrorMessage"
err := New(op, ENOTFOUND, "A not found error", nil)
msg := ErrorMessage(err)
assert.NotNil(t, err)
assert.Equal(t, msg, "A not found error")
}
func TestErrorStacktrace(t *testing.T) {
err0 := errors.New("A plain error message")
err1 := New("Depth 1", EINTERNAL, "", err0)
err2 := New("Depth 2", EINVALID, "Not so original error", err1)
err3 := Wrap("Depth 3", err2)
ErrorStacktrace(err3)
}
func TestAddDataToNilData(t *testing.T) {
err := New("TestOp", ECONFLICT, "message", nil)
err.AddData("key1", "value1")
expected := map[string]interface{}{"key1": "value1"}
assert.Equal(t, expected, err.Data)
}
func TestAddDataToExistingData(t *testing.T) {
err := &Error{Data: map[string]interface{}{"existing": "data"}}
err.AddData("key2", 42)
expected := map[string]interface{}{
"existing": "data",
"key2": 42,
}
assert.Equal(t, expected, err.Data)
}
func TestAddDataOverrideExisting(t *testing.T) {
err := &Error{Data: map[string]interface{}{"key": "old"}}
err.AddData("key", "new")
expected := map[string]interface{}{"key": "new"}
assert.Equal(t, expected, err.Data)
}