forked from geckoboard/sql-dataset
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
316 lines (297 loc) · 9.6 KB
/
integration_test.go
File metadata and controls
316 lines (297 loc) · 9.6 KB
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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"path/filepath"
"strings"
"testing"
"github.com/geckoboard/sql-dataset/models"
)
var originalBatchRows = 500
type GBRequest struct {
Path string
Body string
}
func TestEndToEndFlow(t *testing.T) {
testCases := []struct {
config models.Config
maxRows int
expectError bool
gbHits int
gbReqs []GBRequest
}{
{
config: models.Config{
DatabaseConfig: &models.DatabaseConfig{
Driver: models.SQLiteDriver,
URL: filepath.Join("models", "fixtures", "db.sqlite"),
},
Datasets: []models.Dataset{
{
Name: "app.counts",
SQL: "SELECT app_name, count(*) FROM builds GROUP BY app_name order by app_name",
UpdateType: models.Append,
Fields: []models.Field{
{Name: "App", Type: models.StringType},
{Name: "Build Count", Type: models.NumberType},
},
},
{
Name: "app.build.costs",
UpdateType: models.Append,
SQL: "SELECT app_name, CAST(build_cost*100 AS INTEGER) FROM builds GROUP BY app_name order by app_name",
Fields: []models.Field{
{Name: "App", Type: models.StringType},
{Name: "Build Cost", Type: models.MoneyType, CurrencyCode: "USD"},
},
},
},
},
gbReqs: []GBRequest{
{
Path: "/datasets/app.counts",
Body: `{"id":"app.counts","fields":{"app":{"type":"string","name":"App"},"build_count":{"type":"number","name":"Build Count"}}}`,
},
{
Path: "/datasets/app.counts/data",
Body: `{"data":[{"app":"","build_count":2},{"app":"everdeen","build_count":2},{"app":"geckoboard-ruby","build_count":3},{"app":"react","build_count":1},{"app":"westworld","build_count":1}]}`,
},
{
Path: "/datasets/app.build.costs",
Body: `{"id":"app.build.costs","fields":{"app":{"type":"string","name":"App"},"build_cost":{"type":"money","name":"Build Cost","currency_code":"USD"}}}`,
},
{
Path: "/datasets/app.build.costs/data",
Body: `{"data":[{"app":"","build_cost":1132},{"app":"everdeen","build_cost":144},{"app":"geckoboard-ruby","build_cost":0},{"app":"react","build_cost":111},{"app":"westworld","build_cost":264}]}`,
},
},
},
{
// Replace update type sends the first batch only
config: models.Config{
DatabaseConfig: &models.DatabaseConfig{
Driver: models.SQLiteDriver,
URL: filepath.Join("models", "fixtures", "db.sqlite"),
},
Datasets: []models.Dataset{
{
Name: "apps.run.time",
SQL: "SELECT app_name, run_time FROM builds ORDER BY app_name",
UpdateType: models.Replace,
Fields: []models.Field{
{Name: "App", Type: models.StringType},
{Name: "Run time", Type: models.NumberType},
},
},
},
},
maxRows: 4,
expectError: true,
gbReqs: []GBRequest{
{
Path: "/datasets/apps.run.time",
Body: `{"id":"apps.run.time","fields":{"app":{"type":"string","name":"App"},"run_time":{"type":"number","name":"Run time"}}}`,
},
{
Path: "/datasets/apps.run.time/data",
Body: `{"data":[{"app":"","run_time":0.12349876543},{"app":"","run_time":46.432763287},{"app":"everdeen","run_time":0.31882276212},{"app":"everdeen","run_time":144.31838122382}]}`,
},
},
},
{
// Append update type sends multiple requests in batches of batchRow limit when more rows exist
config: models.Config{
DatabaseConfig: &models.DatabaseConfig{
Driver: models.SQLiteDriver,
URL: filepath.Join("models", "fixtures", "db.sqlite"),
},
Datasets: []models.Dataset{
{
Name: "apps.run.time",
SQL: "SELECT app_name, run_time FROM builds ORDER BY app_name",
UpdateType: models.Append,
Fields: []models.Field{
{Name: "App", Type: models.StringType},
{Name: "Run time", Type: models.NumberType},
},
},
},
},
maxRows: 4,
gbReqs: []GBRequest{
{
Path: "/datasets/apps.run.time",
Body: `{"id":"apps.run.time","fields":{"app":{"type":"string","name":"App"},"run_time":{"type":"number","name":"Run time"}}}`,
},
{
Path: "/datasets/apps.run.time/data",
Body: `{"data":[{"app":"","run_time":0.12349876543},{"app":"","run_time":46.432763287},{"app":"everdeen","run_time":0.31882276212},{"app":"everdeen","run_time":144.31838122382}]}`,
},
{
Path: "/datasets/apps.run.time/data",
Body: `{"data":[{"app":"geckoboard-ruby","run_time":0.21882232124},{"app":"geckoboard-ruby","run_time":77.21381276421},{"app":"geckoboard-ruby","run_time":0},{"app":"react","run_time":118.18382961212}]}`,
},
{
Path: "/datasets/apps.run.time/data",
Body: `{"data":[{"app":"westworld","run_time":321.93774373}]}`,
},
},
},
{
// Unique by correctly used and sent - doesn't do validation used with the correct update type
config: models.Config{
DatabaseConfig: &models.DatabaseConfig{
Driver: models.SQLiteDriver,
URL: filepath.Join("models", "fixtures", "db.sqlite"),
},
Datasets: []models.Dataset{
{
Name: "app.counts",
SQL: "SELECT app_name, count(*) FROM builds GROUP BY app_name order by app_name",
UpdateType: models.Append,
UniqueBy: []string{"app"},
Fields: []models.Field{
{Name: "App", Type: models.StringType},
{Name: "Build Count", Type: models.NumberType},
},
},
},
},
gbReqs: []GBRequest{
{
Path: "/datasets/app.counts",
Body: `{"id":"app.counts","unique_by":["app"],"fields":{"app":{"type":"string","name":"App"},"build_count":{"type":"number","name":"Build Count"}}}`,
},
{
Path: "/datasets/app.counts/data",
Body: `{"data":[{"app":"","build_count":2},{"app":"everdeen","build_count":2},{"app":"geckoboard-ruby","build_count":3},{"app":"react","build_count":1},{"app":"westworld","build_count":1}]}`,
},
},
},
{
// Unique by without a matching field errors makes no requests
config: models.Config{
DatabaseConfig: &models.DatabaseConfig{
Driver: models.SQLiteDriver,
URL: filepath.Join("models", "fixtures", "db.sqlite"),
},
Datasets: []models.Dataset{
{
Name: "app.counts",
SQL: "SELECT app_name, count(*) FROM builds GROUP BY app_name order by app_name",
UpdateType: models.Append,
UniqueBy: []string{"app_name"},
Fields: []models.Field{
{Name: "App", Type: models.StringType},
{Name: "Build Count", Type: models.NumberType},
},
},
},
},
gbReqs: []GBRequest{},
expectError: true,
},
{
// Optional field correctly sent as null
config: models.Config{
DatabaseConfig: &models.DatabaseConfig{
Driver: models.SQLiteDriver,
URL: filepath.Join("models", "fixtures", "db.sqlite"),
},
Datasets: []models.Dataset{
{
Name: "app.counts",
SQL: `SELECT "test", null FROM builds limit 1`,
UpdateType: models.Append,
Fields: []models.Field{
{Name: "App", Type: models.StringType},
{Name: "Build Count", Type: models.NumberType, Optional: true},
},
},
},
},
gbReqs: []GBRequest{
{
Path: "/datasets/app.counts",
Body: `{"id":"app.counts","fields":{"app":{"type":"string","name":"App"},"build_count":{"type":"number","name":"Build Count","optional":true}}}`,
},
{
Path: "/datasets/app.counts/data",
Body: `{"data":[{"app":"test","build_count":null}]}`,
},
},
},
{
// No data rows retrieved - so should send {'data': []} when type replace
config: models.Config{
DatabaseConfig: &models.DatabaseConfig{
Driver: models.SQLiteDriver,
URL: filepath.Join("models", "fixtures", "db.sqlite"),
},
Datasets: []models.Dataset{
{
Name: "empty.sql.rows",
SQL: `SELECT "test", null FROM builds WHERE id < 0`,
UpdateType: models.Replace,
Fields: []models.Field{
{Name: "App", Type: models.StringType},
{Name: "Build Count", Type: models.NumberType, Optional: true},
},
},
},
},
gbReqs: []GBRequest{
{
Path: "/datasets/empty.sql.rows",
Body: `{"id":"empty.sql.rows","fields":{"app":{"type":"string","name":"App"},"build_count":{"type":"number","name":"Build Count","optional":true}}}`,
},
{
Path: "/datasets/empty.sql.rows/data",
Body: `{"data":[]}`,
},
},
},
}
for i, tc := range testCases {
maxRows = originalBatchRows
if tc.maxRows != 0 {
maxRows = tc.maxRows
}
gbWS := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tc.gbHits++
if tc.gbHits-1 >= len(tc.gbReqs) {
t.Errorf("[%d] Got unexpected extra request for geckoboard unable to process", i)
return
}
tcReq := tc.gbReqs[tc.gbHits-1]
if tcReq.Path != r.URL.Path {
t.Errorf("[%d] Expected geckoboard request path %s but got %s", i, tcReq.Path, r.URL.Path)
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Fatal("Failed to consume body", err)
}
if strings.TrimSpace(string(b)) != tcReq.Body {
t.Errorf("[%d] Expected geckoboard request body %s but got %s", i, tcReq.Body, string(b))
}
fmt.Fprintf(w, `{}`)
}))
client := NewClient("fakeKey")
dc := tc.config.DatabaseConfig
db, err := newDBConnection(dc.Driver, dc.URL)
if err != nil {
t.Fatal(err)
}
gbHost = gbWS.URL
bol := processAllDatasets(&tc.config, client, db)
if tc.expectError != bol {
t.Errorf("[%d] Expected hasErrors to be %t but got %t", i, tc.expectError, bol)
}
if tc.gbHits != len(tc.gbReqs) {
t.Errorf("Expected %d requests but got %d", len(tc.gbReqs), tc.gbHits)
}
gbWS.Close()
}
}