-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2structs_test.go
More file actions
377 lines (318 loc) · 7.24 KB
/
Copy pathcsv2structs_test.go
File metadata and controls
377 lines (318 loc) · 7.24 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
365
366
367
368
369
370
371
372
373
374
375
376
377
package csv2structs
import (
"encoding/csv"
"fmt"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func testSetup(t *testing.T, header []string, rows ...[]string) *os.File {
tmpFile, err := os.CreateTemp("", "test_*.csv")
assert.NoError(t, err)
writer := csv.NewWriter(tmpFile)
defer writer.Flush()
assert.NoError(t, writer.Write(header))
assert.NoError(t, writer.WriteAll(rows))
pos, err := tmpFile.Seek(0, 0)
assert.NoError(t, err)
assert.Equal(t, int64(0), pos)
t.Cleanup(func() {
// we might already be closed
_ = tmpFile.Close()
assert.NoError(t, os.Remove(tmpFile.Name()))
})
return tmpFile
}
func TestParse_Simple(t *testing.T) {
type simpleObj struct {
Ident int
Name string
OtherThing string
hiddenFieldsAreIgnored uint64
}
tests := []struct {
name string
header []string
rows [][]string
options []Option
expected []*simpleObj
expectedErr error
}{
{
"happy path",
[]string{"ident", "name", "other_thing"},
[][]string{
{"1", "foo", "bar"},
{"2", "bar", "baz"},
},
nil,
[]*simpleObj{
{
1,
"foo",
"bar",
0,
},
{
2,
"bar",
"baz",
0,
},
},
nil,
},
{
"snake case header transformation is default",
[]string{"Ident", "Name", "OtherThing"},
[][]string{
{"1", "foo", "bar"},
{"2", "bar", "baz"},
},
nil,
nil,
fmt.Errorf("missing headers: OtherThing, Otherthing"),
},
{
"happy path without transforming headers",
[]string{"Ident", "Name", "OtherThing"},
[][]string{
{"1", "foo", "bar"},
{"2", "bar", "baz"},
},
[]Option{WithHeaderType(HeaderTypeNone)},
[]*simpleObj{
{
1,
"foo",
"bar",
0,
},
{
2,
"bar",
"baz",
0,
},
},
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpFile := testSetup(t, tt.header, tt.rows...)
actual, err := Parse[simpleObj](tmpFile, tt.options...)
assert.EqualValues(t, tt.expectedErr, err)
assert.Equal(t, tt.expected, actual)
for _, obj := range actual {
assert.Equal(t, uint64(0), obj.hiddenFieldsAreIgnored)
}
})
}
}
func TestParse_Complete(t *testing.T) {
type completeObj struct {
Int int
Int8 int8
Int16 int16
Int32 int32
Int64 int64
Uint uint
Uint8 uint8
Uint16 uint16
Uint32 uint32
Uint64 uint64
Float32 float32
Float64 float64
String string
Bool bool
}
tests := []struct {
name string
header []string
rows [][]string
expected []*completeObj
expectedErr error
}{
{
"happy path",
[]string{"Int", "Int8", "Int16", "Int32", "Int64", "Uint", "Uint8", "Uint16", "Uint32", "Uint64", "Float32", "Float64", "String", "Bool"},
[][]string{
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11.1", "12.2", "foo", "true"},
},
[]*completeObj{
{
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11.1,
12.2,
"foo",
true,
},
},
nil,
},
{
"missing header is an error",
[]string{"Int", "Int8", "Int16", "Int32", "Int64", "Uint", "Uint8", "Uint16", "Uint32", "Uint64", "Float32", "Float64", "String"},
[][]string{
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11.1", "12.2", "foo", "false"},
},
nil,
fmt.Errorf("missing header: Bool"),
},
{
"lower case headers are title cased, underscores are removed",
[]string{"int", "int_8", "int_16", "int_32", "int_64", "uint", "uint_8", "uint_16", "uint_32", "uint_64", "float_32", "float_64", "string", "bool"},
[][]string{
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11.1", "12.2", "bar", "false"},
},
[]*completeObj{
{
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11.1,
12.2,
"bar",
false,
},
},
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpFile := testSetup(t, tt.header, tt.rows...)
actual, err := Parse[completeObj](tmpFile)
assert.EqualValues(t, tt.expectedErr, err)
assert.Equal(t, tt.expected, actual)
})
}
}
func TestParse_ErrorInterface(t *testing.T) {
type Foo interface{}
resp, err := Parse[Foo](nil)
assert.Nil(t, resp)
assert.ErrorIs(t, err, errInvalidType)
}
func TestParse_ErrorNoFields(t *testing.T) {
type NoFields struct{}
resp, err := Parse[NoFields](nil)
assert.Nil(t, resp)
assert.ErrorIs(t, err, errNoVisibleFields)
}
func TestParse_ErrorNoExportedFields(t *testing.T) {
type NoExportedFields struct {
_ string
}
resp, err := Parse[NoExportedFields](nil)
assert.Nil(t, resp)
assert.ErrorIs(t, err, errNoExportedFields)
}
func TestParse_ErrorInvalidType(t *testing.T) {
type InvalidType struct {
// slices as a field type don't really make sense for CSV...
// you probably want a string then json load that later or something
FieldA []string
}
csvFile := testSetup(t, []string{"field_a"}, []string{`["foo","bar","baz"]`})
resp, err := Parse[InvalidType](csvFile)
assert.Nil(t, resp)
assert.Error(t, err)
assert.Equal(t, "unsupported type: slice", err.Error())
}
func TestParse_ErrorRead(t *testing.T) {
type Foo struct {
Bar string
}
tmpFile := testSetup(t, []string{"bar"}, []string{"baz"})
// close the file so it can't be read from
assert.NoError(t, tmpFile.Close())
resp, err := Parse[Foo](tmpFile)
assert.Nil(t, resp)
assert.Error(t, err)
assert.Contains(t, err.Error(), "file already closed")
}
func TestParse_ErrorIntConversion(t *testing.T) {
type Foo struct {
Bar int
}
tmpFile := testSetup(t, []string{"bar"}, []string{"baz"})
resp, err := Parse[Foo](tmpFile)
assert.Nil(t, resp)
assert.Error(t, err)
assert.Equal(t, err.Error(), `failed to convert "baz" to int`)
}
func TestParse_ErrorBoolConversion(t *testing.T) {
type Foo struct {
Bar bool
}
tmpFile := testSetup(t, []string{"bar"}, []string{"baz"})
resp, err := Parse[Foo](tmpFile)
assert.Nil(t, resp)
assert.Error(t, err)
assert.Equal(t, err.Error(), `failed to convert "baz" to bool`)
}
func TestParse_ErrorFloatConversion(t *testing.T) {
type Foo struct {
Bar float64
}
tmpFile := testSetup(t, []string{"bar"}, []string{"baz"})
resp, err := Parse[Foo](tmpFile)
assert.Nil(t, resp)
assert.Error(t, err)
assert.Equal(t, err.Error(), `failed to convert "baz" to float`)
}
func TestParse_ErrorUintConversion(t *testing.T) {
type Foo struct {
Bar uint
}
tmpFile := testSetup(t, []string{"bar"}, []string{"baz"})
resp, err := Parse[Foo](tmpFile)
assert.Nil(t, resp)
assert.Error(t, err)
assert.Equal(t, err.Error(), `failed to convert "baz" to uint`)
}
func TestParse_CustomTransform(t *testing.T) {
type Foo struct {
BAR string
}
tmpFile := testSetup(t, []string{"bar"}, []string{"baz"})
screamer := func(s string) string {
return strings.ToUpper(s)
}
resp, err := Parse[Foo](tmpFile, WithHeaderTransform(screamer))
assert.NoError(t, err)
assert.Equal(t, []*Foo{{"baz"}}, resp)
}
func TestParse_LeadingByteOrderMark(t *testing.T) {
type Foo struct {
Foo string
Bar string
}
reader := strings.NewReader(
"\xEF\xBB\xBF" + // BOM
"foo,bar\n" + // header
"baz,foo\n", // row
)
resp, err := Parse[Foo](reader)
assert.NoError(t, err)
assert.Equal(t, []*Foo{{"baz", "foo"}}, resp)
}