-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherror_test.go
More file actions
207 lines (171 loc) · 5.39 KB
/
Copy patherror_test.go
File metadata and controls
207 lines (171 loc) · 5.39 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
package validator
import (
"encoding/json"
"errors"
"testing"
)
func TestErrorsError(t *testing.T) {
// Test empty errors
var errs Errors
if errs.Error() != "" {
t.Errorf("Expected empty string for empty errors, got %s", errs.Error())
}
// Test single error
errs = Errors{errors.New("single error")}
if errs.Error() != "single error" {
t.Errorf("Expected 'single error', got %s", errs.Error())
}
// Test multiple errors
errs = Errors{
errors.New("first error"),
errors.New("second error"),
errors.New("third error"),
}
expected := "first error\nsecond error\nthird error"
if errs.Error() != expected {
t.Errorf("Expected '%s', got '%s'", expected, errs.Error())
}
}
func TestErrorsErrors(t *testing.T) {
err1 := errors.New("error 1")
err2 := errors.New("error 2")
errs := Errors{err1, err2}
result := errs.Errors()
if len(result) != 2 {
t.Errorf("Expected 2 errors, got %d", len(result))
}
if result[0] != err1 || result[1] != err2 {
t.Error("Errors() did not return the original errors")
}
}
func TestErrorsFieldErrors(t *testing.T) {
fieldErr := &FieldError{Name: "email", Message: "invalid email"}
genericErr := errors.New("generic error")
errs := Errors{fieldErr, genericErr}
fieldErrors := errs.FieldErrors()
if len(fieldErrors) != 2 {
t.Errorf("Expected 2 field errors, got %d", len(fieldErrors))
}
if fieldErrors[0].Name != "email" {
t.Error("First field error should be the original FieldError")
}
if fieldErrors[1].Message != "generic error" {
t.Error("Second field error should be converted from generic error")
}
}
func TestErrorsHasFieldError(t *testing.T) {
fieldErr := &FieldError{Name: "email", Message: "invalid email"}
errs := Errors{fieldErr}
if !errs.HasFieldError("email") {
t.Error("Expected HasFieldError to return true for existing field")
}
if errs.HasFieldError("name") {
t.Error("Expected HasFieldError to return false for non-existing field")
}
}
func TestErrorsGetFieldError(t *testing.T) {
fieldErr := &FieldError{Name: "email", Message: "invalid email"}
errs := Errors{fieldErr}
result := errs.GetFieldError("email")
if result == nil {
t.Error("Expected GetFieldError to return the field error")
} else if result.Name != "email" {
t.Error("Expected field error name to be 'email'")
}
result = errs.GetFieldError("name")
if result != nil {
t.Error("Expected GetFieldError to return nil for non-existing field")
}
}
func TestErrorsGroupByField(t *testing.T) {
fieldErr1 := &FieldError{Name: "email", Message: "required"}
fieldErr2 := &FieldError{Name: "email", Message: "invalid format"}
fieldErr3 := &FieldError{Name: "name", Message: "required"}
errs := Errors{fieldErr1, fieldErr2, fieldErr3}
groups := errs.GroupByField()
if len(groups) != 2 {
t.Errorf("Expected 2 groups, got %d", len(groups))
}
if len(groups["email"]) != 2 {
t.Errorf("Expected 2 errors for email field, got %d", len(groups["email"]))
}
if len(groups["name"]) != 1 {
t.Errorf("Expected 1 error for name field, got %d", len(groups["name"]))
}
}
func TestErrorsMarshalJSON(t *testing.T) {
// Test empty errors
var errs Errors
data, err := json.Marshal(errs)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if string(data) != "[]" {
t.Errorf("Expected '[]', got %s", string(data))
}
// Test with field errors
fieldErr := &FieldError{Name: "email", Message: "invalid email"}
errs = Errors{fieldErr}
data, err = json.Marshal(errs)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
var responses []ErrorResponse
err = json.Unmarshal(data, &responses)
if err != nil {
t.Errorf("Failed to unmarshal JSON: %v", err)
}
if len(responses) != 1 {
t.Errorf("Expected 1 response, got %d", len(responses))
}
if responses[0].Message != "invalid email" || responses[0].Parameter != "email" {
t.Error("JSON output does not match expected format")
}
}
func TestFieldErrorError(t *testing.T) {
// Test with custom message
fe := &FieldError{Name: "email", Message: "Custom error message"}
if fe.Error() != "Custom error message" {
t.Errorf("Expected 'Custom error message', got %s", fe.Error())
}
// Test with function error
fe = &FieldError{Name: "email", FuncError: errors.New("function error")}
expected := "validation failed for field 'email': function error"
if fe.Error() != expected {
t.Errorf("Expected '%s', got %s", expected, fe.Error())
}
// Test with no message or function error
fe = &FieldError{Name: "email"}
expected = "validation failed for field 'email'"
if fe.Error() != expected {
t.Errorf("Expected '%s', got %s", expected, fe.Error())
}
}
func TestFieldErrorUnwrap(t *testing.T) {
originalErr := errors.New("original error")
fe := &FieldError{Name: "email", FuncError: originalErr}
if fe.Unwrap() != originalErr {
t.Error("Unwrap should return the original function error")
}
fe = &FieldError{Name: "email"}
if fe.Unwrap() != nil {
t.Error("Unwrap should return nil when no function error")
}
}
func TestFieldErrorHasFuncError(t *testing.T) {
fe := &FieldError{Name: "email", FuncError: errors.New("error")}
if !fe.HasFuncError() {
t.Error("Expected HasFuncError to return true")
}
fe = &FieldError{Name: "email"}
if fe.HasFuncError() {
t.Error("Expected HasFuncError to return false")
}
}
func TestFieldErrorSetMessage(t *testing.T) {
fe := &FieldError{Name: "email"}
fe.SetMessage("New message")
if fe.Message != "New message" {
t.Errorf("Expected 'New message', got %s", fe.Message)
}
}