-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcsv_test.go
More file actions
720 lines (632 loc) · 18.5 KB
/
Copy pathcsv_test.go
File metadata and controls
720 lines (632 loc) · 18.5 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
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
package otters
import (
"math"
"os"
"testing"
"time"
)
func TestReadCSVEdgeCases(t *testing.T) {
// Test with skip rows
csvData := `header1,header2
skip1,skip2
data1,data2
data3,data4`
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString(csvData)
tmpfile.Close()
df, err := ReadCSVWithOptions(tmpfile.Name(), CSVOptions{
HasHeader: true,
Delimiter: ',',
SkipRows: 1,
})
if err != nil {
t.Errorf("ReadCSVWithOptions error: %v", err)
}
if df.Len() != 2 {
t.Errorf("Expected 2 rows, got %d", df.Len())
}
}
func TestReadCSVWithoutHeaders(t *testing.T) {
csvData := `1,2,3
4,5,6
7,8,9`
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString(csvData)
tmpfile.Close()
df, err := ReadCSVWithOptions(tmpfile.Name(), CSVOptions{
HasHeader: false,
Delimiter: ',',
})
if err != nil {
t.Errorf("ReadCSVWithOptions error: %v", err)
}
if df.Width() != 3 {
t.Errorf("Expected 3 columns, got %d", df.Width())
}
if !df.HasColumn("Column_0") {
t.Error("Should have generated column names")
}
}
func TestReadCSVMaxRows(t *testing.T) {
csvData := `a,b
1,2
3,4
5,6
7,8`
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString(csvData)
tmpfile.Close()
df, err := ReadCSVWithOptions(tmpfile.Name(), CSVOptions{
HasHeader: true,
Delimiter: ',',
MaxRows: 2,
})
if err != nil {
t.Errorf("ReadCSVWithOptions error: %v", err)
}
if df.Len() != 2 {
t.Errorf("Expected 2 rows with MaxRows, got %d", df.Len())
}
}
func TestReadCSVFromStringEdgeCases(t *testing.T) {
csvData := `name,age
Alice,25
Bob,30`
df, err := ReadCSVFromStringWithOptions(csvData, CSVOptions{
HasHeader: true,
Delimiter: ',',
})
if err != nil {
t.Errorf("ReadCSVFromStringWithOptions error: %v", err)
}
if df.Len() != 2 {
t.Errorf("Expected 2 rows, got %d", df.Len())
}
}
func TestWriteCSVEdgeCases(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
"col2": []string{"a", "b", "c"},
}
df, _ := NewDataFrameFromMap(data)
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.Close()
err := df.WriteCSV(tmpfile.Name())
if err != nil {
t.Errorf("WriteCSV error: %v", err)
}
// Read it back
df2, err := ReadCSV(tmpfile.Name())
if err != nil {
t.Errorf("ReadCSV error: %v", err)
}
if df2.Len() != 3 {
t.Error("Written CSV should be readable")
}
}
func TestDetectDelimiter(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString("a;b;c\n1;2;3")
tmpfile.Close()
delim, err := DetectDelimiter(tmpfile.Name())
if err != nil || delim != ';' {
t.Errorf("DetectDelimiter = %c, %v, want ;", delim, err)
}
}
func TestValidateCSV(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString("a,b,c\n1,2,3\n4,5,6")
tmpfile.Close()
info, err := ValidateCSV(tmpfile.Name())
if err != nil {
t.Errorf("ValidateCSV error: %v", err)
}
if info.Columns != 3 {
t.Errorf("ValidateCSV columns = %d, want 3", info.Columns)
}
}
func TestCleanHeader(t *testing.T) {
// Test BOM removal
header := "\ufeffName"
cleaned := cleanHeader(header)
if cleaned != "Name" {
t.Errorf("cleanHeader should remove BOM, got %s", cleaned)
}
// Test whitespace trimming
header2 := " Name "
cleaned2 := cleanHeader(header2)
if cleaned2 != "Name" {
t.Errorf("cleanHeader should trim spaces, got %s", cleaned2)
}
}
func TestCSV_ConvertStringSliceToType_Success_AllTypes(t *testing.T) {
// int64
intData := []string{"1", "2", "3"}
result, err := convertStringSliceToType(intData, Int64Type)
if err != nil {
t.Errorf("convertStringSliceToType int64 error: %v", err)
}
intSlice, ok := result.([]int64)
if !ok || len(intSlice) != 3 || intSlice[0] != 1 {
t.Error("convertStringSliceToType should convert to []int64")
}
// float64
floatData := []string{"1.1", "2.2", "3.3"}
result2, err2 := convertStringSliceToType(floatData, Float64Type)
if err2 != nil {
t.Errorf("convertStringSliceToType float64 error: %v", err2)
}
floatSlice, ok2 := result2.([]float64)
if !ok2 || len(floatSlice) != 3 {
t.Error("convertStringSliceToType should convert to []float64")
}
// bool
boolData := []string{"true", "false", "true"}
result3, err3 := convertStringSliceToType(boolData, BoolType)
if err3 != nil {
t.Errorf("convertStringSliceToType bool error: %v", err3)
}
boolSlice, ok3 := result3.([]bool)
if !ok3 || len(boolSlice) != 3 || !boolSlice[0] {
t.Error("convertStringSliceToType should convert to []bool")
}
// time
timeData := []string{"2023-01-01", "2023-01-02"}
result4, err4 := convertStringSliceToType(timeData, TimeType)
if err4 != nil {
t.Errorf("convertStringSliceToType time error: %v", err4)
}
timeSlice, ok4 := result4.([]time.Time)
if !ok4 || len(timeSlice) != 2 {
t.Error("convertStringSliceToType should convert to []time.Time")
}
// string
strData := []string{"a", "b", "c"}
result5, err5 := convertStringSliceToType(strData, StringType)
if err5 != nil {
t.Errorf("convertStringSliceToType string error: %v", err5)
}
strSlice, ok5 := result5.([]string)
if !ok5 || len(strSlice) != 3 {
t.Error("convertStringSliceToType should keep []string")
}
}
func TestCSV_ConvertStringSliceToType_Failure_InvalidData(t *testing.T) {
invalidInt := []string{"not", "a", "number"}
_, err := convertStringSliceToType(invalidInt, Int64Type)
if err == nil {
t.Error("convertStringSliceToType should error on invalid int64")
}
invalidFloat := []string{"not", "a", "float"}
_, err2 := convertStringSliceToType(invalidFloat, Float64Type)
if err2 == nil {
t.Error("convertStringSliceToType should error on invalid float64")
}
invalidBool := []string{"not", "a", "bool"}
_, err3 := convertStringSliceToType(invalidBool, BoolType)
if err3 == nil {
t.Error("convertStringSliceToType should error on invalid bool")
}
invalidTime := []string{"not", "a", "time"}
_, err4 := convertStringSliceToType(invalidTime, TimeType)
if err4 == nil {
t.Error("convertStringSliceToType should error on invalid time")
}
}
func TestCSV_BuildDataFrameFromRows_EdgeCases(t *testing.T) {
// Empty headers
df, err := buildDataFrameFromRows([]string{}, [][]string{})
if err != nil || df.Width() != 0 {
t.Error("buildDataFrameFromRows empty should work")
}
// No rows
df2, err2 := buildDataFrameFromRows([]string{"col1", "col2"}, [][]string{})
if err2 != nil || df2.Width() != 2 {
t.Error("buildDataFrameFromRows no rows should create empty DataFrame with columns")
}
}
func TestCSV_ReadCSV_EmptyFile_ReturnsEmptyDataFrame(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.Close()
df, err := ReadCSV(tmpfile.Name())
if err != nil {
t.Errorf("ReadCSV empty file error: %v", err)
}
if df.Len() != 0 {
t.Error("ReadCSV empty file should return empty DataFrame")
}
}
func TestCSV_ReadCSV_RowLengthMismatch_Errors(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString("a,b,c\n1,2,3\n4,5\n")
tmpfile.Close()
_, err := ReadCSV(tmpfile.Name())
if err == nil {
t.Error("ReadCSV should error on row length mismatch")
}
}
func TestCSV_ReadCSVWithOptions_SkipRowsPastEOF_ReturnsEmpty(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString("header\n")
tmpfile.Close()
df, err := ReadCSVWithOptions(tmpfile.Name(), CSVOptions{
HasHeader: true,
Delimiter: ',',
SkipRows: 10,
})
if err != nil {
t.Errorf("ReadCSVWithOptions error: %v", err)
}
if df.Len() != 0 {
t.Error("Should return empty DataFrame when skipping past EOF")
}
}
func TestCSV_ReadCSVWithOptions_EOF_ReturnsEmpty(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString("")
tmpfile.Close()
df, _ := ReadCSVWithOptions(tmpfile.Name(), CSVOptions{
HasHeader: true,
Delimiter: ',',
})
if df.Len() != 0 {
t.Error("ReadCSVWithOptions EOF should return empty DataFrame")
}
}
func TestCSV_ReadCSVWithOptions_MaxRows_NoHeader_LimitsRows(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString("1,2,3\n4,5,6\n7,8,9\n10,11,12")
tmpfile.Close()
df, err := ReadCSVWithOptions(tmpfile.Name(), CSVOptions{
HasHeader: false,
Delimiter: ',',
MaxRows: 2,
})
if err != nil {
t.Errorf("ReadCSVWithOptions error: %v", err)
}
if df.Len() != 2 {
t.Errorf("Expected 2 rows with MaxRows, got %d", df.Len())
}
}
func TestCSV_ReadCSVFromStringWithOptions_NoHeader_GeneratesColumnNames(t *testing.T) {
csvData := "1,2,3\n4,5,6"
df, err := ReadCSVFromStringWithOptions(csvData, CSVOptions{
HasHeader: false,
Delimiter: ',',
})
if err != nil {
t.Errorf("ReadCSVFromStringWithOptions error: %v", err)
}
if df.Width() != 3 {
t.Error("Should generate column names")
}
}
func TestCSV_ReadCSVFromStringWithOptions_RowMismatch_Errors(t *testing.T) {
csvData := "a,b,c\n1,2,3\n4,5"
_, err := ReadCSVFromStringWithOptions(csvData, CSVOptions{
HasHeader: true,
Delimiter: ',',
})
if err == nil {
t.Error("Should error on row length mismatch")
}
}
func TestCSV_ReadCSVFromStringWithOptions_MaxRows_LimitsRows(t *testing.T) {
csvData := "a,b\n1,2\n3,4\n5,6\n7,8"
df, err := ReadCSVFromStringWithOptions(csvData, CSVOptions{
HasHeader: true,
Delimiter: ',',
MaxRows: 2,
})
if err != nil {
t.Errorf("ReadCSVFromStringWithOptions error: %v", err)
}
if df.Len() != 2 {
t.Errorf("Expected 2 rows with MaxRows, got %d", df.Len())
}
}
func TestCSV_WriteCSV_PropagatesDataFrameError(t *testing.T) {
df := NewDataFrame()
df.err = newOpError("test", "error")
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.Close()
err := df.WriteCSV(tmpfile.Name())
if err == nil {
t.Error("WriteCSV should propagate error")
}
}
func TestCSV_WriteCSVWithOptions_WritesFile(t *testing.T) {
data := map[string]any{
"col1": []int64{1, 2, 3},
"col2": []float64{1.1, 2.2, 3.3},
"col3": []bool{true, false, true},
}
df, _ := NewDataFrameFromMap(data)
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.Close()
err := df.WriteCSVWithOptions(tmpfile.Name(), CSVOptions{
HasHeader: true,
Delimiter: ',',
})
if err != nil {
t.Errorf("WriteCSVWithOptions error: %v", err)
}
}
func TestCSV_WriteCSV_TimeColumn_WritesFile(t *testing.T) {
tm := time.Date(2023, 1, 1, 12, 30, 0, 0, time.UTC)
data := map[string]any{
"col1": []time.Time{tm, tm},
}
df, _ := NewDataFrameFromMap(data)
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.Close()
err := df.WriteCSV(tmpfile.Name())
if err != nil {
t.Errorf("WriteCSV with time error: %v", err)
}
}
func TestCSV_DetectDelimiter_Tab(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString("a\tb\tc\n1\t2\t3")
tmpfile.Close()
delim, err := DetectDelimiter(tmpfile.Name())
if err != nil || delim != '\t' {
t.Errorf("DetectDelimiter = %c, %v, want tab", delim, err)
}
}
func TestCSV_DetectDelimiter_Pipe(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString("a|b|c\n1|2|3")
tmpfile.Close()
delim, err := DetectDelimiter(tmpfile.Name())
if err != nil || delim != '|' {
t.Errorf("DetectDelimiter = %c, %v, want |", delim, err)
}
}
func TestCSV_DetectDelimiter_DefaultComma(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString("abc")
tmpfile.Close()
delim, err := DetectDelimiter(tmpfile.Name())
if err != nil || delim != ',' {
t.Errorf("DetectDelimiter should default to comma, got %c", delim)
}
}
func TestCSV_DetectDelimiter_ErrorOnMissingFile(t *testing.T) {
_, err := DetectDelimiter("/nonexistent/file.csv")
if err == nil {
t.Error("DetectDelimiter should error on nonexistent file")
}
}
func TestCSV_CleanHeader_TrimsSpaces(t *testing.T) {
header := " Name "
cleaned := cleanHeader(header)
if cleaned != "Name" {
t.Errorf("cleanHeader = %s, want Name", cleaned)
}
}
func TestCSV_CleanHeader_StripsBOMAndSpaces(t *testing.T) {
header := "\ufeff Name "
cleaned := cleanHeader(header)
if cleaned != "Name" {
t.Errorf("cleanHeader = %s, want Name", cleaned)
}
}
func TestCSV_ValidateCSV_ErrorOnMissingFile(t *testing.T) {
_, err := ValidateCSV("/nonexistent/file.csv")
if err == nil {
t.Error("ValidateCSV should error on nonexistent file")
}
}
func TestCSV_ValidateCSV_ReturnsInfo(t *testing.T) {
tmpfile, _ := os.CreateTemp("", "test*.csv")
defer os.Remove(tmpfile.Name())
tmpfile.WriteString("a,b,c\n1,2,3\n4,5,6")
tmpfile.Close()
info, err := ValidateCSV(tmpfile.Name())
if err != nil {
t.Errorf("ValidateCSV error: %v", err)
}
if info.Columns != 3 {
t.Errorf("CSVInfo.Columns = %d, want 3", info.Columns)
}
if info.Rows < 2 {
t.Errorf("CSVInfo.Rows = %d, want at least 2", info.Rows)
}
if info.Delimiter != ',' {
t.Errorf("CSVInfo.Delimiter = %c, want ,", info.Delimiter)
}
}
// Regression: a CSV column of 0/1 flags used to be inferred as BoolType
// because strconv.ParseBool accepts "0"/"1"/"t"/"f", so round-tripping a
// file rewrote the data as true/false.
func TestCSVZeroOneColumnRoundTrip(t *testing.T) {
dir := t.TempDir()
in := dir + "/in.csv"
out := dir + "/out.csv"
if err := os.WriteFile(in, []byte("id,flag\n10,0\n20,1\n30,0\n"), 0o644); err != nil {
t.Fatal(err)
}
df, err := ReadCSV(in)
if err != nil {
t.Fatal(err)
}
colType, _ := df.GetColumnType("flag")
if colType != Int64Type {
t.Errorf("flag column inferred as %v, want int64", colType)
}
if err := df.WriteCSV(out); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(out)
if err != nil {
t.Fatal(err)
}
want := "id,flag\n10,0\n20,1\n30,0\n"
if string(data) != want {
t.Errorf("round-trip changed the data:\n got: %q\nwant: %q", string(data), want)
}
}
// TestCSVOperations covers CSV parsing with automatic type inference.
func TestCSVOperations(t *testing.T) {
csvData := `name,age,score
Alice,25,95.5
Bob,30,87.2
Carol,28,92.1`
df, err := ReadCSVFromString(csvData)
if err != nil {
t.Fatalf("Failed to read CSV: %v", err)
}
// Test automatic type inference
ageType, err := df.GetColumnType("age")
if err != nil {
t.Fatalf("Failed to get column type: %v", err)
}
if ageType != Int64Type {
t.Errorf("Expected Int64Type for age, got %v", ageType)
}
scoreType, err := df.GetColumnType("score")
if err != nil {
t.Fatalf("Failed to get column type: %v", err)
}
if scoreType != Float64Type {
t.Errorf("Expected Float64Type for score, got %v", scoreType)
}
// Test statistics with proper floating point comparison
avgScore, err := df.Mean("score")
if err != nil {
t.Fatalf("Failed to calculate mean: %v", err)
}
expectedAvg := (95.5 + 87.2 + 92.1) / 3
// Use tolerance for floating point comparison
tolerance := 0.001
if math.Abs(avgScore-expectedAvg) > tolerance {
t.Errorf("Expected average %.6f, got %.6f", expectedAvg, avgScore)
}
}
// TestCSVFileOperations covers file-based CSV I/O using os.CreateTemp.
func TestCSVFileOperations(t *testing.T) {
data := map[string]any{
"id": []int64{1, 2, 3},
"name": []string{"Alice", "Bob", "Carol"},
"age": []int64{25, 30, 35},
}
df, err := NewDataFrameFromMap(data)
if err != nil {
t.Fatalf("setup failed: %v", err)
}
// WriteCSV + ReadCSV roundtrip
tmpCSV, err := os.CreateTemp("", "otter_test_*.csv")
if err != nil {
t.Fatalf("CreateTemp error: %v", err)
}
tmpCSV.Close()
defer os.Remove(tmpCSV.Name())
if err := df.WriteCSV(tmpCSV.Name()); err != nil {
t.Fatalf("WriteCSV error: %v", err)
}
df2, err := ReadCSV(tmpCSV.Name())
if err != nil {
t.Fatalf("ReadCSV error: %v", err)
}
rows, cols := df2.Shape()
if rows != 3 || cols != 3 {
t.Errorf("ReadCSV roundtrip: got shape (%d, %d), want (3, 3)", rows, cols)
}
// WriteCSVWithOptions + ReadCSVWithOptions with tab delimiter
tmpTSV, err := os.CreateTemp("", "otter_test_*.tsv")
if err != nil {
t.Fatalf("CreateTemp error: %v", err)
}
tmpTSV.Close()
defer os.Remove(tmpTSV.Name())
if err := df.WriteCSVWithOptions(tmpTSV.Name(), CSVOptions{HasHeader: true, Delimiter: '\t'}); err != nil {
t.Fatalf("WriteCSVWithOptions error: %v", err)
}
df3, err := ReadCSVWithOptions(tmpTSV.Name(), CSVOptions{HasHeader: true, Delimiter: '\t'})
if err != nil {
t.Fatalf("ReadCSVWithOptions error: %v", err)
}
rows, cols = df3.Shape()
if rows != 3 || cols != 3 {
t.Errorf("ReadCSVWithOptions tab roundtrip: got shape (%d, %d), want (3, 3)", rows, cols)
}
// DetectDelimiter on tab-delimited file
delim, err := DetectDelimiter(tmpTSV.Name())
if err != nil {
t.Fatalf("DetectDelimiter error: %v", err)
}
if delim != '\t' {
t.Errorf("DetectDelimiter: got %q, want tab", string(delim))
}
// ValidateCSV on valid file
info, err := ValidateCSV(tmpCSV.Name())
if err != nil {
t.Fatalf("ValidateCSV error: %v", err)
}
if info == nil {
t.Fatal("ValidateCSV returned nil info")
}
// ValidateCSV on invalid file (inconsistent column counts)
tmpInvalid, err := os.CreateTemp("", "otter_invalid_*.csv")
if err != nil {
t.Fatalf("CreateTemp error: %v", err)
}
_, _ = tmpInvalid.WriteString("col1,col2\n1,2\n3,4,5\n")
tmpInvalid.Close()
defer os.Remove(tmpInvalid.Name())
_, err = ValidateCSV(tmpInvalid.Name())
if err == nil {
t.Error("ValidateCSV: expected error for inconsistent columns, got nil")
}
// Headerless CSV
tmpNoHeader, err := os.CreateTemp("", "otter_noheader_*.csv")
if err != nil {
t.Fatalf("CreateTemp error: %v", err)
}
_, _ = tmpNoHeader.WriteString("1,Alice\n2,Bob\n")
tmpNoHeader.Close()
defer os.Remove(tmpNoHeader.Name())
dfNoHeader, err := ReadCSVWithOptions(tmpNoHeader.Name(), CSVOptions{HasHeader: false, Delimiter: ','})
if err != nil {
t.Fatalf("ReadCSVWithOptions headerless error: %v", err)
}
nhRows, nhCols := dfNoHeader.Shape()
if nhRows != 2 || nhCols != 2 {
t.Errorf("Headerless CSV: got shape (%d, %d), want (2, 2)", nhRows, nhCols)
}
// MaxRows option
tmpMaxRows, err := os.CreateTemp("", "otter_maxrows_*.csv")
if err != nil {
t.Fatalf("CreateTemp error: %v", err)
}
_, _ = tmpMaxRows.WriteString("id,name\n1,Alice\n2,Bob\n3,Carol\n4,Dave\n")
tmpMaxRows.Close()
defer os.Remove(tmpMaxRows.Name())
dfMax, err := ReadCSVWithOptions(tmpMaxRows.Name(), CSVOptions{HasHeader: true, Delimiter: ',', MaxRows: 2})
if err != nil {
t.Fatalf("ReadCSVWithOptions MaxRows error: %v", err)
}
maxRows, _ := dfMax.Shape()
if maxRows != 2 {
t.Errorf("MaxRows: got %d rows, want 2", maxRows)
}
}