-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_test.go
153 lines (119 loc) · 3.41 KB
/
handler_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
package bqloader
import (
"bytes"
"context"
"fmt"
"strings"
"testing"
)
func Test_Handler_WithSkipping(t *testing.T) {
t.Parallel()
projector := func(_ context.Context, r []string) ([]string, error) {
if r[0] == "" {
return nil, nil
}
return r, nil
}
rawCSV := `123,456,789
,foo bar,123
234,567,890`
src := bytes.NewBufferString(rawCSV)
tl := newTestLoader()
handler := &Handler{
Name: "test-handler",
Parser: CSVParser(),
Projector: projector,
BatchSize: defaultBatchSize,
Extractor: newTestExtractor(),
Loader: tl,
semaphore: make(chan struct{}, 1),
}
e := Event{Name: "test/name", Bucket: "bucket", source: src}
err := handler.Handle(context.Background(), e)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
res := tl.(*testLoader)
if len(res.result) != 2 {
t.Fatalf("Size of result records should be 2, but %d", len(res.result))
}
if len(res.result[0]) != 3 {
t.Fatalf("Size of each record be 3, but %d", len(res.result[0]))
}
if res.result[0][0] != "123" {
t.Errorf(`results[0][0] should be "123", but "%s"`, res.result[0][0])
}
if res.result[0][1] != "456" {
t.Errorf(`results[0][1] should be "456", but "%s"`, res.result[0][1])
}
if res.result[0][2] != "789" {
t.Errorf(`results[0][2] should be "789", but "%s"`, res.result[0][2])
}
if len(res.result[1]) != 3 {
t.Fatalf("Size of each record be 3, but %d", len(res.result[1]))
}
if res.result[1][0] != "234" {
t.Errorf(`results[1][0] should be "234", but "%s"`, res.result[1][0])
}
if res.result[1][1] != "567" {
t.Errorf(`results[1][1] should be "567", but "%s"`, res.result[1][1])
}
if res.result[1][2] != "890" {
t.Errorf(`results[1][2] should be "890", but "%s"`, res.result[1][2])
}
}
func Test_Handler_WithPreprocessor(t *testing.T) {
t.Parallel()
projector := func(ctx context.Context, r []string) ([]string, error) {
prefix, ok := ctx.Value("prefix").(string)
if !ok {
return nil, fmt.Errorf("prefix is not found")
}
row := make([]string, 4)
row[0] = prefix
row[1] = r[0]
row[2] = r[1]
row[3] = r[2]
return row, nil
}
preprocessor := func(ctx context.Context, e Event) (context.Context, error) {
prefix := strings.Split(e.Name, "/")[0]
return context.WithValue(ctx, "prefix", prefix), nil
}
src := bytes.NewBufferString(`123,456,789`)
tl := newTestLoader()
handler := &Handler{
Name: "test-handler",
Parser: CSVParser(),
Projector: projector,
Preprocessor: preprocessor,
BatchSize: defaultBatchSize,
Extractor: newTestExtractor(),
Loader: tl,
semaphore: make(chan struct{}, 1),
}
e := Event{Name: "test/name", Bucket: "bucket", source: src}
err := handler.Handle(context.Background(), e)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
res := tl.(*testLoader)
if len(res.result) != 1 {
t.Fatalf("Size of result records should be 1, but %d", len(res.result))
}
if len(res.result[0]) != 4 {
t.Fatalf("Size of each record be 4, but %d", len(res.result[0]))
}
if res.result[0][0] != "test" {
t.Errorf(`results[0][0] should be "test", but "%s"`, res.result[0][0])
}
if res.result[0][1] != "123" {
t.Errorf(`results[0][0] should be "123", but "%s"`, res.result[0][1])
}
if res.result[0][2] != "456" {
t.Errorf(`results[0][1] should be "456", but "%s"`, res.result[0][2])
}
if res.result[0][3] != "789" {
t.Errorf(`results[0][2] should be "789", but "%s"`, res.result[0][3])
}
}