This repository has been archived by the owner on May 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
busstops_test.go
363 lines (354 loc) · 8.01 KB
/
busstops_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
package busetabot
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestInMemoryBusStopRepository_Get(t *testing.T) {
busStops := []BusStop{
{
BusStopCode: "00481",
RoadName: "Woodlands Rd",
Description: "BT PANJANG TEMP BUS PK",
Latitude: 1.383764,
Longitude: 103.7583,
},
{
BusStopCode: "01012",
RoadName: "Victoria St",
Description: "Hotel Grand Pacific",
Latitude: 1.29684825487647,
Longitude: 103.85253591654006,
},
}
repo := NewInMemoryBusStopRepository(busStops, nil)
testCases := []struct {
ID string
Expected *BusStop
}{
{
ID: "00481",
Expected: &BusStop{
BusStopCode: "00481",
RoadName: "Woodlands Rd",
Description: "BT PANJANG TEMP BUS PK",
Latitude: 1.383764,
Longitude: 103.7583,
},
},
{
ID: "01012",
Expected: &BusStop{
BusStopCode: "01012",
RoadName: "Victoria St",
Description: "Hotel Grand Pacific",
Latitude: 1.29684825487647,
Longitude: 103.85253591654006,
},
},
{
ID: "",
Expected: nil,
},
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
actual := repo.Get(tc.ID)
assert.Equal(t, tc.Expected, actual)
})
}
}
func TestInMemoryBusStopRepository_Nearby(t *testing.T) {
busStops := []BusStop{
{
BusStopCode: "01012",
RoadName: "Victoria St",
Description: "Hotel Grand Pacific",
Latitude: 1.29684825487647,
Longitude: 103.85253591654006,
},
{
BusStopCode: "00481",
RoadName: "Woodlands Rd",
Description: "BT PANJANG TEMP BUS PK",
Latitude: 1.383764,
Longitude: 103.7583,
},
}
repo := NewInMemoryBusStopRepository(busStops, nil)
testCases := []struct {
Name string
Lat float64
Lon float64
Radius float64
Limit int
Expected []NearbyBusStop
}{
{
Name: "one nearby bus stop",
Lat: 1.3837,
Lon: 103.75,
Radius: 10000,
Limit: 0,
Expected: []NearbyBusStop{
{
BusStop: BusStop{
BusStopCode: "00481",
RoadName: "Woodlands Rd",
Description: "BT PANJANG TEMP BUS PK",
Latitude: 1.383764,
Longitude: 103.7583,
},
Distance: 923.9831005649131},
},
},
{
Name: "two nearby bus stops, sorted by distance",
Lat: 1.3422,
Lon: 103.8023,
Radius: 10000,
Limit: 0,
Expected: []NearbyBusStop{
{
BusStop: BusStop{
BusStopCode: "00481",
RoadName: "Woodlands Rd",
Description: "BT PANJANG TEMP BUS PK",
Latitude: 1.383764,
Longitude: 103.7583,
},
Distance: 6716.655692096068,
},
{
BusStop: BusStop{
BusStopCode: "01012",
RoadName: "Victoria St",
Description: "Hotel Grand Pacific",
Latitude: 1.29684825487647,
Longitude: 103.85253591654006},
Distance: 7511.381516450915,
},
},
},
{
Name: "two nearby bus stops limited to one closest one",
Lat: 1.3422,
Lon: 103.8023,
Radius: 10000,
Limit: 1,
Expected: []NearbyBusStop{
{
BusStop: BusStop{
BusStopCode: "00481",
RoadName: "Woodlands Rd",
Description: "BT PANJANG TEMP BUS PK",
Latitude: 1.383764,
Longitude: 103.7583,
},
Distance: 6716.655692096068,
},
},
},
{
Name: "limit greater than number of nearby bus stops",
Lat: 1.3422,
Lon: 103.8023,
Radius: 10000,
Limit: 3,
Expected: []NearbyBusStop{
{
BusStop: BusStop{
BusStopCode: "00481",
RoadName: "Woodlands Rd",
Description: "BT PANJANG TEMP BUS PK",
Latitude: 1.383764,
Longitude: 103.7583,
},
Distance: 6716.655692096068,
},
{
BusStop: BusStop{
BusStopCode: "01012",
RoadName: "Victoria St",
Description: "Hotel Grand Pacific",
Latitude: 1.29684825487647,
Longitude: 103.85253591654006},
Distance: 7511.381516450915,
},
},
},
}
for _, tc := range testCases {
t.Run("", func(t *testing.T) {
actual := repo.Nearby(context.Background(), tc.Lat, tc.Lon, tc.Radius, tc.Limit)
assert.Equal(t, tc.Expected, actual)
})
}
}
func Test_replaceSynonyms(t *testing.T) {
synonyms := map[string]string{
"bukit": "bk",
"park": "pk",
}
tokens := []string{"bukit", "park"}
actual := replaceSynonyms(synonyms, tokens)
expected := []string{"bk", "pk"}
assert.Equal(t, expected, actual)
}
func TestInMemoryBusStopRepository_Search(t *testing.T) {
busStops := []BusStop{
{
BusStopCode: "01019",
RoadName: "Victoria St",
Description: "Bras Basah Cplx",
Latitude: 1.29698951191332,
Longitude: 103.85302201172507,
},
{
BusStopCode: "00481",
RoadName: "Woodlands Rd",
Description: "BT PANJANG TEMP BUS PK",
Latitude: 1.383764,
Longitude: 103.7583,
},
{
BusStopCode: "01029",
RoadName: "Nth Bridge Rd",
Description: "Cosmic Insurance Bldg",
Latitude: 1.2966729849642,
Longitude: 103.85441422464267,
},
{
BusStopCode: "04159",
RoadName: "Victoria St",
Description: "Aft Chijmes",
Latitude: 1.29489484954945,
Longitude: 103.85108138663934,
},
{
BusStopCode: "93031",
RoadName: "Marine Parade Rd",
Description: "Opp Victoria Sch",
Latitude: 1.30983637164414,
Longitude: 103.92937539318251,
},
}
synonyms := map[string]string{
"bukit": "bk",
"park": "pk",
}
repo := NewInMemoryBusStopRepository(busStops, nil)
repo.synonyms = synonyms
testCases := []struct {
Name string
Query string
Limit int
Expected []BusStop
}{
{
Name: "an empty string matches all bus stops",
Query: "",
Expected: busStops,
},
{
Name: "should respect limit when query is empty",
Query: "",
Limit: 1,
Expected: []BusStop{
{
BusStopCode: "01019",
RoadName: "Victoria St",
Description: "Bras Basah Cplx",
Latitude: 1.29698951191332,
Longitude: 103.85302201172507,
},
},
},
{
Name: "search by bus stop code",
Query: "01019",
Expected: []BusStop{
{
BusStopCode: "01019",
RoadName: "Victoria St",
Description: "Bras Basah Cplx",
Latitude: 1.29698951191332,
Longitude: 103.85302201172507,
},
},
},
{
Name: "search for word",
Query: "bridge",
Expected: []BusStop{
{
BusStopCode: "01029",
RoadName: "Nth Bridge Rd",
Description: "Cosmic Insurance Bldg",
Latitude: 1.2966729849642,
Longitude: 103.85441422464267,
},
},
},
{
Name: "matches in description should rank higher than matches in road name",
Query: "victoria",
Expected: []BusStop{
{
BusStopCode: "93031",
RoadName: "Marine Parade Rd",
Description: "Opp Victoria Sch",
Latitude: 1.30983637164414,
Longitude: 103.92937539318251,
},
{
BusStopCode: "01019",
RoadName: "Victoria St",
Description: "Bras Basah Cplx",
Latitude: 1.29698951191332,
Longitude: 103.85302201172507,
},
{
BusStopCode: "04159",
RoadName: "Victoria St",
Description: "Aft Chijmes",
Latitude: 1.29489484954945,
Longitude: 103.85108138663934,
},
},
},
{
Name: "matches in description should rank higher than matches in road name and should respect limit",
Query: "victoria",
Limit: 1,
Expected: []BusStop{
{
BusStopCode: "93031",
RoadName: "Marine Parade Rd",
Description: "Opp Victoria Sch",
Latitude: 1.30983637164414,
Longitude: 103.92937539318251,
},
},
},
{
Name: "searches for words should match their abbreviations",
Query: "bukit park",
Expected: []BusStop{
{
BusStopCode: "00481",
RoadName: "Woodlands Rd",
Description: "BT PANJANG TEMP BUS PK",
Latitude: 1.383764,
Longitude: 103.7583,
},
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
actual := repo.Search(context.Background(), tc.Query, tc.Limit)
assert.Equal(t, tc.Expected, actual)
})
}
}