forked from segmentio/go-athena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rows_test.go
237 lines (220 loc) · 5.4 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
package athena
import (
"database/sql/driver"
"errors"
"io"
"math/rand"
"testing"
"github.com/aws/aws-sdk-go/service/athena"
"github.com/aws/aws-sdk-go/service/athena/athenaiface"
"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) *athena.ColumnInfo {
caseSensitive := true
catalogName := "hive"
nullable := "UNKNOWN"
precision := int64(2147483647)
scale := int64(0)
schemaName := ""
tableName := ""
columnType := "varchar"
return &athena.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 []*athena.ColumnInfo) *athena.Row {
var data []*athena.Datum
for i := 0; i < len(columns); i++ {
if isHeader {
data = append(data, &athena.Datum{
VarCharValue: columns[i].Name,
})
} else {
s := randomString()
data = append(data, &athena.Datum{
VarCharValue: &s,
})
}
}
return &athena.Row{
Data: data,
}
}
func dummySelectQueryResponse(token string) (*athena.GetQueryResultsOutput, error) {
switch token {
case "":
var nextToken = "page_1"
columns := []*athena.ColumnInfo{
genColumnInfo("first_name"),
genColumnInfo("last_name"),
}
return &athena.GetQueryResultsOutput{
NextToken: &nextToken,
ResultSet: &athena.ResultSet{
ResultSetMetadata: &athena.ResultSetMetadata{
ColumnInfo: columns,
},
Rows: []*athena.Row{
genRow(true, columns),
genRow(false, columns),
genRow(false, columns),
genRow(false, columns),
genRow(false, columns),
},
},
}, nil
case "page_1":
columns := []*athena.ColumnInfo{
genColumnInfo("first_name"),
genColumnInfo("last_name"),
}
return &athena.GetQueryResultsOutput{
ResultSet: &athena.ResultSet{
ResultSetMetadata: &athena.ResultSetMetadata{
ColumnInfo: columns,
},
Rows: []*athena.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 := []*athena.ColumnInfo{
genColumnInfo("partition"),
}
return &athena.GetQueryResultsOutput{
ResultSet: &athena.ResultSet{
ResultSetMetadata: &athena.ResultSetMetadata{
ColumnInfo: columns,
},
Rows: []*athena.Row{
genRow(false, columns),
genRow(false, columns),
},
},
}, nil
}
func dummyFailedIterationResponse(token string) (*athena.GetQueryResultsOutput, error) {
switch token {
case "":
var nextToken = "page_1"
columns := []*athena.ColumnInfo{
genColumnInfo("first_name"),
genColumnInfo("last_name"),
}
return &athena.GetQueryResultsOutput{
NextToken: &nextToken,
ResultSet: &athena.ResultSet{
ResultSetMetadata: &athena.ResultSetMetadata{
ColumnInfo: columns,
},
Rows: []*athena.Row{
genRow(true, columns),
genRow(false, columns),
genRow(false, columns),
genRow(false, columns),
genRow(false, columns),
},
},
}, nil
default:
return nil, dummyError
}
}
type mockAthenaClient struct {
athenaiface.AthenaAPI
}
func (m *mockAthenaClient) GetQueryResults(query *athena.GetQueryResultsInput) (*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,
},
}
for _, test := range tests {
r, _ := newRows(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)
}
}
}