-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_test.go
More file actions
108 lines (87 loc) · 2.47 KB
/
stream_test.go
File metadata and controls
108 lines (87 loc) · 2.47 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
package fileprep
import (
"io"
"testing"
"github.com/nao1215/fileparser"
)
func TestStream_Format(t *testing.T) {
t.Parallel()
tests := []struct {
name string
outputFormat fileparser.FileType
originalFormat fileparser.FileType
wantFormat fileparser.FileType
}{
{"CSV", fileparser.CSV, fileparser.CSV, fileparser.CSV},
{"CSV gzip returns CSV", fileparser.CSV, fileparser.CSVGZ, fileparser.CSV},
{"TSV bzip2 returns TSV", fileparser.TSV, fileparser.TSVBZ2, fileparser.TSV},
{"XLSX outputs as CSV", fileparser.CSV, fileparser.XLSXZSTD, fileparser.CSV},
{"Parquet outputs as CSV", fileparser.CSV, fileparser.Parquet, fileparser.CSV},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
s := newStream([]byte("test data"), tt.outputFormat, tt.originalFormat)
if got := s.Format(); got != tt.wantFormat {
t.Errorf("Format() = %v, want %v", got, tt.wantFormat)
}
if got := s.OriginalFormat(); got != tt.originalFormat {
t.Errorf("OriginalFormat() = %v, want %v", got, tt.originalFormat)
}
})
}
}
func TestStream_Read(t *testing.T) {
t.Parallel()
data := []byte("hello, world")
s := newStream(data, fileparser.CSV, fileparser.CSV)
// Read all data
result, err := io.ReadAll(s)
if err != nil {
t.Fatalf("Read() error = %v", err)
}
if string(result) != string(data) {
t.Errorf("Read() = %q, want %q", result, data)
}
}
func TestStream_Seek(t *testing.T) {
t.Parallel()
data := []byte("hello, world")
s := newStream(data, fileparser.CSV, fileparser.CSV)
// Read all
if _, err := io.ReadAll(s); err != nil {
t.Fatalf("ReadAll() error = %v", err)
}
// Seek to beginning
pos, err := s.Seek(0, io.SeekStart)
if err != nil {
t.Fatalf("Seek() error = %v", err)
}
if pos != 0 {
t.Errorf("Seek() pos = %d, want 0", pos)
}
// Read again
result, err := io.ReadAll(s)
if err != nil {
t.Fatalf("ReadAll() after Seek error = %v", err)
}
if string(result) != string(data) {
t.Errorf("After Seek, Read() = %q, want %q", result, data)
}
}
func TestStream_Len(t *testing.T) {
t.Parallel()
data := []byte("hello")
s := newStream(data, fileparser.CSV, fileparser.CSV)
if got := s.Len(); got != len(data) {
t.Errorf("Len() = %d, want %d", got, len(data))
}
// Read some bytes
buf := make([]byte, 2)
if _, err := s.Read(buf); err != nil {
t.Fatalf("Read() error = %v", err)
}
if got := s.Len(); got != len(data)-2 {
t.Errorf("After read, Len() = %d, want %d", got, len(data)-2)
}
}