Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
222 lines (206 loc) · 5.77 KB
/
Copy patherrors_test.go
File metadata and controls
222 lines (206 loc) · 5.77 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package zvec
import (
"errors"
"fmt"
"testing"
)
func TestErrorCodeConstants(t *testing.T) {
tests := []struct {
name string
got ErrorCode
expected int
}{
{"OK", OK, 0},
{"NotFound", NotFound, 1},
{"AlreadyExists", AlreadyExists, 2},
{"InvalidArgument", InvalidArgument, 3},
{"PermissionDenied", PermissionDenied, 4},
{"FailedPrecondition", FailedPrecondition, 5},
{"ResourceExhausted", ResourceExhausted, 6},
{"Unavailable", Unavailable, 7},
{"InternalError", InternalError, 8},
{"NotSupported", NotSupported, 9},
{"Unknown", Unknown, 10},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if int(tt.got) != tt.expected {
t.Errorf("ErrorCode %s = %d, want %d", tt.name, tt.got, tt.expected)
}
})
}
}
func TestErrorCodeString(t *testing.T) {
tests := []struct {
code ErrorCode
}{
{OK},
{NotFound},
{AlreadyExists},
{InvalidArgument},
{InternalError},
{Unknown},
}
for _, tt := range tests {
s := tt.code.String()
if s == "" {
t.Errorf("ErrorCode(%d).String() returned empty string", tt.code)
}
}
}
func TestErrorMessage(t *testing.T) {
err := &Error{Code: NotFound, Message: "item not found"}
got := err.Error()
if got == "" {
t.Error("Error.Error() should not be empty")
}
expected := fmt.Sprintf("zvec error [%s]: item not found", NotFound)
if got != expected {
t.Errorf("Error.Error() = %q, want %q", got, expected)
}
}
func TestErrorMessageFormatting(t *testing.T) {
tests := []struct {
code ErrorCode
message string
}{
{OK, "success"},
{InternalError, "something broke"},
{Unknown, ""},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("code_%d", tt.code), func(t *testing.T) {
err := &Error{Code: tt.code, Message: tt.message}
want := fmt.Sprintf("zvec error [%s]: %s", tt.code, tt.message)
if err.Error() != want {
t.Errorf("Error.Error() = %q, want %q", err.Error(), want)
}
})
}
}
func TestErrorImplementsErrorInterface(t *testing.T) {
err := &Error{Code: NotFound, Message: "test"}
var asError error = err
if asError.Error() == "" {
t.Error("Error.Error() should not be empty")
}
}
func TestIsNotFound(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{"nil error", nil, false},
{"not found error", &Error{Code: NotFound, Message: "not found"}, true},
{"other zvec error", &Error{Code: InternalError, Message: "internal"}, false},
{"standard error", errors.New("some error"), false},
{"wrapped not found", fmt.Errorf("wrapped: %w", &Error{Code: NotFound, Message: "not found"}), true},
{"wrapped other", fmt.Errorf("wrapped: %w", &Error{Code: AlreadyExists, Message: "exists"}), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsNotFound(tt.err); got != tt.want {
t.Errorf("IsNotFound() = %v, want %v", got, tt.want)
}
})
}
}
func TestIsAlreadyExists(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{"nil error", nil, false},
{"already exists error", &Error{Code: AlreadyExists, Message: "exists"}, true},
{"not found error", &Error{Code: NotFound, Message: "not found"}, false},
{"standard error", errors.New("some error"), false},
{"wrapped already exists", fmt.Errorf("wrapped: %w", &Error{Code: AlreadyExists, Message: "exists"}), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsAlreadyExists(tt.err); got != tt.want {
t.Errorf("IsAlreadyExists() = %v, want %v", got, tt.want)
}
})
}
}
func TestIsInvalidArgument(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{"nil error", nil, false},
{"invalid argument error", &Error{Code: InvalidArgument, Message: "bad arg"}, true},
{"not found error", &Error{Code: NotFound, Message: "not found"}, false},
{"standard error", errors.New("some error"), false},
{"wrapped invalid argument", fmt.Errorf("wrapped: %w", &Error{Code: InvalidArgument, Message: "bad"}), true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsInvalidArgument(tt.err); got != tt.want {
t.Errorf("IsInvalidArgument() = %v, want %v", got, tt.want)
}
})
}
}
func TestErrorsAs(t *testing.T) {
originalErr := &Error{Code: NotFound, Message: "resource not found"}
wrappedErr := fmt.Errorf("operation failed: %w", originalErr)
var zvecErr *Error
if !errors.As(wrappedErr, &zvecErr) {
t.Fatal("errors.As should find *Error in wrapped error")
}
if zvecErr.Code != NotFound {
t.Errorf("extracted error code = %d, want %d", zvecErr.Code, NotFound)
}
if zvecErr.Message != "resource not found" {
t.Errorf("extracted error message = %q, want %q", zvecErr.Message, "resource not found")
}
}
func TestSentinelErrors(t *testing.T) {
sentinels := []*Error{
ErrNotFound,
ErrAlreadyExists,
ErrInvalidArgument,
ErrPermissionDenied,
ErrFailedPrecondition,
ErrResourceExhausted,
ErrUnavailable,
ErrInternalError,
ErrNotSupported,
ErrUnknown,
}
expectedCodes := []ErrorCode{
NotFound, AlreadyExists, InvalidArgument, PermissionDenied,
FailedPrecondition, ResourceExhausted, Unavailable,
InternalError, NotSupported, Unknown,
}
for i, sentinel := range sentinels {
if sentinel.Code != expectedCodes[i] {
t.Errorf("sentinel[%d].Code = %d, want %d", i, sentinel.Code, expectedCodes[i])
}
if sentinel.Message == "" {
t.Errorf("sentinel[%d].Message should not be empty", i)
}
}
}
func BenchmarkErrorCreation(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = &Error{Code: NotFound, Message: "not found"}
}
}
func BenchmarkIsNotFound(b *testing.B) {
err := &Error{Code: NotFound, Message: "not found"}
for i := 0; i < b.N; i++ {
IsNotFound(err)
}
}
func BenchmarkErrorMessage(b *testing.B) {
err := &Error{Code: NotFound, Message: "not found"}
for i := 0; i < b.N; i++ {
_ = err.Error()
}
}