-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
367 lines (339 loc) · 9.2 KB
/
client_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
package tfl
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
const (
appID = "APP_ID"
appKey = "APP_KEY"
)
var (
server *httptest.Server
client *TflClient
)
func getTestDataFileContents(fname string) []byte {
fpath := filepath.Join("testdata", fname)
b, err := ioutil.ReadFile(fpath)
if err != nil {
panic(err)
}
return b
}
func TflAPIClientStub() func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var resp []byte
switch r.URL.RequestURI() {
case fmt.Sprintf("/StopPoint/9100ECROYDN?app_id=%s&app_key=%s", appID, appKey):
resp = getTestDataFileContents("Should_retrieve_StopPoint_given_valid_ID.json")
case fmt.Sprintf("/StopPoint/INVALID?app_id=%s&app_key=%s", appID, appKey):
resp = getTestDataFileContents("Should_handle_response_for_invalid_ID.json")
w.WriteHeader(http.StatusNotFound)
case fmt.Sprintf("/StopPoint/Search/%s?app_id=%s&app_key=%s", "London%20Bridge", appID, appKey):
resp = getTestDataFileContents("Should_retrieve_Search_Reponses_given_valid_ID.json")
case fmt.Sprintf("/StopPoint/Search/%s?app_id=%s&app_key=%s&modes=%s", "London%20Bridge", appID, appKey, "national-rail%2Ctube"):
resp = getTestDataFileContents("Should_retrieve_filtered_Search_Reponses_given_valid_ID.json")
case fmt.Sprintf("/StopPoint/Search/Nope?app_id=%s&app_key=%s", appID, appKey):
resp = getTestDataFileContents("Should_retrieve_no_matches_for_invalid_searchTerm.json")
case fmt.Sprintf("/Journey/JourneyResults/1001089/to/1000173?app_id=%s&app_key=%s&date=20190401&mode=%s&time=0715", appID, appKey, "national-rail%2Ctube"):
resp = getTestDataFileContents("Should_retrieve_journey_planner_itinerary_for_valid_search.json")
case fmt.Sprintf("/StopPoint/940GZZLUCYF/FareTo/910GPURLEYO?app_id=%s&app_key=%s", appID, appKey):
resp = getTestDataFileContents("single_fare_finder.json")
}
w.Write(resp)
}))
client, _ = New(
WithBaseURL(server.URL),
WithAppID(appID),
WithAppKey(appKey),
)
return func() {
server.Close()
}
}
func TestMain(m *testing.M) {
teardown := TflAPIClientStub()
defer teardown()
os.Exit(m.Run())
}
func TestTflAPIClient_buildURL(t *testing.T) {
type args struct {
pathParams []string
}
tests := []struct {
name string
api *TflClient
args args
want string
}{
{
name: "Should build URL correctly encoded with no queryParams",
api: client,
args: args{
pathParams: []string{
"StopPoint", "Search", "London Bridge",
},
},
want: fmt.Sprintf("%s/StopPoint/Search/%s?app_id=%s&app_key=%s", server.URL, "London%20Bridge", appID, appKey),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.api.buildURL(tt.args.pathParams); got != tt.want {
t.Errorf("TflAPIClient.buildURL() = %v, want %v", got, tt.want)
}
})
}
}
func TestTflAPIClient_buildURLWithQueryParams(t *testing.T) {
type args struct {
pathParams []string
queryParams *map[string]string
}
tests := []struct {
name string
api *TflClient
args args
want string
}{
{
name: "Should build URL correctly with queryParams",
api: client,
args: args{
pathParams: []string{
"StopPoint", "Search",
},
queryParams: &map[string]string{
"queryParam": "one",
"aBeforeAppID": "two",
},
},
want: fmt.Sprintf("%s/StopPoint/Search?aBeforeAppID=two&app_id=%s&app_key=%s&queryParam=one",
server.URL, appID, appKey),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.api.buildURLWithQueryParams(tt.args.pathParams, tt.args.queryParams); got != tt.want {
t.Errorf("TflAPIClient.buildURLWithQueryParams() = %v, want %v", got, tt.want)
}
})
}
}
func TestTflAPIClient_GetStopPointForID(t *testing.T) {
expected := StopPointAPIResponse{}
json.Unmarshal(getTestDataFileContents("Should_retrieve_StopPoint_given_valid_ID.json"), &expected)
type args struct {
id string
}
tests := []struct {
name string
api *TflClient
args args
want *StopPointAPIResponse
wantErr error
}{
{
name: "Should retrieve StopPoint given valid ID",
api: client,
args: args{
id: "9100ECROYDN",
},
want: &expected,
},
{
name: "Should handle response for invalid ID",
api: client,
args: args{
id: "INVALID",
},
wantErr: errors.New("The following stop point is not recognised: INVALID"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.api.GetStopPointForID(tt.args.id)
if tt.wantErr != nil && !reflect.DeepEqual(err, tt.wantErr) {
t.Errorf("TflAPIClient.GetStopPointForID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("TflAPIClient.GetStopPointForID() = %v, want %v", got, tt.want)
}
})
}
}
func TestTflAPIClient_SearchStopPoints(t *testing.T) {
response := EntitySearchResponse{}
json.Unmarshal(getTestDataFileContents("Should_retrieve_Search_Reponses_given_valid_ID.json"), &response)
expected := response.Matches
type args struct {
searchTerm string
}
tests := []struct {
name string
api *TflClient
args args
want *[]EntityMatchedStop
wantErr error
}{
{
name: "Should retrieve matches for valid searchTerm",
api: client,
args: args{
searchTerm: "London Bridge",
},
want: &expected,
},
{
name: "Should retrieve no matches for invalid searchTerm",
api: client,
args: args{
searchTerm: "Nope",
},
want: &[]EntityMatchedStop{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.api.SearchStopPoints(tt.args.searchTerm)
if tt.wantErr != nil && !reflect.DeepEqual(err, tt.wantErr) {
t.Errorf("TflAPIClient.SearchStopPoints() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("TflAPIClient.SearchStopPoints() = %v, want %v", got, tt.want)
}
})
}
}
func TestTflAPIClient_SearchStopPointsWithModes(t *testing.T) {
response := EntitySearchResponse{}
json.Unmarshal(getTestDataFileContents("Should_retrieve_filtered_Search_Reponses_given_valid_ID.json"), &response)
expected := response.Matches
type args struct {
searchTerm string
modes []string
}
tests := []struct {
name string
api *TflClient
args args
want *[]EntityMatchedStop
wantErr error
}{
{
name: "Should retrieve matches for valid searchTerm and filter by modes",
api: client,
args: args{
searchTerm: "London Bridge",
modes: []string{"national-rail", "tube"},
},
want: &expected,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.api.SearchStopPointsWithModes(tt.args.searchTerm, tt.args.modes)
if tt.wantErr != nil && !reflect.DeepEqual(err, tt.wantErr) {
t.Errorf("TflAPIClient.SearchStopPointsWithModes() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("TflAPIClient.SearchStopPointsWithModes() = %v, want %v", got, tt.want)
}
})
}
}
func TestTflAPIClient_GetJourneyPlannerItinerary(t *testing.T) {
expected := JourneyPlannerItineraryResult{}
json.Unmarshal(getTestDataFileContents("Should_retrieve_journey_planner_itinerary_for_valid_search.json"), &expected)
type args struct {
query JourneyPlannerQuery
}
tests := []struct {
name string
api *TflClient
args args
want *JourneyPlannerItineraryResult
wantErr error
}{
{
name: "Should retrieve journey planner itinerary for valid search",
api: client,
args: args{
query: JourneyPlannerQuery{
From: "1001089",
To: "1000173",
Date: "20190401",
Time: "0715",
Modes: []string{"national-rail", "tube"},
},
},
want: &expected,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.api.GetJourneyPlannerItinerary(tt.args.query)
if tt.wantErr != nil && !reflect.DeepEqual(err, tt.wantErr) {
t.Errorf("TflAPIClient.GetJourneyPlannerItinerary() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("TflAPIClient.GetJourneyPlannerItinerary() = %v, want %v", got, tt.want)
}
})
}
}
func TestTflClient_SingleFareFinder(t *testing.T) {
expected := []FaresSection{}
json.Unmarshal(getTestDataFileContents("single_fare_finder.json"), &expected)
type args struct {
input SingleFareFinderInput
}
tests := []struct {
name string
api *TflClient
args args
want *[]FaresSection
wantErr error
}{
{
name: "should retrieve single fare finder",
api: client,
args: args{
input: SingleFareFinderInput{
From: "940GZZLUCYF",
To: "910GPURLEYO",
},
},
want: &expected,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.api.SingleFareFinder(tt.args.input)
if err != nil && tt.wantErr == nil {
assert.Fail(t, fmt.Sprintf(
"Error not expected but got one:\n"+
"error: %q", err),
)
return
}
if tt.wantErr != nil {
assert.EqualError(t, err, tt.wantErr.Error())
return
}
assert.Equal(t, tt.want, got)
})
}
}