-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_test.go
More file actions
364 lines (320 loc) · 9.25 KB
/
parser_test.go
File metadata and controls
364 lines (320 loc) · 9.25 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package fileprep
import (
"reflect"
"testing"
)
func TestParsePrepTag(t *testing.T) {
t.Parallel()
tests := []struct {
name string
tag string
wantLen int
wantErr bool
}{
{"empty tag", "", 0, false},
{"single trim", "trim", 1, false},
{"multiple preps", "trim,lowercase", 2, false},
{"with default", "trim,default=N/A", 2, false},
{"unknown tag", "unknown", 0, true},
{"spaces in tag", " trim , lowercase ", 2, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
preps, err := parsePrepTag(tt.tag)
if (err != nil) != tt.wantErr {
t.Errorf("parsePrepTag() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && len(preps) != tt.wantLen {
t.Errorf("parsePrepTag() len = %d, want %d", len(preps), tt.wantLen)
}
})
}
}
func TestParseValidateTag(t *testing.T) {
t.Parallel()
tests := []struct {
name string
tag string
wantLen int
}{
{"empty tag", "", 0},
{"required", "required", 1},
{"unknown tag ignored", "unknown", 0}, // Unknown tags are ignored for now
{"spaces in tag", " required ", 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
vals, _ := parseValidateTag(tt.tag)
if len(vals) != tt.wantLen {
t.Errorf("parseValidateTag() len = %d, want %d", len(vals), tt.wantLen)
}
})
}
}
func TestGetStructType(t *testing.T) {
t.Parallel()
type TestStruct struct {
Name string
}
var nilSlicePtr *[]TestStruct
tests := []struct {
name string
input any
wantErr bool
}{
{"valid slice pointer", &[]TestStruct{}, false},
{"non-pointer", []TestStruct{}, true},
{"pointer to non-slice", &TestStruct{}, true},
{"pointer to slice of non-struct", &[]string{}, true},
{"nil interface", nil, true},
{"nil typed pointer", nilSlicePtr, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
_, err := getStructType(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("getStructType() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
// TestParsePrepTagInvalidFormats tests that invalid tag formats are handled gracefully
func TestParsePrepTagInvalidFormats(t *testing.T) {
t.Parallel()
tests := []struct {
name string
tag string
wantLen int
wantErr bool
errContain string
}{
// Invalid pad_left format - non-numeric value should be skipped (length=0)
{"pad_left non-numeric", "pad_left=abc", 0, false, ""},
{"pad_left negative", "pad_left=-5", 0, false, ""},
{"pad_left empty", "pad_left=", 0, false, ""},
{"pad_left valid", "pad_left=5:0", 1, false, ""},
// Invalid regex_replace format - bad regex should be skipped
{"regex_replace bad pattern", "regex_replace=bad[:X", 0, false, ""},
{"regex_replace no colon", "regex_replace=pattern", 0, false, ""},
{"regex_replace valid", "regex_replace=\\d+:X", 1, false, ""},
// Invalid coerce format - wrong type should be skipped
{"coerce invalid type", "coerce=string", 0, false, ""},
{"coerce valid int", "coerce=int", 1, false, ""},
{"coerce valid float", "coerce=float", 1, false, ""},
{"coerce valid bool", "coerce=bool", 1, false, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
preps, err := parsePrepTag(tt.tag)
if (err != nil) != tt.wantErr {
t.Errorf("parsePrepTag(%q) error = %v, wantErr %v", tt.tag, err, tt.wantErr)
return
}
if tt.wantErr && tt.errContain != "" {
if err == nil || !containsString(err.Error(), tt.errContain) {
t.Errorf("parsePrepTag(%q) error should contain %q, got %v", tt.tag, tt.errContain, err)
}
}
if !tt.wantErr && len(preps) != tt.wantLen {
t.Errorf("parsePrepTag(%q) len = %d, want %d", tt.tag, len(preps), tt.wantLen)
}
})
}
}
// containsString is a helper for checking error messages
func containsString(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
(len(s) > len(substr) && findSubstring(s, substr)))
}
func findSubstring(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}
// TestParseStructTypeWithEmbeddedStruct tests how embedded (anonymous) struct fields are handled
func TestParseStructTypeWithEmbeddedStruct(t *testing.T) {
t.Parallel()
type Embedded struct {
EmbeddedField string `prep:"trim"`
}
type TestStruct struct {
Embedded // Anonymous embedded field
RegularField string `prep:"lowercase"`
}
structType := reflect.TypeOf(TestStruct{})
info, err := parseStructType(structType)
if err != nil {
t.Fatalf("parseStructType() error = %v", err)
}
// Embedded struct should be treated as a single field (Embedded type)
// Regular exported fields should also be parsed
if len(info.Fields) < 1 {
t.Errorf("parseStructType() fields = %d, want at least 1", len(info.Fields))
}
// Check that RegularField is parsed correctly
foundRegular := false
for _, field := range info.Fields {
if field.Name == "RegularField" {
foundRegular = true
if len(field.Preprocessors) != 1 {
t.Errorf("RegularField.Preprocessors len = %d, want 1", len(field.Preprocessors))
}
}
}
if !foundRegular {
t.Error("RegularField not found in parsed fields")
}
}
func TestParseStructType(t *testing.T) {
t.Parallel()
type TestStruct struct {
Name string `prep:"trim" validate:"required"`
Email string `prep:"trim,lowercase"`
Age int
private string //nolint:unused // intentionally unexported for testing
}
structType := reflect.TypeOf(TestStruct{})
info, err := parseStructType(structType)
if err != nil {
t.Fatalf("parseStructType() error = %v", err)
}
// Should have 3 fields (private is skipped)
if len(info.Fields) != 3 {
t.Errorf("parseStructType() fields = %d, want 3", len(info.Fields))
}
// Check first field
if len(info.Fields) > 0 {
field := info.Fields[0]
if field.Name != "Name" {
t.Errorf("Field[0].Name = %q, want %q", field.Name, "Name")
}
if len(field.Preprocessors) != 1 {
t.Errorf("Field[0].Preprocessors len = %d, want 1", len(field.Preprocessors))
}
if len(field.Validators) != 1 {
t.Errorf("Field[0].Validators len = %d, want 1", len(field.Validators))
}
}
// Check second field
if len(info.Fields) > 1 {
field := info.Fields[1]
if field.Name != "Email" {
t.Errorf("Field[1].Name = %q, want %q", field.Name, "Email")
}
if len(field.Preprocessors) != 2 {
t.Errorf("Field[1].Preprocessors len = %d, want 2", len(field.Preprocessors))
}
}
}
// TestToSnakeCase tests the snake_case conversion function
func TestToSnakeCase(t *testing.T) {
t.Parallel()
tests := []struct {
input string
want string
}{
{"", ""},
{"name", "name"},
{"Name", "name"},
{"UserName", "user_name"},
{"ID", "id"},
{"UserID", "user_id"},
{"HTTPServer", "http_server"},
{"XMLParser", "xml_parser"},
{"getHTTPResponse", "get_http_response"},
{"already_snake_case", "already_snake_case"},
{"A", "a"},
{"ABC", "abc"},
{"ABCdef", "ab_cdef"},
{"abcDEF", "abc_def"},
{"IOReader", "io_reader"},
{"myURLParser", "my_url_parser"},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
t.Parallel()
got := toSnakeCase(tt.input)
if got != tt.want {
t.Errorf("toSnakeCase(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}
// TestColumnNameFromNameTag tests that name tag overrides auto-generated column name
func TestColumnNameFromNameTag(t *testing.T) {
t.Parallel()
type TestStruct struct {
UserName string `name:"user_name"`
EmailAddr string `name:"email_address"`
Age int // No name tag - should use "age" (snake_case of "Age")
HTTPStatus string // No name tag - should use "http_status"
CustomField string `name:"custom_col"`
}
structType := reflect.TypeOf(TestStruct{})
info, err := parseStructType(structType)
if err != nil {
t.Fatalf("parseStructType() error = %v", err)
}
expected := map[string]string{
"UserName": "user_name",
"EmailAddr": "email_address",
"Age": "age",
"HTTPStatus": "http_status",
"CustomField": "custom_col",
}
for _, field := range info.Fields {
want, ok := expected[field.Name]
if !ok {
continue
}
if field.ColumnName != want {
t.Errorf("Field %q.ColumnName = %q, want %q", field.Name, field.ColumnName, want)
}
}
}
// TestAutoSnakeCaseColumnNames tests automatic snake_case conversion for column names
func TestAutoSnakeCaseColumnNames(t *testing.T) {
t.Parallel()
type TestStruct struct {
FirstName string
LastName string
EmailAddr string
PhoneNumber string
ID string
UserID string
HTTPCode string
XMLData string
}
structType := reflect.TypeOf(TestStruct{})
info, err := parseStructType(structType)
if err != nil {
t.Fatalf("parseStructType() error = %v", err)
}
expected := map[string]string{
"FirstName": "first_name",
"LastName": "last_name",
"EmailAddr": "email_addr",
"PhoneNumber": "phone_number",
"ID": "id",
"UserID": "user_id",
"HTTPCode": "http_code",
"XMLData": "xml_data",
}
for _, field := range info.Fields {
want, ok := expected[field.Name]
if !ok {
continue
}
if field.ColumnName != want {
t.Errorf("Field %q.ColumnName = %q, want %q", field.Name, field.ColumnName, want)
}
}
}