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
/
Copy patheta_test.go
704 lines (667 loc) · 18.1 KB
/
eta_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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
package busetabot
import (
"context"
"errors"
"fmt"
"math"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/yi-jiayu/datamall/v3"
"github.com/yi-jiayu/bus-eta-bot/v4/telegram"
)
type mockBusStopRepository struct {
BusStop *BusStop
NearbyBusStops []BusStop
}
func (b mockBusStopRepository) Search(ctx context.Context, query string, limit int) []BusStop {
panic("implement me")
}
func (b mockBusStopRepository) Nearby(ctx context.Context, lat, lon, radius float64, limit int) (nearby []NearbyBusStop) {
for _, bs := range b.NearbyBusStops {
nearby = append(nearby, NearbyBusStop{
BusStop: bs,
Distance: math.Sqrt(SquaredEuclideanDistanceAtEquator(lat, lon, bs.Latitude, bs.Longitude)),
})
}
return
}
func (b mockBusStopRepository) Get(ID string) *BusStop {
return b.BusStop
}
type mockETAService struct {
BusArrival datamall.BusArrival
Error error
}
func (s mockETAService) GetBusArrival(string, string) (datamall.BusArrival, error) {
return s.BusArrival, s.Error
}
type mockDatamall struct {
}
func (d mockDatamall) GetBusArrival(code string, serviceNo string) (datamall.BusArrival, error) {
return newArrival(time.Time{}, code), nil
}
func newArrival(t time.Time, code string) datamall.BusArrival {
return datamall.BusArrival{
BusStopCode: code,
Services: []datamall.Service{
{
ServiceNo: "2",
NextBus: datamall.ArrivingBus{
EstimatedArrival: t.Add(-100 * time.Second),
Load: "SDA",
Type: "DD",
},
NextBus2: datamall.ArrivingBus{
EstimatedArrival: t.Add(600 * time.Second),
Load: "SDA",
Type: "DD",
},
NextBus3: datamall.ArrivingBus{
EstimatedArrival: t.Add(2200 * time.Second),
Load: "LSD",
Feature: "WAB",
Type: "BD",
},
},
{
ServiceNo: "24",
NextBus: datamall.ArrivingBus{
EstimatedArrival: t.Add(100 * time.Second),
Load: "SEA",
Type: "SD",
},
NextBus2: datamall.ArrivingBus{
EstimatedArrival: t.Add(200 * time.Second),
Load: "SDA",
Type: "DD",
Feature: "WAB",
},
NextBus3: datamall.ArrivingBus{
EstimatedArrival: t.Add(400 * time.Second),
Load: "LSD",
Type: "BD",
},
},
},
}
}
func TestInferEtaQuery(t *testing.T) {
testCases := []struct {
Name string
Text string
Expected struct {
BusStopID string
ServiceNos []string
Err error
}
}{
{
Name: "Bus stop ID only",
Text: "96049",
Expected: struct {
BusStopID string
ServiceNos []string
Err error
}{
BusStopID: "96049",
ServiceNos: []string{},
},
},
{
Name: "Bus stop ID and services",
Text: "96049 2 24",
Expected: struct {
BusStopID string
ServiceNos []string
Err error
}{
BusStopID: "96049",
ServiceNos: []string{"2", "24"},
},
},
{
Name: "Not comprised of 5 digits",
Text: "hello",
Expected: struct {
BusStopID string
ServiceNos []string
Err error
}{
Err: errInvalidBusStopCode,
},
},
{
Name: "More than 5 digits",
Text: "123456",
Expected: struct {
BusStopID string
ServiceNos []string
Err error
}{
Err: errInvalidBusStopCode,
},
},
{
Name: "doesn't start with bus stop code",
Text: "hello 12345",
Expected: struct {
BusStopID string
ServiceNos []string
Err error
}{
Err: errInvalidBusStopCode,
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
text := tc.Text
busStopID, serviceNos, err := InferEtaQuery(text)
if expected := tc.Expected.Err; expected != nil {
assert.Equal(t, expected, err)
} else {
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tc.Expected.BusStopID, busStopID)
assert.Equal(t, tc.Expected.ServiceNos, serviceNos)
}
})
}
}
func TestCalculateEtas(t *testing.T) {
now, _ := time.Parse(time.RFC3339, time.RFC3339)
t.Run("In operation, all etas", func(t *testing.T) {
etas := CalculateEtas(now, newArrival(now, ""))
actual := etas.Services
expected := []IncomingBuses{{ServiceNo: "2", Etas: [3]string{"-1", "10", "36"}, Loads: [3]string{"SDA", "SDA", "LSD"}, Features: [3]string{"", "", "WAB"}, Types: [3]string{"DD", "DD", "BD"}}, {ServiceNo: "24", Etas: [3]string{"1", "3", "6"}, Loads: [3]string{"SEA", "SDA", "LSD"}, Features: [3]string{"", "WAB", ""}, Types: [3]string{"SD", "DD", "BD"}}}
if !reflect.DeepEqual(actual, expected) {
fmt.Printf("Expected:\n%#v\nActual:\n%#v\n", expected, actual)
t.Fail()
}
})
t.Run("In operation, missing etas", func(t *testing.T) {
arrival := newArrival(now, "")
arrival.Services[0].NextBus2.EstimatedArrival = time.Time{}
arrival.Services[0].NextBus3.EstimatedArrival = time.Time{}
etas := CalculateEtas(now, arrival)
actual := etas.Services
expected := []IncomingBuses{{ServiceNo: "2", Etas: [3]string{"-1", "?", "?"}, Loads: [3]string{"SDA", "SDA", "LSD"}, Features: [3]string{"", "", "WAB"}, Types: [3]string{"DD", "DD", "BD"}}, {ServiceNo: "24", Etas: [3]string{"1", "3", "6"}, Loads: [3]string{"SEA", "SDA", "LSD"}, Features: [3]string{"", "WAB", ""}, Types: [3]string{"SD", "DD", "BD"}}}
if !reflect.DeepEqual(actual, expected) {
fmt.Printf("Expected:\n%#v\nActual:\n%#v\n", expected, actual)
t.Fail()
}
})
t.Run("In operation, no etas", func(t *testing.T) {
arrival := newArrival(now, "")
arrival.Services[0].NextBus.EstimatedArrival = time.Time{}
arrival.Services[0].NextBus2.EstimatedArrival = time.Time{}
arrival.Services[0].NextBus3.EstimatedArrival = time.Time{}
etas := CalculateEtas(now, arrival)
actual := etas.Services
expected := []IncomingBuses{{ServiceNo: "2", Etas: [3]string{"?", "?", "?"}, Loads: [3]string{"SDA", "SDA", "LSD"}, Features: [3]string{"", "", "WAB"}, Types: [3]string{"DD", "DD", "BD"}}, {ServiceNo: "24", Etas: [3]string{"1", "3", "6"}, Loads: [3]string{"SEA", "SDA", "LSD"}, Features: [3]string{"", "WAB", ""}, Types: [3]string{"SD", "DD", "BD"}}}
if !reflect.DeepEqual(actual, expected) {
fmt.Printf("Expected:\n%#v\nActual:\n%#v\n", expected, actual)
t.Fail()
}
})
}
func TestEtaTable(t *testing.T) {
services := [][4]string{
{"2", "-2", "12", "20"},
{"24", "2", "9", "18"},
}
expected := "| Svc | Next | 2nd | 3rd |\n|-----|------|------|------|\n| 2 | -2 | 12 | 20 |\n| 24 | 2 | 9 | 18 |"
actual := EtaTable(services)
if actual != expected {
fmt.Printf("Expected:\n%q\nActual:\n%q\n", expected, actual)
t.Fail()
}
}
func TestSummaryETAFormatter_Format(t *testing.T) {
svc2 := IncomingBuses{
ServiceNo: "2",
Etas: [3]string{"-1", "10", "36"},
Loads: [3]string{"", "SDA", "LSD"},
Features: [3]string{"", "", "WAB"},
Types: [3]string{"", "DD", "BD"},
}
svc24 := IncomingBuses{
ServiceNo: "24",
Etas: [3]string{"1", "3", "6"},
Loads: [3]string{"SEA", "SDA", "LSD"},
Features: [3]string{"", "WAB", ""},
Types: [3]string{"SD", "DD", "BD"},
}
type testCase struct {
Name string
ETAs BusEtas
ServiceNos []string
Expected string
}
testCases := []testCase{
{
Name: "single eta",
ETAs: BusEtas{
Services: []IncomingBuses{
svc2,
},
},
Expected: "```\n| Svc | Next | 2nd | 3rd |\n|-----|------|------|------|\n| 2 | -1 | 10 | 36 |```\nShowing 1 out of 1 service for this bus stop.",
},
{
Name: "multiple etas",
ETAs: BusEtas{
Services: []IncomingBuses{
svc2,
svc24,
},
},
Expected: "```\n| Svc | Next | 2nd | 3rd |\n|-----|------|------|------|\n| 2 | -1 | 10 | 36 |\n| 24 | 1 | 3 | 6 |```\nShowing 2 out of 2 services for this bus stop.",
},
{
Name: "filtered etas",
ETAs: BusEtas{
Services: []IncomingBuses{
svc2,
svc24,
},
},
ServiceNos: []string{"2"},
Expected: "```\n| Svc | Next | 2nd | 3rd |\n|-----|------|------|------|\n| 2 | -1 | 10 | 36 |```\nShowing 1 out of 2 services for this bus stop.",
},
}
for _, tc := range testCases {
formatter := SummaryETAFormatter{}
t.Run(tc.Name, func(t *testing.T) {
actual := formatter.Format(tc.ETAs, tc.ServiceNos)
assert.Equal(t, tc.Expected, actual)
})
}
}
func TestNewRefreshButton(t *testing.T) {
type testCase struct {
Name string
BusStopCode string
ServiceNos []string
Expected telegram.InlineKeyboardButton
}
testCases := []testCase{
{
Name: "with bus stop code only",
BusStopCode: "96049",
ServiceNos: nil,
Expected: telegram.InlineKeyboardButton{
Text: "Refresh",
CallbackData: `{"t":"refresh","b":"96049"}`,
},
},
{
Name: "with bus stop code and service nos",
BusStopCode: "96049",
ServiceNos: []string{"2", "24"},
Expected: telegram.InlineKeyboardButton{
Text: "Refresh",
CallbackData: `{"t":"refresh","b":"96049","s":["2","24"]}`,
SwitchInlineQueryCurrentChat: (*string)(nil),
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
actual := NewRefreshButton(tc.BusStopCode, tc.ServiceNos, "")
assert.Equal(t, tc.Expected, actual)
})
}
}
func TestNewResendButton(t *testing.T) {
type testCase struct {
Name string
BusStopCode string
ServiceNos []string
Expected telegram.InlineKeyboardButton
}
testCases := []testCase{
{
Name: "with bus stop code only",
BusStopCode: "96049",
ServiceNos: nil,
Expected: telegram.InlineKeyboardButton{
Text: "Resend",
CallbackData: `{"t":"resend","b":"96049"}`,
},
},
{
Name: "with bus stop code and service nos",
BusStopCode: "96049",
ServiceNos: []string{"2", "24"},
Expected: telegram.InlineKeyboardButton{
Text: "Resend",
CallbackData: `{"t":"resend","b":"96049","s":["2","24"]}`,
SwitchInlineQueryCurrentChat: (*string)(nil),
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
actual := NewResendButton(tc.BusStopCode, tc.ServiceNos, "")
assert.Equal(t, tc.Expected, actual)
})
}
}
func TestNewToggleFavouriteButton(t *testing.T) {
type testCase struct {
Name string
BusStopCode string
ServiceNos []string
Expected telegram.InlineKeyboardButton
}
testCases := []testCase{
{
Name: "with bus stop code only",
BusStopCode: "96049",
ServiceNos: nil,
Expected: telegram.InlineKeyboardButton{
Text: "⭐",
CallbackData: `{"t":"togf","a":"96049"}`,
},
},
{
Name: "with bus stop code and service nos",
BusStopCode: "96049",
ServiceNos: []string{"2", "24"},
Expected: telegram.InlineKeyboardButton{
Text: "⭐",
CallbackData: `{"t":"togf","a":"96049 2 24"}`,
SwitchInlineQueryCurrentChat: (*string)(nil),
},
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
actual := NewToggleFavouriteButton(tc.BusStopCode, tc.ServiceNos)
assert.Equal(t, tc.Expected, actual)
})
}
}
func TestNewETAMessageReplyMarkup(t *testing.T) {
type args struct {
busStopCode string
serviceNos []string
formatter string
inline bool
}
tests := []struct {
name string
args args
want telegram.InlineKeyboardMarkup
}{
{
name: "when not inline",
args: args{
busStopCode: "96049",
inline: false,
},
want: telegram.InlineKeyboardMarkup{
InlineKeyboard: [][]telegram.InlineKeyboardButton{
{
{
Text: "Refresh",
CallbackData: `{"t":"refresh","b":"96049"}`,
},
{
Text: "Resend",
CallbackData: `{"t":"resend","b":"96049"}`,
},
{
Text: "⭐",
CallbackData: `{"t":"togf","a":"96049"}`,
},
},
{
{
Text: "Show incoming bus details",
CallbackData: `{"t":"refresh","b":"96049","f":"f"}`,
},
},
},
},
},
{
name: "when not inline, with formatter",
args: args{
busStopCode: "96049",
formatter: FormatterSummary,
inline: false,
},
want: telegram.InlineKeyboardMarkup{
InlineKeyboard: [][]telegram.InlineKeyboardButton{
{
{
Text: "Refresh",
CallbackData: `{"t":"refresh","b":"96049","f":"s"}`,
},
{
Text: "Resend",
CallbackData: `{"t":"resend","b":"96049","f":"s"}`,
},
{
Text: "⭐",
CallbackData: `{"t":"togf","a":"96049"}`,
},
},
{
{
Text: "Show incoming bus details",
CallbackData: `{"t":"refresh","b":"96049","f":"f"}`,
},
},
},
},
},
{
name: "when not inline, with features formatter",
args: args{
busStopCode: "96049",
formatter: FormatterFeatures,
inline: false,
},
want: telegram.InlineKeyboardMarkup{
InlineKeyboard: [][]telegram.InlineKeyboardButton{
{
{
Text: "Refresh",
CallbackData: `{"t":"refresh","b":"96049","f":"f"}`,
},
{
Text: "Resend",
CallbackData: `{"t":"resend","b":"96049","f":"f"}`,
},
{
Text: "⭐",
CallbackData: `{"t":"togf","a":"96049"}`,
},
},
{
{
Text: "Show incoming bus summary",
CallbackData: `{"t":"refresh","b":"96049","f":"s"}`,
},
},
},
},
},
{
name: "when inline",
args: args{
busStopCode: "96049",
inline: true,
},
want: telegram.InlineKeyboardMarkup{
InlineKeyboard: [][]telegram.InlineKeyboardButton{
{
{
Text: "Refresh",
CallbackData: `{"t":"refresh","b":"96049"}`,
},
},
{
{
Text: "Show incoming bus details",
CallbackData: `{"t":"refresh","b":"96049","f":"f"}`,
},
},
},
},
},
{
name: "when inline, with formatter",
args: args{
busStopCode: "96049",
formatter: FormatterFeatures,
inline: true,
},
want: telegram.InlineKeyboardMarkup{
InlineKeyboard: [][]telegram.InlineKeyboardButton{
{
{
Text: "Refresh",
CallbackData: `{"t":"refresh","b":"96049","f":"f"}`,
},
},
{
{
Text: "Show incoming bus summary",
CallbackData: `{"t":"refresh","b":"96049","f":"s"}`,
},
},
},
},
},
{
name: "when inline, with features formatter",
args: args{
busStopCode: "96049",
formatter: FormatterSummary,
inline: true,
},
want: telegram.InlineKeyboardMarkup{
InlineKeyboard: [][]telegram.InlineKeyboardButton{
{
{
Text: "Refresh",
CallbackData: `{"t":"refresh","b":"96049","f":"s"}`,
},
},
{
{
Text: "Show incoming bus details",
CallbackData: `{"t":"refresh","b":"96049","f":"f"}`,
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := NewETAMessageReplyMarkup(tt.args.busStopCode, tt.args.serviceNos, tt.args.formatter, tt.args.inline)
assert.Equal(t, tt.want, actual)
})
}
}
func TestNewETA(t *testing.T) {
busStop := BusStop{
BusStopCode: "96049",
RoadName: "Upp Changi Rd East",
Description: "Opp Tropicana Condo",
}
t.Run("populates bus stop when bus stop is found", func(t *testing.T) {
getter := mockBusStopRepository{
BusStop: &busStop,
}
actual := NewETA(context.Background(), getter, mockDatamall{}, ETARequest{})
assert.Equal(t, busStop, actual.BusStop)
})
t.Run("populates bus stop code when bus stop is not found", func(t *testing.T) {
getter := mockBusStopRepository{}
code := "96049"
actual := NewETA(context.Background(), getter, mockDatamall{}, ETARequest{
Code: code,
})
expected := BusStop{
BusStopCode: code,
}
assert.Equal(t, expected, actual.BusStop)
})
t.Run("contains appropriate error when datamall is down", func(t *testing.T) {
getter := mockBusStopRepository{}
etaService := mockETAService{
Error: &datamall.Error{
StatusCode: 503,
},
}
actual := NewETA(context.Background(), getter, etaService, ETARequest{})
expected := "LTA DataMall could be down at the moment (status code 503)"
assert.Equal(t, expected, actual.Error)
})
t.Run("contains appropriate error when something else went wrong", func(t *testing.T) {
getter := mockBusStopRepository{}
etaService := mockETAService{
Error: errors.New("some other error"),
}
actual := NewETA(context.Background(), getter, etaService, ETARequest{})
expected := "An error occurred while fetching ETAs (request ID: )"
assert.Equal(t, expected, actual.Error)
})
t.Run("doesn't filter services when services is empty", func(t *testing.T) {
getter := mockBusStopRepository{}
arrival := newArrival(time.Time{}, "")
etaService := mockETAService{
BusArrival: arrival,
}
actual := NewETA(context.Background(), getter, etaService, ETARequest{})
assert.Equal(t, arrival.Services, actual.Services)
})
t.Run("doesn't filter services when services is empty", func(t *testing.T) {
getter := mockBusStopRepository{}
arrival := newArrival(time.Time{}, "")
etaService := mockETAService{
BusArrival: arrival,
}
request := ETARequest{
Services: []string{"2"},
}
actual := NewETA(context.Background(), getter, etaService, request)
expected := []datamall.Service{
arrival.Services[0],
}
assert.Equal(t, expected, actual.Services)
})
}
type mockFormatter struct{}
func (f mockFormatter) GetFormatter(ctx context.Context, userID int) Formatter {
return f
}
func (f mockFormatter) Format(eta ETA) (string, error) {
var output string
for _, svc := range eta.Services {
output += " " + svc.ServiceNo
}
return "services:" + output, nil
}
func TestETAMessageFactory_Text(t *testing.T) {
f := ETAMessageFactory{
busStopGetter: mockBusStopRepository{},
etaService: mockDatamall{},
formatterFactory: mockFormatter{},
}
actual, err := f.Text(context.Background(), ETARequest{})
if err != nil {
t.Fatal(err)
}
expected := "services: 2 24"
assert.Equal(t, expected, actual)
}