-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
729 lines (712 loc) · 18.1 KB
/
api_test.go
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
package permissivecsv_test
import (
"errors"
"io"
"strings"
"testing"
"github.com/eltorocorp/permissivecsv"
"github.com/go-test/deep"
"github.com/stretchr/testify/assert"
)
var ErrReader = errors.New("arbitrary reader error")
// BadReader returns ErrReader on the first Read call.
func BadReader(r io.ReadSeeker) io.Reader { return &badReader{r} }
type badReader struct {
r io.ReadSeeker
}
func (r *badReader) Read(p []byte) (int, error) {
return 0, ErrReader
}
func Test_Reader(t *testing.T) {
tests := []struct {
name string
reader io.Reader
expScans int
expCurrentRecord []string
}{
{
name: "reader is nil",
reader: nil,
expScans: 0,
expCurrentRecord: nil,
},
{
name: "reader is not nil",
reader: strings.NewReader(""),
expScans: 0,
expCurrentRecord: []string{},
},
{
// If a reader reports an error, the scanner will stop after the
// current record. If possible, whichever value is loaded as the
// current record in the underlaying io.Scanner is returned, but
// this is typically an empty string. Since no more reads are
// possible, permissivecsv considers this the end of the file.
name: "reader returns an error",
reader: BadReader(strings.NewReader("a\nb\nc")),
expScans: 0,
expCurrentRecord: []string{},
},
}
for _, test := range tests {
testFn := func(t *testing.T) {
s := permissivecsv.NewScanner(test.reader, permissivecsv.HeaderCheckAssumeNoHeader)
n := 0
for s.Scan() {
n++
}
currentRecord := s.CurrentRecord()
assert.Equal(t, test.expScans, n, "expected scans is incorrect")
assert.ElementsMatch(t, test.expCurrentRecord, currentRecord, "current record is incorrect")
}
t.Run(test.name, testFn)
}
}
func Test_ScanAndCurrentRecord(t *testing.T) {
tests := []struct {
name string
input string
result [][]string
}{
{
name: "single empty record",
input: "",
result: [][]string{},
},
{
name: "single record",
input: "1,2,3",
result: [][]string{[]string{"1", "2", "3"}},
},
{
// permissivecsv accepts standard unix record terminators
name: "unix terminators",
input: "a,a,a\nb,b,b\nc,c,c",
result: [][]string{
[]string{"a", "a", "a"},
[]string{"b", "b", "b"},
[]string{"c", "c", "c"},
},
},
{
// permissivecsv accepts standard DOS record terminators
name: "DOS terminators",
input: "a,a,a\r\nb,b,b\r\nc,c,c",
result: [][]string{
[]string{"a", "a", "a"},
[]string{"b", "b", "b"},
[]string{"c", "c", "c"},
},
},
{
// permissivecsv accepts non-standard carriage return terminators
name: "carriage return as terminator",
input: "a,a,a\rb,b,b\rc,c,c",
result: [][]string{
[]string{"a", "a", "a"},
[]string{"b", "b", "b"},
[]string{"c", "c", "c"},
},
},
{
// permissivecsv accepts non-standard "inverted DOS" terminators
name: "inverted DOS terminator",
input: "a,a,a\n\rb,b,b\n\rc,c,c",
result: [][]string{
[]string{"a", "a", "a"},
[]string{"b", "b", "b"},
[]string{"c", "c", "c"},
},
},
{
name: "dangling terminators",
input: "a,a,a\nb,b,b\nc,c,c\n\n\n\n",
result: [][]string{
[]string{"a", "a", "a"},
[]string{"b", "b", "b"},
[]string{"c", "c", "c"},
},
},
{
name: "leading terminators",
input: "\r\n\r\n\r\n\r\n\r\n\r\na,a,a\r\nb,b,b\r\nc,c,c",
result: [][]string{
[]string{"a", "a", "a"},
[]string{"b", "b", "b"},
[]string{"c", "c", "c"},
},
},
{
name: "empty records",
input: "a,a,a\nb,b,b\n\n\n\n\nc,c,c",
result: [][]string{
[]string{"a", "a", "a"},
[]string{"b", "b", "b"},
[]string{"c", "c", "c"},
},
},
{
// if the first thing permissivecsv encounters is an empty field
// followed by a terminator, it assumes that to be correct. All
// subsequent records are expected to also only have one field, and
// are truncated as necessary.
name: "loitering empty field",
input: "\"\"\na,a,a\nb,b,b\nc,c,c",
result: [][]string{
[]string{""},
[]string{"a"},
[]string{"b"},
[]string{"c"},
},
},
{
// permissivecsv doesn't care about mixing terminators.
name: "mixed terminators",
input: "a,a\nb,b\nc,c\r\nd,d\ne,e\n\rf,f",
result: [][]string{
[]string{"a", "a"},
[]string{"b", "b"},
[]string{"c", "c"},
[]string{"d", "d"},
[]string{"e", "e"},
[]string{"f", "f"},
},
},
{
// permissivecsv ignores terminators that are quoted
name: "ignore quoted",
input: "a,a,a\n\"\n\",b,b\nc,c,c",
result: [][]string{
[]string{"a", "a", "a"},
[]string{"\n", "b", "b"},
[]string{"c", "c", "c"},
},
},
{
// permissivecsv will nullify the values for all of a record's
// fields if it encounters a bare quote. This is the most
// consistent way to represent data that has been corrupted in this
// manner. permissivecsv's handling of bare quotes differs from
// the stdlib's csv.Reader. csv.Reader will concatenate all of a
// record's data into a single field if it encounters an unpaired
// quote. This results in a variation of data output per record that
// encounters this issue. permissivecsv instead blanks the data
// for the bad record, and reports the issue via Summary.
// This reduces the number of data variants output by the Scanner,
// while allowing the caller to still handle issues as they see fit.
name: "bare quotes",
input: "a,a,a\n\"b\"b,b,b\nc,c,c",
result: [][]string{
[]string{"a", "a", "a"},
[]string{"", "", ""},
[]string{"c", "c", "c"},
},
},
{
// permissivecsv handles extraneous quotes the same way that it
// handles bare quotes, by nullifying the field values of the
// affected record.
name: "extraneous quote",
input: "a,a,a\nb\"\"b,b,b\nc,c,c",
result: [][]string{
[]string{"a", "a", "a"},
[]string{"", "", ""},
[]string{"c", "c", "c"},
},
},
}
for _, test := range tests {
testFn := func(t *testing.T) {
r := strings.NewReader(test.input)
s := permissivecsv.NewScanner(r, permissivecsv.HeaderCheckAssumeNoHeader)
result := [][]string{}
for s.Scan() {
result = append(result, s.CurrentRecord())
}
assert.Equal(t, test.result, result)
}
t.Run(test.name, testFn)
}
}
func Test_Summary(t *testing.T) {
tests := []struct {
name string
data io.ReadSeeker
// scanLimit caps the number of times the test fixture will
// call Scan. -1 will call Scan until it returns false.
scanLimit int
expSummary *permissivecsv.ScanSummary
}{
{
name: "summary nil before Scan called",
data: strings.NewReader("a,b,c"),
scanLimit: 0,
expSummary: nil,
},
{
name: "nil reader",
data: nil,
scanLimit: -1,
expSummary: &permissivecsv.ScanSummary{
RecordCount: -1,
AlterationCount: -1,
EOF: false,
Err: permissivecsv.ErrReaderIsNil,
Alterations: []*permissivecsv.Alteration{},
},
},
{
name: "extraneous quotes",
data: strings.NewReader("\""),
scanLimit: -1,
expSummary: &permissivecsv.ScanSummary{
RecordCount: 1,
AlterationCount: 1,
EOF: true,
Err: nil,
Alterations: []*permissivecsv.Alteration{
&permissivecsv.Alteration{
RecordOrdinal: 1,
OriginalData: "\"",
ResultingRecord: []string{},
AlterationDescription: permissivecsv.AltExtraneousQuote,
},
},
},
},
{
name: "bare quote",
data: strings.NewReader("a\nb\""),
scanLimit: -1,
expSummary: &permissivecsv.ScanSummary{
RecordCount: 2,
AlterationCount: 1,
EOF: true,
Err: nil,
Alterations: []*permissivecsv.Alteration{
&permissivecsv.Alteration{
RecordOrdinal: 2,
OriginalData: "b\"",
ResultingRecord: []string{""},
AlterationDescription: permissivecsv.AltBareQuote,
},
},
},
},
{
name: "truncated record",
data: strings.NewReader("a,b,c\nd,e,f,g"),
scanLimit: -1,
expSummary: &permissivecsv.ScanSummary{
RecordCount: 2,
AlterationCount: 1,
EOF: true,
Err: nil,
Alterations: []*permissivecsv.Alteration{
&permissivecsv.Alteration{
RecordOrdinal: 2,
OriginalData: "d,e,f,g",
ResultingRecord: []string{"d", "e", "f"},
AlterationDescription: permissivecsv.AltTruncatedRecord,
},
},
},
},
{
name: "padded record",
data: strings.NewReader("a,b,c\nd,e"),
scanLimit: -1,
expSummary: &permissivecsv.ScanSummary{
RecordCount: 2,
AlterationCount: 1,
EOF: true,
Err: nil,
Alterations: []*permissivecsv.Alteration{
&permissivecsv.Alteration{
RecordOrdinal: 2,
OriginalData: "d,e",
ResultingRecord: []string{"d", "e", ""},
AlterationDescription: permissivecsv.AltPaddedRecord,
},
},
},
},
{
name: "EOF false before end of file",
data: strings.NewReader("a\n\b\nc"),
scanLimit: 1,
expSummary: &permissivecsv.ScanSummary{
RecordCount: 1,
AlterationCount: 0,
EOF: false,
Err: nil,
Alterations: []*permissivecsv.Alteration{},
},
},
}
for _, test := range tests {
testFn := func(t *testing.T) {
s := permissivecsv.NewScanner(test.data, permissivecsv.HeaderCheckAssumeNoHeader)
for n := 0; ; n++ {
if test.scanLimit >= 0 && n >= test.scanLimit {
break
}
more := s.Scan()
if !more {
break
}
}
summary := s.Summary()
if test.expSummary == nil {
assert.Nil(t, summary)
} else {
diff := deep.Equal(summary, test.expSummary)
if diff != nil {
t.Error(diff)
}
}
}
t.Run(test.name, testFn)
}
}
func Test_HeaderCheckCallback(t *testing.T) {
tests := []struct {
name string
data string
scanLimit int
expFirstRecord []string
expSecondRecord []string
}{
{
name: "nils before Scan",
data: "a,b,c\nd,e,f\ng,h,i",
scanLimit: 0,
expFirstRecord: nil,
},
{
name: "1st correct on first Scan",
data: "a,b,c\nd,e,f\ng,h,i",
scanLimit: 1,
expFirstRecord: []string{"a", "b", "c"},
},
{
name: "scan advanced beyond first record",
data: "a,b,c\nd,e,f\ng,h,i",
scanLimit: -1,
expFirstRecord: nil,
},
}
for _, test := range tests {
testFn := func(t *testing.T) {
var actualFirstRecord []string
headerCheck := func(firstRecord []string) bool {
actualFirstRecord = firstRecord
return false
}
r := strings.NewReader(test.data)
s := permissivecsv.NewScanner(r, headerCheck)
for n := 0; ; n++ {
if test.scanLimit >= 0 && n >= test.scanLimit {
break
}
more := s.Scan()
// actual result of RecordIsHeader isn't pertinant to these test
// cases
_ = s.RecordIsHeader()
if !more {
break
}
}
if test.expFirstRecord == nil {
assert.Nil(t, actualFirstRecord, "expected first record to be nil")
} else {
assert.Equal(t, test.expFirstRecord, actualFirstRecord)
}
}
t.Run(test.name, testFn)
}
}
func Test_Partition(t *testing.T) {
// The partition tests specifically target segment generation capabilities,
// and presume that the underlaying record splitter is properly identifying
// terminators and returning raw records to Split as intended.
tests := []struct {
name string
data io.ReadSeeker
recordsPerPartition int
excludeHeader bool
expPartitions []*permissivecsv.Segment
}{
{
name: "nil reader",
data: nil,
recordsPerPartition: 10,
excludeHeader: false,
expPartitions: []*permissivecsv.Segment{},
},
{
name: "empty file",
data: strings.NewReader(""),
recordsPerPartition: 10,
excludeHeader: false,
expPartitions: []*permissivecsv.Segment{},
},
{
name: "one byte long terminator",
data: strings.NewReader("a,b\nc,d\ne,f\ng,h\ni,j\nk,l"),
recordsPerPartition: 2,
excludeHeader: false,
expPartitions: []*permissivecsv.Segment{
&permissivecsv.Segment{
Ordinal: 1,
LowerOffset: 0,
Length: 8,
},
&permissivecsv.Segment{
Ordinal: 2,
LowerOffset: 8,
Length: 8,
},
&permissivecsv.Segment{
Ordinal: 3,
LowerOffset: 16,
Length: 7,
},
},
},
{
name: "two byte long terminator",
data: strings.NewReader("a,b\r\nc,d\r\ne,f\r\ng,h\r\ni,j\r\nk,l"),
recordsPerPartition: 2,
excludeHeader: false,
expPartitions: []*permissivecsv.Segment{
&permissivecsv.Segment{
Ordinal: 1,
LowerOffset: 0,
Length: 10,
},
&permissivecsv.Segment{
Ordinal: 2,
LowerOffset: 10,
Length: 10,
},
&permissivecsv.Segment{
Ordinal: 3,
LowerOffset: 20,
Length: 8,
},
},
},
{
name: "one byte term with partial final segment",
data: strings.NewReader("a,b\nc,d\ne,f\ng,h\ni,j\nk,l\nm,n"),
recordsPerPartition: 2,
excludeHeader: false,
expPartitions: []*permissivecsv.Segment{
&permissivecsv.Segment{
Ordinal: 1,
LowerOffset: 0,
Length: 8,
},
&permissivecsv.Segment{
Ordinal: 2,
LowerOffset: 8,
Length: 8,
},
&permissivecsv.Segment{
Ordinal: 3,
LowerOffset: 16,
Length: 8,
},
&permissivecsv.Segment{
Ordinal: 4,
LowerOffset: 24,
Length: 3,
},
},
},
{
name: "two byte term with partial final segment",
data: strings.NewReader("a,b\r\nc,d\r\ne,f\r\ng,h\r\ni,j\r\nk,l\r\nm,n"),
recordsPerPartition: 2,
excludeHeader: false,
expPartitions: []*permissivecsv.Segment{
&permissivecsv.Segment{
Ordinal: 1,
LowerOffset: 0,
Length: 10,
},
&permissivecsv.Segment{
Ordinal: 2,
LowerOffset: 10,
Length: 10,
},
&permissivecsv.Segment{
Ordinal: 3,
LowerOffset: 20,
Length: 10,
},
&permissivecsv.Segment{
Ordinal: 4,
LowerOffset: 30,
Length: 3,
},
},
},
{
name: "mixed terminators",
data: strings.NewReader("a,b\r\nc,d\ne,f\ng,h\ni,j\nk,l\nm,n"),
recordsPerPartition: 2,
excludeHeader: false,
expPartitions: []*permissivecsv.Segment{
&permissivecsv.Segment{
Ordinal: 1,
LowerOffset: 0,
Length: 9,
},
&permissivecsv.Segment{
Ordinal: 2,
LowerOffset: 9,
Length: 8,
},
&permissivecsv.Segment{
Ordinal: 3,
LowerOffset: 17,
Length: 8,
},
&permissivecsv.Segment{
Ordinal: 4,
LowerOffset: 25,
Length: 3,
},
},
},
{
name: "variable record lengths",
data: strings.NewReader("a,b,c\ndd\nee,ff,gg,h\ni,j"),
recordsPerPartition: 2,
excludeHeader: false,
expPartitions: []*permissivecsv.Segment{
&permissivecsv.Segment{
Ordinal: 1,
LowerOffset: 0,
Length: 9,
},
&permissivecsv.Segment{
Ordinal: 2,
LowerOffset: 9,
Length: 14,
},
},
},
{
name: "one byte term ignore header",
data: strings.NewReader("a,b\nc,d\ne,f\ng,h\ni,j\nk,l\nm,n"),
recordsPerPartition: 2,
excludeHeader: true,
expPartitions: []*permissivecsv.Segment{
&permissivecsv.Segment{
Ordinal: 1,
LowerOffset: 4,
Length: 8,
},
&permissivecsv.Segment{
Ordinal: 2,
LowerOffset: 12,
Length: 8,
},
&permissivecsv.Segment{
Ordinal: 3,
LowerOffset: 20,
Length: 7,
},
},
},
{
name: "two byte term ignore header",
data: strings.NewReader("a,b\n\rc,d\n\re,f\n\rg,h\n\ri,j\n\rk,l\n\rm,n"),
recordsPerPartition: 2,
excludeHeader: true,
expPartitions: []*permissivecsv.Segment{
&permissivecsv.Segment{
Ordinal: 1,
LowerOffset: 5,
Length: 10,
},
&permissivecsv.Segment{
Ordinal: 2,
LowerOffset: 15,
Length: 10,
},
&permissivecsv.Segment{
Ordinal: 3,
LowerOffset: 25,
Length: 8,
},
},
},
{
name: "leading terminators",
data: strings.NewReader("\n\n\na\nb\nc\nd"),
recordsPerPartition: 2,
excludeHeader: false,
expPartitions: []*permissivecsv.Segment{
&permissivecsv.Segment{
Ordinal: 1,
LowerOffset: 0,
Length: 7,
},
&permissivecsv.Segment{
Ordinal: 2,
LowerOffset: 7,
Length: 3,
},
},
},
{
name: "dangling terminators",
data: strings.NewReader("a\nb\n\n\n"),
recordsPerPartition: 2,
excludeHeader: false,
expPartitions: []*permissivecsv.Segment{
&permissivecsv.Segment{
Ordinal: 1,
LowerOffset: 0,
Length: 6,
},
},
},
{
name: "empty records",
data: strings.NewReader("a\nb\n\n\nc"),
recordsPerPartition: 2,
excludeHeader: false,
expPartitions: []*permissivecsv.Segment{
&permissivecsv.Segment{
Ordinal: 1,
LowerOffset: 0,
Length: 6,
},
&permissivecsv.Segment{
Ordinal: 2,
LowerOffset: 6,
Length: 1,
},
},
},
}
for _, test := range tests {
testFn := func(t *testing.T) {
s := permissivecsv.NewScanner(test.data, permissivecsv.HeaderCheckAssumeHeaderExists)
partitions := s.Partition(test.recordsPerPartition, test.excludeHeader)
diff := deep.Equal(test.expPartitions, partitions)
if diff != nil {
for _, d := range diff {
t.Log(d)
}
t.Fail()
}
}
t.Run(test.name, testFn)
}
}