-
Notifications
You must be signed in to change notification settings - Fork 64
/
rows_test.go
239 lines (222 loc) · 5.46 KB
/
rows_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
package athena
import (
"context"
"database/sql/driver"
"errors"
"io"
"math/rand"
"testing"
"github.com/aws/aws-sdk-go-v2/service/athena"
"github.com/aws/aws-sdk-go-v2/service/athena/types"
"github.com/stretchr/testify/assert"
)
var dummyError = errors.New("dummy error")
type genQueryResultsOutputByToken func(token string) (*athena.GetQueryResultsOutput, error)
var queryToResultsGenMap = map[string]genQueryResultsOutputByToken{
"select": dummySelectQueryResponse,
"show": dummyShowResponse,
"iteration_fail": dummyFailedIterationResponse,
}
func genColumnInfo(column string) types.ColumnInfo {
caseSensitive := true
catalogName := "hive"
nullable := types.ColumnNullableUnknown
precision := int32(2147483647)
scale := int32(0)
schemaName := ""
tableName := ""
columnType := "varchar"
return types.ColumnInfo{
CaseSensitive: caseSensitive,
CatalogName: &catalogName,
Nullable: nullable,
Precision: precision,
Scale: scale,
SchemaName: &schemaName,
TableName: &tableName,
Type: &columnType,
Label: &column,
Name: &column,
}
}
func randomString() string {
const alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
s := make([]byte, 10)
for i := 0; i < len(s); i++ {
s[i] = alphabet[rand.Intn(len(alphabet))]
}
return string(s)
}
func genRow(isHeader bool, columns []types.ColumnInfo) types.Row {
var data []types.Datum
for i := 0; i < len(columns); i++ {
if isHeader {
data = append(data, types.Datum{
VarCharValue: columns[i].Name,
})
} else {
s := randomString()
data = append(data, types.Datum{
VarCharValue: &s,
})
}
}
return types.Row{
Data: data,
}
}
func dummySelectQueryResponse(token string) (*athena.GetQueryResultsOutput, error) {
switch token {
case "":
var nextToken = "page_1"
columns := []types.ColumnInfo{
genColumnInfo("first_name"),
genColumnInfo("last_name"),
}
return &athena.GetQueryResultsOutput{
NextToken: &nextToken,
ResultSet: &types.ResultSet{
ResultSetMetadata: &types.ResultSetMetadata{
ColumnInfo: columns,
},
Rows: []types.Row{
genRow(true, columns),
genRow(false, columns),
genRow(false, columns),
genRow(false, columns),
genRow(false, columns),
},
},
}, nil
case "page_1":
columns := []types.ColumnInfo{
genColumnInfo("first_name"),
genColumnInfo("last_name"),
}
return &athena.GetQueryResultsOutput{
ResultSet: &types.ResultSet{
ResultSetMetadata: &types.ResultSetMetadata{
ColumnInfo: columns,
},
Rows: []types.Row{
genRow(false, columns),
genRow(false, columns),
genRow(false, columns),
genRow(false, columns),
genRow(false, columns),
},
},
}, nil
default:
return nil, dummyError
}
}
func dummyShowResponse(_ string) (*athena.GetQueryResultsOutput, error) {
columns := []types.ColumnInfo{
genColumnInfo("partition"),
}
return &athena.GetQueryResultsOutput{
ResultSet: &types.ResultSet{
ResultSetMetadata: &types.ResultSetMetadata{
ColumnInfo: columns,
},
Rows: []types.Row{
genRow(false, columns),
genRow(false, columns),
},
},
}, nil
}
func dummyFailedIterationResponse(token string) (*athena.GetQueryResultsOutput, error) {
switch token {
case "":
var nextToken = "page_1"
columns := []types.ColumnInfo{
genColumnInfo("first_name"),
genColumnInfo("last_name"),
}
return &athena.GetQueryResultsOutput{
NextToken: &nextToken,
ResultSet: &types.ResultSet{
ResultSetMetadata: &types.ResultSetMetadata{
ColumnInfo: columns,
},
Rows: []types.Row{
genRow(true, columns),
genRow(false, columns),
genRow(false, columns),
genRow(false, columns),
genRow(false, columns),
},
},
}, nil
default:
return nil, dummyError
}
}
type mockAthenaClient struct {
athenaAPI
}
func (m *mockAthenaClient) GetQueryResults(ctx context.Context, query *athena.GetQueryResultsInput, opts ...func(*athena.Options)) (*athena.GetQueryResultsOutput, error) {
var nextToken = ""
if query.NextToken != nil {
nextToken = *query.NextToken
}
return queryToResultsGenMap[*query.QueryExecutionId](nextToken)
}
func castToValue(dest ...driver.Value) []driver.Value {
return dest
}
func TestRows_Next(t *testing.T) {
tests := []struct {
desc string
queryID string
skipHeader bool
expectedResultsSize int
expectedError error
}{
{
desc: "show query, no header, 2 rows, no error",
queryID: "show",
skipHeader: false,
expectedResultsSize: 2,
expectedError: nil,
},
{
desc: "select query, header, multipage, 9 rows, no error",
queryID: "select",
skipHeader: true,
expectedResultsSize: 9,
expectedError: nil,
},
{
desc: "failed during calling next",
queryID: "iteration_fail",
skipHeader: true,
expectedError: dummyError,
},
}
ctx := context.Background()
for _, test := range tests {
r, _ := newRows(ctx, rowsConfig{
Athena: new(mockAthenaClient),
QueryID: test.queryID,
SkipHeader: test.skipHeader,
})
var firstName, lastName string
cnt := 0
for {
err := r.Next(castToValue(&firstName, &lastName))
if err != nil {
if err != io.EOF {
assert.Equal(t, test.expectedError, err)
}
break
}
cnt++
}
if test.expectedError == nil {
assert.Equal(t, test.expectedResultsSize, cnt)
}
}
}