-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcsv_test.go
More file actions
523 lines (456 loc) · 13 KB
/
csv_test.go
File metadata and controls
523 lines (456 loc) · 13 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
package otters
import (
"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]interface{}{
"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]interface{}{
"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]interface{}{
"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)
}
}