-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvprocessor_test.go
278 lines (259 loc) · 6.71 KB
/
csvprocessor_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
// Package csvprocessor can be used to efficiently transform and split CSV files.
//
// See README.md for more details
package csvprocessor_test
import (
"encoding/csv"
"io"
"strings"
"testing"
"github.com/sivaramasubramanian/csvprocessor"
)
const verySmallCSV = `a,b,c
d,e,f
g,h,i
j,k,l
`
// noOpLogger is used for disabling logging when running benchmarks.
var noOpLogger csvprocessor.Logger = func(string, ...any) {
}
type args struct {
reader io.Reader
opt []csvprocessor.Option
expectedChunks int
}
type expect struct {
wantErr bool
bufferLength int
elementLength int
}
func TestProcessor_Process(t *testing.T) {
tests := []struct {
name string
args args
expect expect
}{
{
name: "Test With Headers - verySmallCSV",
args: args{
reader: strings.NewReader(verySmallCSV),
expectedChunks: 3,
opt: []csvprocessor.Option{
csvprocessor.WithLogger(t.Logf),
},
},
expect: expect{
wantErr: false,
bufferLength: 3,
elementLength: 12,
},
},
{
name: "Test Without Headers - verySmallCSV",
args: args{
reader: strings.NewReader(verySmallCSV),
expectedChunks: 4,
opt: []csvprocessor.Option{
csvprocessor.SkipHeaders(true),
csvprocessor.WithLogger(t.Logf),
},
},
expect: expect{
wantErr: false,
bufferLength: 4,
elementLength: 6,
},
},
{
name: "Test Without Headers - smallCSV",
args: args{
reader: strings.NewReader(strings.Repeat(verySmallCSV, 100)),
expectedChunks: 4 * 100,
opt: []csvprocessor.Option{
csvprocessor.SkipHeaders(true),
csvprocessor.WithLogger(t.Logf),
},
},
expect: expect{
wantErr: false,
bufferLength: 400,
elementLength: 6,
},
},
{
name: "Test - smallCSV - chunk 10",
args: args{
reader: strings.NewReader(strings.Repeat(verySmallCSV, 100)),
// 400 lines = 1 header + 399 lines
expectedChunks: 399 / 3,
opt: []csvprocessor.Option{
csvprocessor.SkipHeaders(false),
csvprocessor.WithLogger(t.Logf),
csvprocessor.WithChunkSize(3),
},
},
expect: expect{
wantErr: false,
bufferLength: 399 / 3,
elementLength: (6 * 3) + 6,
},
},
{
name: "Test Without Headers - smallCSV - chunk 10",
args: args{
reader: strings.NewReader(strings.Repeat(verySmallCSV, 100)),
// 400 lines = 0 header + 400 lines
expectedChunks: ((4 * 100) / 10),
opt: []csvprocessor.Option{
csvprocessor.SkipHeaders(true),
csvprocessor.WithLogger(t.Logf),
csvprocessor.WithChunkSize(10),
},
},
expect: expect{
wantErr: false,
bufferLength: ((4 * 100) / 10),
elementLength: 6 * 10,
},
},
{
name: "Test With Headers - verySmallCSV - with transformer",
args: args{
reader: strings.NewReader(verySmallCSV),
expectedChunks: 3,
opt: []csvprocessor.Option{
csvprocessor.WithLogger(t.Logf),
csvprocessor.WithTransformer(
csvprocessor.AddChunkRowNoTransformer("id"),
),
},
},
expect: expect{
wantErr: false,
bufferLength: 3,
// existing content + id header + id value
elementLength: (6 * 2) + (2 + 1) + (1 + 1),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var buffer = make([]strings.Builder, tt.args.expectedChunks)
proc := newProcessor(t, tt.args.reader, buffer, tt.args.opt...)
if err := proc.Process(); (err != nil) != tt.expect.wantErr {
t.Errorf("Processor.Process() error = %v, wantErr %v", err, tt.expect.wantErr)
}
if len(buffer) != tt.expect.bufferLength {
t.Errorf("Processor.Process() bufferLength = %v, expect %v", len(buffer), tt.expect.bufferLength)
}
for index, element := range buffer {
if element.Len() != tt.expect.elementLength {
t.Errorf("Processor.Process() index = %v, elementLength = %v, expect %v", index, element.Len(), tt.expect.elementLength)
}
}
})
}
}
func BenchmarkProcessor(b *testing.B) {
csv4mRows := strings.NewReader(strings.Repeat(verySmallCSV, 1_000_000))
benches := []struct {
name string
args args
expect expect
}{
{
name: "BenchmarkProcessor_Process_4000_rows",
args: args{
// 4 * 1000
reader: strings.NewReader(strings.Repeat(verySmallCSV, 1000)),
expectedChunks: ((4 * 1000) / 10),
opt: []csvprocessor.Option{
csvprocessor.WithLogger(noOpLogger),
csvprocessor.WithChunkSize(10),
},
},
expect: expect{
wantErr: false,
},
},
{
name: "BenchmarkProcessor_Process_4000_rows_cz_100",
args: args{
reader: strings.NewReader(strings.Repeat(verySmallCSV, 1000)),
expectedChunks: ((4 * 1000) / 100),
opt: []csvprocessor.Option{
csvprocessor.WithLogger(noOpLogger),
csvprocessor.WithChunkSize(100),
},
},
expect: expect{
wantErr: false,
},
},
{
name: "BenchmarkProcessor_ProcessWithoutHeader_4000_rows",
args: args{
reader: strings.NewReader(strings.Repeat(verySmallCSV, 1000)),
expectedChunks: ((4 * 1000) / 10),
opt: []csvprocessor.Option{
csvprocessor.WithLogger(noOpLogger),
csvprocessor.WithChunkSize(10),
csvprocessor.SkipHeaders(true),
},
},
expect: expect{
wantErr: false,
},
},
{
name: "BenchmarkProcessor_ProcessWithoutHeader_4_000_000_rows_cz_100",
args: args{
reader: csv4mRows,
expectedChunks: ((4 * 1_000_000) / 1000),
opt: []csvprocessor.Option{
csvprocessor.WithLogger(noOpLogger),
csvprocessor.WithChunkSize(1000),
csvprocessor.SkipHeaders(true),
},
},
expect: expect{
wantErr: false,
},
},
}
for _, bench := range benches {
b.Run(bench.name, func(b *testing.B) {
runBenchmark(b, bench.args, bench.expect.wantErr)
})
}
}
func runBenchmark(b *testing.B, arg args, wantErr bool) {
b.Helper()
for i := 0; i < b.N; i++ {
var buffer = make([]strings.Builder, arg.expectedChunks)
proc := newProcessor(b, arg.reader, buffer, arg.opt...)
if err := proc.Process(); (err != nil) != wantErr {
b.Errorf(" error = %v, wantErr %v", err, wantErr)
}
}
}
// Util Functions for test.
func newProcessor(tb testing.TB, reader io.Reader, bytesArr []strings.Builder, opt ...csvprocessor.Option) *csvprocessor.Processor {
tb.Helper()
bufferOpt := []csvprocessor.Option{
csvprocessor.WithReader(csv.NewReader(reader)),
csvprocessor.WithWriterGenerator(func(i int) (io.WriteCloser, error) {
return csvprocessor.NoOpCloser(&bytesArr[i-1]), nil
}),
csvprocessor.WithChunkSize(1),
}
bufferOpt = append(bufferOpt, opt...)
c, err := csvprocessor.New(
bufferOpt...,
)
if err != nil {
tb.Errorf("Processor.Process() buffer creation error = %v", err)
}
return c
}
type any = interface{} //nolint:predeclared