-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
212 lines (185 loc) · 4.69 KB
/
errors_test.go
File metadata and controls
212 lines (185 loc) · 4.69 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
package fileprep
import (
"strings"
"testing"
)
func TestValidationError_Error(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err *ValidationError
wantRow int
wantCol string
}{
{
name: "basic error",
err: &ValidationError{
Row: 1,
Column: "email",
Field: "Email",
Value: "invalid",
Tag: "email",
Message: "must be a valid email",
},
wantRow: 1,
wantCol: "email",
},
{
name: "empty value",
err: &ValidationError{
Row: 5,
Column: "name",
Field: "Name",
Value: "",
Tag: "required",
Message: "field is required",
},
wantRow: 5,
wantCol: "name",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
errStr := tt.err.Error()
if !strings.Contains(errStr, "row") {
t.Error("error should contain 'row'")
}
if !strings.Contains(errStr, tt.wantCol) {
t.Errorf("error should contain column %q", tt.wantCol)
}
})
}
}
func Test_newValidationError(t *testing.T) {
t.Parallel()
err := newValidationError(1, "email", "Email", "invalid", "email", "must be a valid email")
if err.Row != 1 {
t.Errorf("Row = %d, want 1", err.Row)
}
if err.Column != "email" {
t.Errorf("Column = %q, want %q", err.Column, "email")
}
if err.Field != "Email" {
t.Errorf("Field = %q, want %q", err.Field, "Email")
}
if err.Value != "invalid" {
t.Errorf("Value = %q, want %q", err.Value, "invalid")
}
if err.Tag != "email" {
t.Errorf("Tag = %q, want %q", err.Tag, "email")
}
if err.Message != "must be a valid email" {
t.Errorf("Message = %q, want %q", err.Message, "must be a valid email")
}
}
func TestPrepError_Error(t *testing.T) {
t.Parallel()
err := &PrepError{
Row: 2,
Column: "value",
Field: "Value",
Tag: "truncate",
Message: "truncation failed",
}
errStr := err.Error()
if !strings.Contains(errStr, "row 2") {
t.Error("error should contain 'row 2'")
}
if !strings.Contains(errStr, "value") {
t.Error("error should contain 'value'")
}
if !strings.Contains(errStr, "prep error") {
t.Error("error should contain 'prep error'")
}
}
func Test_newPrepError(t *testing.T) {
t.Parallel()
err := newPrepError(3, "data", "Data", "regex_replace", "invalid regex pattern")
if err.Row != 3 {
t.Errorf("Row = %d, want 3", err.Row)
}
if err.Column != "data" {
t.Errorf("Column = %q, want %q", err.Column, "data")
}
if err.Field != "Data" {
t.Errorf("Field = %q, want %q", err.Field, "Data")
}
if err.Tag != "regex_replace" {
t.Errorf("Tag = %q, want %q", err.Tag, "regex_replace")
}
if err.Message != "invalid regex pattern" {
t.Errorf("Message = %q, want %q", err.Message, "invalid regex pattern")
}
}
func TestProcessResult_InvalidRowCount(t *testing.T) {
t.Parallel()
tests := []struct {
name string
rowCount int
validRowCount int
want int
}{
{"all valid", 10, 10, 0},
{"some invalid", 10, 7, 3},
{"all invalid", 10, 0, 10},
{"empty", 0, 0, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
r := &ProcessResult{
RowCount: tt.rowCount,
ValidRowCount: tt.validRowCount,
}
if got := r.InvalidRowCount(); got != tt.want {
t.Errorf("InvalidRowCount() = %d, want %d", got, tt.want)
}
})
}
}
func TestProcessResult_HasErrors(t *testing.T) {
t.Parallel()
t.Run("no errors", func(t *testing.T) {
t.Parallel()
r := &ProcessResult{}
if r.HasErrors() {
t.Error("HasErrors() should return false for empty errors")
}
})
t.Run("with errors", func(t *testing.T) {
t.Parallel()
r := &ProcessResult{
Errors: []error{newValidationError(1, "col", "Field", "val", "tag", "msg")},
}
if !r.HasErrors() {
t.Error("HasErrors() should return true when errors exist")
}
})
}
func TestProcessResult_ValidationErrors(t *testing.T) {
t.Parallel()
ve1 := newValidationError(1, "col1", "Field1", "val1", "tag1", "msg1")
ve2 := newValidationError(2, "col2", "Field2", "val2", "tag2", "msg2")
pe1 := newPrepError(3, "col3", "Field3", "tag3", "msg3")
r := &ProcessResult{
Errors: []error{ve1, pe1, ve2},
}
validationErrors := r.ValidationErrors()
if len(validationErrors) != 2 {
t.Errorf("ValidationErrors() returned %d errors, want 2", len(validationErrors))
}
}
func TestProcessResult_PrepErrors(t *testing.T) {
t.Parallel()
ve1 := newValidationError(1, "col1", "Field1", "val1", "tag1", "msg1")
pe1 := newPrepError(2, "col2", "Field2", "tag2", "msg2")
pe2 := newPrepError(3, "col3", "Field3", "tag3", "msg3")
r := &ProcessResult{
Errors: []error{ve1, pe1, pe2},
}
prepErrors := r.PrepErrors()
if len(prepErrors) != 2 {
t.Errorf("PrepErrors() returned %d errors, want 2", len(prepErrors))
}
}