-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathconcurrent_test.go
432 lines (410 loc) · 8.78 KB
/
concurrent_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
package gonymizer
import (
"bufio"
"github.com/stretchr/testify/require"
"io"
"reflect"
"strings"
"sync"
"sync/atomic"
"testing"
)
// MockDBMapper that has implementation of ColumnMapper function
type MockDBMapper struct{}
// mockColumnMapper to check address of in tests
var mockColumnMapper = ColumnMapper{
Processors: []ProcessorDefinition{
{Name: "ScrubString"},
},
}
// mock ColumnMapper implementation that returns nil on everything but "foo" for columnName
func (m MockDBMapper) ColumnMapper(schemaName, tableName, columnName string) *ColumnMapper {
if columnName == "foo" {
return &mockColumnMapper
}
return nil
}
// MockReaderWriter mocks the Reader and Writer of files in one struct and has a buffer to test expected output.
type MockReaderWriter struct {
ReadBuffer []string
WriteBuffer []string
ExpectedWriteBuffer []string
Index int
}
func (rw *MockReaderWriter) ReadString(delim byte) (string, error) {
if rw.Index < len(rw.ReadBuffer) {
result := rw.ReadBuffer[rw.Index]
rw.Index++
return result, nil
}
return "", io.EOF
}
func (rw *MockReaderWriter) WriteString(input string) (int, error) {
rw.WriteBuffer = append(rw.WriteBuffer, input)
return 0, nil
}
func (rw *MockReaderWriter) CheckExpected(t *testing.T) {
require.Equal(t, rw.ExpectedWriteBuffer, rw.WriteBuffer)
}
func Test_createChunks(t *testing.T) {
type args struct {
reader *bufio.Reader
inclusive bool
maxLinesPerChunk int
}
type testCase struct {
name string
expectedChunks int32
args args
}
testData := `--
--
COPY public.foo_foo (id, name) FROM stdin;
1 A
2 B
\.
COPY public.bar_bar (id, name) FROM stdin;
1 C
2 D
\.
`
tests := []testCase{
{
name: "can chunk with no split sections",
args: args{
reader: bufio.NewReader(strings.NewReader(testData)),
inclusive: true,
maxLinesPerChunk: 6,
},
// 1. First comment till end token
// 2. First empty line till second end token
// 3. Second empty line
expectedChunks: 3,
},
{
name: "can chunk with split sections",
args: args{
reader: bufio.NewReader(strings.NewReader(testData)),
inclusive: true,
maxLinesPerChunk: 2,
},
// 1. Two comment lines
// 2. First COPY + 1 A line
// 3. 2 B lines + first end token
// 4. First empty line + second COPY
// 5. 1 C + 2 D lines
// 6. Second end token
// 7. Second empty line
expectedChunks: 7,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var (
chunkCount int32
wg sync.WaitGroup
)
var chunks = make(chan Chunk)
arg := tt.args
go createChunks(chunks, arg.reader, &wg, arg.inclusive, arg.maxLinesPerChunk)
wg.Add(1)
go func() {
defer wg.Done()
for chunk := range chunks {
atomic.AddInt32(&chunkCount, 1)
if chunk.SubChunkNumber >= 1 {
require.NotEmpty(t, chunk.ColumnNames)
require.NotEmpty(t, chunk.TableName)
require.NotEmpty(t, chunk.SchemaName)
} else {
require.Empty(t, chunk.ColumnNames)
require.Empty(t, chunk.TableName)
require.Empty(t, chunk.SchemaName)
}
}
}()
wg.Wait()
require.Equal(t, tt.expectedChunks, chunkCount)
})
}
}
func Test_getColumnMappers(t *testing.T) {
type args struct {
mapper ColumnMapperContainer
chunk Chunk
}
tests := []struct {
name string
args args
want []*ColumnMapper
wantErr bool
}{
{
name: "returns nil if Chunk has empty .ColumnNames",
args: args{
mapper: MockDBMapper{},
chunk: Chunk{
ColumnNames: make([]string, 0),
},
},
want: nil,
wantErr: false,
},
{
name: "returns error if Chunk is Inclusive but ColumnMapper is nil",
args: args{
mapper: MockDBMapper{},
chunk: Chunk{
ColumnNames: []string{"bar"},
Inclusive: true,
},
},
want: nil,
wantErr: true,
},
{
name: "returns equal number of columnMappers",
args: args{
mapper: MockDBMapper{},
chunk: Chunk{
ColumnNames: []string{"foo", "foo", "foo"},
Inclusive: true,
},
},
want: []*ColumnMapper{&mockColumnMapper, &mockColumnMapper, &mockColumnMapper},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getColumnMappers(tt.args.mapper, tt.args.chunk)
if (err != nil) != tt.wantErr {
t.Errorf("getColumnMappers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getColumnMappers() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_processRawValue(t *testing.T) {
type args struct {
rawValue string
columnName string
cmap *ColumnMapper
}
tests := []struct {
name string
args args
want string
}{
{
name: "keeps newliness intact",
args: args{
rawValue: "a\n",
columnName: "foo",
cmap: &ColumnMapper{
Processors: []ProcessorDefinition{
{Name: "Identity"},
},
},
},
want: "a\n",
},
{
name: "processes if cmap is not nil",
args: args{
rawValue: "a",
columnName: "foo",
cmap: &mockColumnMapper,
},
want: "*",
},
{
name: "keeps value if cmap is nil",
args: args{
rawValue: "a",
columnName: "foo",
cmap: nil,
},
want: "a",
},
{
name: "keeps value if raw value is \\N",
args: args{
rawValue: "\\N",
columnName: "foo",
cmap: &mockColumnMapper,
},
want: "\\N",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := processRawValue(tt.args.rawValue, tt.args.columnName, tt.args.cmap); got != tt.want {
t.Errorf("processRawValue() = %v, want %v", got, tt.want)
}
})
}
}
func Test_processRowFromChunk(t *testing.T) {
type args struct {
cmaps []*ColumnMapper
inputLine string
chunk Chunk
}
tests := []struct {
name string
args args
want string
}{
{
name: "anonymizes row correctly",
args: args{
cmaps: []*ColumnMapper{
&mockColumnMapper,
&mockColumnMapper,
},
inputLine: "aaa\tbbb\n",
chunk: Chunk{
ColumnNames: []string{"foo", "foo"},
},
},
want: "***\t***\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := processRowFromChunk(tt.args.cmaps, tt.args.inputLine, tt.args.chunk); got != tt.want {
t.Errorf("processRowFromChunk() = %v, want %v", got, tt.want)
}
})
}
}
func Test_processFromReader(t *testing.T) {
type args struct {
chunk Chunk
readerWriter MockReaderWriter
cmaps []*ColumnMapper
}
tests := []struct {
name string
args args
}{
{
name: "does not modify lines above data",
args: args{
chunk: Chunk{
DataBegins: 100,
ColumnNames: []string{"foo", "foo"},
},
readerWriter: MockReaderWriter{
ReadBuffer: []string{
"abc",
"\tdef",
"ghi\n",
},
WriteBuffer: make([]string, 0),
ExpectedWriteBuffer: []string{
"abc",
"\tdef",
"ghi\n",
},
},
cmaps: nil,
},
},
{
name: "copies end line",
args: args{
chunk: Chunk{
DataBegins: 0,
ColumnNames: []string{"foo", "foo"},
},
readerWriter: MockReaderWriter{
ReadBuffer: []string{
"\\.",
},
WriteBuffer: make([]string, 0),
ExpectedWriteBuffer: []string{
"\\.",
},
},
cmaps: nil,
},
},
{
name: "copies lines with just whitespace",
args: args{
chunk: Chunk{
DataBegins: 0,
ColumnNames: []string{"foo", "foo"},
},
readerWriter: MockReaderWriter{
ReadBuffer: []string{
" ",
},
WriteBuffer: make([]string, 0),
ExpectedWriteBuffer: []string{
" ",
},
},
cmaps: nil,
},
},
{
name: "copies lines that have no data",
args: args{
chunk: Chunk{
DataBegins: 0,
ColumnNames: nil,
},
readerWriter: MockReaderWriter{
ReadBuffer: []string{
"abc",
"\tdef",
"ghi\n",
},
WriteBuffer: make([]string, 0),
ExpectedWriteBuffer: []string{
"abc",
"\tdef",
"ghi\n",
},
},
cmaps: nil,
},
},
{
name: "processes all data from chunk",
args: args{
chunk: Chunk{
DataBegins: 0,
ColumnNames: []string{"foo", "foo"},
},
readerWriter: MockReaderWriter{
ReadBuffer: []string{
"aaa\tbbb\n",
"ccc\tddd\n",
},
WriteBuffer: make([]string, 0),
ExpectedWriteBuffer: []string{
"***\t***\n",
"***\t***\n",
},
},
cmaps: []*ColumnMapper{
&mockColumnMapper,
&mockColumnMapper,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
processFromReader(tt.args.chunk, &tt.args.readerWriter, &tt.args.readerWriter, tt.args.cmaps)
tt.args.readerWriter.CheckExpected(t)
})
}
}