-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_service_test.go
377 lines (320 loc) · 11.1 KB
/
dataset_service_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
package geckoboard
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"gotest.tools/v3/assert"
)
func TestDatasetService_FindOrCreate(t *testing.T) {
t.Run("successfully creates dataset", func(t *testing.T) {
want := &Dataset{
Name: "bullhorn-test",
Fields: map[string]Field{
"id": {
Name: "ID",
Type: StringType,
Optional: false,
},
"created_at": {
Name: "Created at",
Type: DatetimeType,
Optional: true,
},
"index": {
Name: "Index",
Type: NumberType,
Optional: true,
},
"money": {
Name: "Money",
Type: MoneyType,
CurrencyCode: "USD",
Optional: true,
},
"duration": {
Name: "Time taken",
Type: MoneyType,
TimeUnit: Hours,
Optional: true,
},
"percent": {
Name: "Completed %",
Type: PercentType,
},
},
UniqueBy: []string{"id"},
}
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Header.Get("Content-Type"), "application/json")
assert.Equal(t, r.Header.Get("Authorization"), "Basic a2V5LTQ0NDo=")
payload := map[string]any{}
assert.NilError(t, json.NewDecoder(r.Body).Decode(&payload))
// Validate keys omitted that shouldn't exist
assert.DeepEqual(t, payload, map[string]any{
"fields": map[string]any{
"created_at": map[string]any{
"name": "Created at",
"optional": true,
"type": "datetime",
},
"duration": map[string]any{
"name": "Time taken",
"optional": true,
"time_unit": "hours",
"type": "money",
},
"id": map[string]any{
"name": "ID",
"optional": false,
"type": "string",
},
"money": map[string]any{
"currency_code": "USD",
"name": "Money",
"optional": true,
"type": "money",
},
"index": map[string]any{
"name": "Index",
"optional": true,
"type": "number",
},
"percent": map[string]any{
"name": "Completed %",
"optional": false,
"type": "percentage",
},
},
"id": "bullhorn-test",
"unique_by": []any{"id"},
})
w.WriteHeader(http.StatusOK)
})
defer server.Close()
ds := newService(server.URL)
assert.NilError(t, ds.FindOrCreate(context.Background(), want))
})
t.Run("returns error when marshaling body fails", func(t *testing.T) {
ds := &datasetService{
client: NewWithURL("key-444", ""),
jsonMarshalFn: func(any) ([]byte, error) {
return nil, errors.New("marshal error")
},
}
err := ds.FindOrCreate(context.Background(), &Dataset{})
assert.ErrorContains(t, err, "marshal error")
})
t.Run("returns error with invalid url", func(t *testing.T) {
ds := newService(string([]byte{0x7f}))
err := ds.FindOrCreate(context.Background(), &Dataset{})
assert.ErrorContains(t, err, "invalid control character in URL")
})
t.Run("returns error when request fails", func(t *testing.T) {
err := newService("").FindOrCreate(context.Background(), &Dataset{})
assert.ErrorContains(t, err, "unsupported protocol scheme")
})
t.Run("returns error when response 500", func(t *testing.T) {
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
})
defer server.Close()
err := newService(server.URL).FindOrCreate(context.Background(), &Dataset{})
assert.Error(t, err, errUnexpectedResponse.Error())
})
t.Run("returns geckoboard error when response 400", func(t *testing.T) {
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error":{"message": "invalid field type"}}`)
})
defer server.Close()
err := newService(server.URL).FindOrCreate(context.Background(), &Dataset{})
assert.DeepEqual(t, err, &Error{
StatusCode: http.StatusBadRequest,
Detail: Detail{
Message: "invalid field type",
},
})
})
t.Run("returns err when it fail to parse error json", func(t *testing.T) {
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{invalid json}`)
})
defer server.Close()
err := newService(server.URL).FindOrCreate(context.Background(), &Dataset{})
assert.ErrorType(t, err, &json.SyntaxError{})
})
}
func TestDatasetService_AppendData(t *testing.T) {
t.Run("makes a single data request", func(t *testing.T) {
var requests int
wantData := Data{
{
"id": "1234",
"title": "My title",
"created_at": "2022-05-10T11:12:13Z",
},
}
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, http.MethodPost)
assert.Equal(t, r.Header.Get("Content-Type"), "application/json")
assert.Equal(t, r.Header.Get("Authorization"), "Basic a2V5LTQ0NDo=")
requests += 1
got := &DataPayload{}
if err := json.NewDecoder(r.Body).Decode(got); err != nil {
t.Fatal(err)
}
assert.DeepEqual(t, got, &DataPayload{Data: wantData})
w.WriteHeader(http.StatusNoContent)
})
defer server.Close()
ds := newService(server.URL)
ds.maxRecordsPerReq = 500
err := ds.AppendData(context.Background(), &Dataset{Name: "test-dataset"}, wantData)
assert.NilError(t, err)
assert.Equal(t, requests, 1)
})
t.Run("makes a multiple data requests", func(t *testing.T) {
var requests int
wantData := Data{
{"id": "3333", "title": "title one", "created_at": "2022-05-10T11:12:13Z"},
{"id": "2222", "title": "title two", "created_at": "2022-02-10T11:12:13Z"},
{"id": "1111", "title": "title three", "created_at": "2022-01-10T11:12:13Z"},
{"id": "750", "title": "title four", "created_at": "2021-11-22T11:12:13Z"},
{"id": "555", "title": "title five", "created_at": "2021-12-15T11:12:13Z"},
}
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Header.Get("Content-Type"), "application/json")
assert.Equal(t, r.Header.Get("Authorization"), "Basic a2V5LTQ0NDo=")
requests += 1
got := &DataPayload{}
if err := json.NewDecoder(r.Body).Decode(got); err != nil {
t.Fatal(err)
}
switch requests {
case 1:
assert.DeepEqual(t, got, &DataPayload{Data: wantData[0:2]})
case 2:
assert.DeepEqual(t, got, &DataPayload{Data: wantData[2:4]})
case 3:
assert.DeepEqual(t, got, &DataPayload{Data: wantData[4:]})
}
w.WriteHeader(http.StatusNoContent)
})
defer server.Close()
ds := newService(server.URL)
ds.maxRecordsPerReq = 2
ctx := context.Background()
err := ds.AppendData(ctx, &Dataset{Name: "test-dataset"}, wantData)
assert.NilError(t, err)
assert.Equal(t, requests, 3)
})
t.Run("returns error when request body marshal fails", func(t *testing.T) {
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {})
defer server.Close()
ds := newService(server.URL)
ds.jsonMarshalFn = func(any) ([]byte, error) {
return nil, errors.New("marshal error")
}
ds.maxRecordsPerReq = 2
ctx := context.Background()
err := ds.AppendData(ctx, &Dataset{Name: "test-dataset"}, Data{{}})
assert.ErrorContains(t, err, "marshal error")
})
t.Run("returns error when building the request fails", func(t *testing.T) {
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {})
defer server.Close()
ctx := context.Background()
ds := newService(string([]byte{0x7f}))
ds.maxRecordsPerReq = 1
err := ds.AppendData(ctx, &Dataset{Name: "test-dataset"}, Data{{}, {}})
assert.ErrorContains(t, err, "invalid control character in URL")
})
}
func TestDatasetService_ReplaceData(t *testing.T) {
t.Run("makes a single data request of the first max records per request", func(t *testing.T) {
var requests int
dataIn := Data{
{"id": "123", "title": "My title 1", "created_at": "2022-05-10T11:12:13Z"},
{"id": "345", "title": "My title 2", "created_at": "2022-05-10T11:12:13Z"},
{"id": "567", "title": "My title 3", "created_at": "2022-05-10T11:12:13Z"},
{"id": "789", "title": "My title 4", "created_at": "2022-05-10T11:12:13Z"},
}
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Method, http.MethodPut)
assert.Equal(t, r.Header.Get("Content-Type"), "application/json")
assert.Equal(t, r.Header.Get("Authorization"), "Basic a2V5LTQ0NDo=")
requests += 1
got := &DataPayload{}
if err := json.NewDecoder(r.Body).Decode(got); err != nil {
t.Fatal(err)
}
assert.DeepEqual(t, got, &DataPayload{Data: dataIn[0:3]})
w.WriteHeader(http.StatusNoContent)
})
defer server.Close()
ds := newService(server.URL)
ds.maxRecordsPerReq = 3
err := ds.ReplaceData(context.Background(), &Dataset{Name: "test-dataset"}, dataIn)
assert.NilError(t, err)
assert.Equal(t, requests, 1)
})
t.Run("fetches only the maximum number of slice items possible", func(t *testing.T) {
dataIn := Data{
{"id": "123", "title": "My title 1", "created_at": "2022-05-10T11:12:13Z"},
{"id": "345", "title": "My title 2", "created_at": "2022-05-10T11:12:13Z"},
{"id": "567", "title": "My title 3", "created_at": "2022-05-10T11:12:13Z"},
{"id": "789", "title": "My title 4", "created_at": "2022-05-10T11:12:13Z"},
}
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {
got := &DataPayload{}
if err := json.NewDecoder(r.Body).Decode(got); err != nil {
t.Fatal(err)
}
assert.DeepEqual(t, got, &DataPayload{Data: dataIn})
w.WriteHeader(http.StatusNoContent)
})
defer server.Close()
ds := newService(server.URL)
ds.maxRecordsPerReq = 10
// This could panic if we tried to do data[0:10] so it should select [0:4] as that
// is the maximum number of records
err := ds.ReplaceData(context.Background(), &Dataset{Name: "test-dataset"}, dataIn)
assert.NilError(t, err)
})
t.Run("returns error when request body marshal fails", func(t *testing.T) {
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {})
defer server.Close()
ds := newService(server.URL)
ds.jsonMarshalFn = func(any) ([]byte, error) {
return nil, errors.New("marshal error")
}
ds.maxRecordsPerReq = 2
ctx := context.Background()
err := ds.ReplaceData(ctx, &Dataset{Name: "test-dataset"}, Data{{}})
assert.ErrorContains(t, err, "marshal error")
})
t.Run("returns error when building the request fails", func(t *testing.T) {
server := buildMockServer(func(w http.ResponseWriter, r *http.Request) {})
defer server.Close()
ctx := context.Background()
ds := newService(string([]byte{0x7f}))
ds.maxRecordsPerReq = 1
err := ds.ReplaceData(ctx, &Dataset{Name: "test-dataset"}, Data{{}, {}})
assert.ErrorContains(t, err, "invalid control character in URL")
})
}
func newService(url string) *datasetService {
return &datasetService{
client: NewWithURL("key-444", url),
jsonMarshalFn: json.Marshal,
maxRecordsPerReq: 500,
}
}
func buildMockServer(handlerFn func(w http.ResponseWriter, r *http.Request)) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(handlerFn))
}