-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic_test.go
1130 lines (1065 loc) · 35 KB
/
static_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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gtfs_test
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"tidbyt.dev/gtfs/model"
"tidbyt.dev/gtfs/testutil"
)
func testStaticDeparturesWindowing(t *testing.T, backend string) {
duration := func(h, m, s int) time.Duration {
return time.Duration(h)*time.Hour + time.Duration(m)*time.Minute + time.Duration(s)*time.Second
}
g := testutil.BuildStatic(t, backend, map[string][]string{
// A weekdays only schedule
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"weekday,20200101,20201231,1,1,1,1,1,0,0",
},
// Two routes: L and F
"routes.txt": {"route_id,route_short_name,route_type", "L,l,0", "F,f,0"},
// A bunch of stops
"stops.txt": {
"stop_id,stop_name,stop_lat,stop_lon",
"3a,3a,1,1",
"14,14,2,2",
"6a,6a,3,3",
"w4,w4,4,4",
"23,23,5,5",
},
// The L has three trips running east, two running west. F runs north then south.
"trips.txt": {
"trip_id,route_id,service_id,direction_id",
"LE1,L,weekday,0",
"LW1,L,weekday,1",
"LE2,L,weekday,0",
"LW2,L,weekday,1",
"LE3,L,weekday,0",
"FN1,F,weekday,1",
"FS1,F,weekday,0",
},
// The L trips run 3rd ave - 14th st - 6th ave. F runs W4 - 14th - 23rd.
"stop_times.txt": {
"trip_id,stop_id,departure_time,arrival_time,stop_sequence",
"LW1,3a,6:10:0,6:10:0,1",
"LW1,14,6:12:0,6:12:0,2",
"LW1,6a,6:14:0,6:14:0,3",
"LE2,6a,6:22:0,6:22:0,100",
"LE2,14,6:24:0,6:24:0,102",
"LE2,3a,6:26:0,6:26:0,104",
"LW2,3a,6:30:0,6:30:0,1",
"LW2,14,6:32:0,6:32:0,2",
"LW2,6a,6:34:0,6:34:0,3",
"LE3,6a,6:42:0,6:42:0,1",
"LE3,14,6:44:0,6:44:0,2",
"LE3,3a,6:46:0,6:46:0,3",
"FN1,w4,6:30:0,6:30:0,1",
"FN1,14,6:35:0,6:35:0,2",
"FN1,23,6:40:0,6:40:0,3",
"FS1,23,6:45:0,6:45:0,10",
"FS1,14,6:50:0,6:50:0,11",
"FS1,w4,6:55:0,6:55:0,15",
},
})
// Feb 4th is a Tuesday, so the weekday schedule
// applies. Within 30 minutes of 6 AM, the 14th street
// station should have 2 L train departures
departures, err := g.Departures("14", time.Date(2020, 2, 4, 6, 0, 0, 0, time.UTC), 30*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "14",
RouteID: "L",
TripID: "LW1",
DirectionID: 1,
StopSequence: 2,
Time: time.Date(2020, 2, 4, 6, 12, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "L",
TripID: "LE2",
StopSequence: 102,
DirectionID: 0,
Time: time.Date(2020, 2, 4, 6, 24, 0, 0, time.UTC)},
}, departures)
// Extend the window to 50 minutes and we capture 2 extra L
// stops, and two F train stops. The last one is right on the
// boundary of the window.
departures, err = g.Departures("14", time.Date(2020, 2, 4, 6, 10, 0, 0, time.UTC), 50*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "14",
RouteID: "L",
TripID: "LW1",
StopSequence: 2,
DirectionID: 1,
Time: time.Date(2020, 2, 4, 6, 12, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "L",
TripID: "LE2",
StopSequence: 102,
DirectionID: 0,
Time: time.Date(2020, 2, 4, 6, 24, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "L",
TripID: "LW2",
StopSequence: 2,
DirectionID: 1,
Time: time.Date(2020, 2, 4, 6, 32, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "F",
TripID: "FN1",
StopSequence: 2,
DirectionID: 1,
Time: time.Date(2020, 2, 4, 6, 35, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "L",
TripID: "LE3",
StopSequence: 2,
DirectionID: 0,
Time: time.Date(2020, 2, 4, 6, 44, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "F",
TripID: "FS1",
StopSequence: 11,
DirectionID: 0,
Time: time.Date(2020, 2, 4, 6, 50, 0, 0, time.UTC)},
}, departures)
// Start window at 6:30 and earlier departures are cut
departures, err = g.Departures("14", time.Date(2020, 2, 4, 6, 30, 0, 0, time.UTC), 50*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "14",
RouteID: "L",
TripID: "LW2",
StopSequence: 2,
DirectionID: 1,
Time: time.Date(2020, 2, 4, 6, 32, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "F",
TripID: "FN1",
StopSequence: 2,
DirectionID: 1,
Time: time.Date(2020, 2, 4, 6, 35, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "L",
TripID: "LE3",
StopSequence: 2,
DirectionID: 0,
Time: time.Date(2020, 2, 4, 6, 44, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "F",
TripID: "FS1",
StopSequence: 11,
DirectionID: 0,
Time: time.Date(2020, 2, 4, 6, 50, 0, 0, time.UTC)},
}, departures)
// Push window past last departure and we get nothing
departures, err = g.Departures("14", time.Date(2020, 2, 4, 6, 51, 0, 0, time.UTC), 50*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{}, departures)
// Non-existent stop also gives us nothing
departures, err = g.Departures("FOO", time.Date(2020, 2, 4, 6, 30, 0, 0, time.UTC), 50*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{}, departures)
// But a large enough window reaches next day's departures.
departures, err = g.Departures("14", time.Date(2020, 2, 4, 6, 51, 0, 0, time.UTC), duration(23, 50, 0), -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "14",
RouteID: "L",
TripID: "LW1",
StopSequence: 2,
DirectionID: 1,
Time: time.Date(2020, 2, 5, 6, 12, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "L",
TripID: "LE2",
StopSequence: 102,
DirectionID: 0,
Time: time.Date(2020, 2, 5, 6, 24, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "L",
TripID: "LW2",
StopSequence: 2,
DirectionID: 1,
Time: time.Date(2020, 2, 5, 6, 32, 0, 0, time.UTC)},
{
StopID: "14",
RouteID: "F",
TripID: "FN1",
StopSequence: 2,
DirectionID: 1,
Time: time.Date(2020, 2, 5, 6, 35, 0, 0, time.UTC)},
}, departures)
// Outside of calendar, we get nothing (Jan 1st 2021 was a Friday)
departures, err = g.Departures("14", time.Date(2021, 1, 1, 6, 30, 0, 0, time.UTC), 50*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{}, departures)
}
func testStaticDeparturesWeekendSchedule(t *testing.T, backend string) {
g := testutil.BuildStatic(t, backend, map[string][]string{
// A weekend and a weekday schedules
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"weekday,20200101,20201231,1,1,1,1,1,0,0",
"weekend,20200101,20201231,0,0,0,0,0,1,1",
},
// Only 1 route: the L
"routes.txt": {"route_id,route_long_name,route_type", "L,The ELL,3"},
// Stops on Manhattan only
"stops.txt": {
"stop_id,stop_name,stop_lat,stop_lon",
"8a,8a,1,1",
"6a,6a,2,2",
"14,14,3,3",
"3a,3a,4,4",
},
// The L runs east then west on weekdays. Only east on weekends.
"trips.txt": {
"trip_id,route_id,service_id,direction_id",
"LE1,L,weekday,0",
"LW1,L,weekday,1",
"LE2,L,weekend,0",
},
// The trips stop at all 4 stations
"stop_times.txt": {
"trip_id,stop_id,stop_sequence,departure_time,arrival_time",
"LE1,8a,1,9:0:0,9:0:0",
"LE1,6a,2,9:5:0,9:5:0",
"LE1,14,3,9:10:0,9:10:0",
"LE1,3a,4,9:15:0,9:15:0",
"LW1,3a,1,9:20:0,9:20:0",
"LW1,14,2,9:25:0,9:25:0",
"LW1,6a,3,9:30:0,9:30:0",
"LW1,8a,4,9:35:0,9:35:0",
"LE2,8a,1,9:1:0,9:1:0",
"LE2,6a,2,9:6:0,9:6:0",
"LE2,14,3,9:11:0,9:11:0",
"LE2,3a,4,9:16:0,9:16:0",
},
})
// Feb 14th is a Friday, so weekday schedule applies.
departures, err := g.Departures("6a", time.Date(2020, 2, 14, 9, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "6a",
RouteID: "L",
TripID: "LE1",
StopSequence: 2,
DirectionID: 0,
Time: time.Date(2020, 2, 14, 9, 5, 0, 0, time.UTC),
},
}, departures)
// Feb 15th will be on weekend schedule
departures, err = g.Departures("6a", time.Date(2020, 2, 15, 9, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "6a",
RouteID: "L",
TripID: "LE2",
StopSequence: 2,
DirectionID: 0,
Time: time.Date(2020, 2, 15, 9, 6, 0, 0, time.UTC),
},
}, departures)
// Window spanning from 14th into 15th can capture stops from both days
departures, err = g.Departures("6a", time.Date(2020, 2, 14, 9, 29, 0, 0, time.UTC), 24*time.Hour-1*time.Second, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "6a",
RouteID: "L",
TripID: "LW1",
StopSequence: 3,
DirectionID: 1,
Time: time.Date(2020, 2, 14, 9, 30, 0, 0, time.UTC),
},
{
StopID: "6a",
RouteID: "L",
TripID: "LE2",
StopSequence: 2,
DirectionID: 0,
Time: time.Date(2020, 2, 15, 9, 6, 0, 0, time.UTC),
},
}, departures)
}
func testStaticDeparturesTimezones(t *testing.T, backend string) {
g := testutil.BuildStatic(t, backend, map[string][]string{
// Eastern Time
"agency.txt": {"agency_timezone,agency_name,agency_url", "America/New_York,MTA,http://example.com"},
// Mondays only!
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"mondays,20200101,20201231,1,0,0,0,0,0,0",
},
"routes.txt": {"route_id,route_short_name,route_type", "L,l,1"},
"stops.txt": {
"stop_id,stop_name,stop_lat,stop_lon",
"8a,8a,1,1",
"6a,6a,2,2",
"14,14,3,3",
"3a,3a,4,4",
},
"trips.txt": {
"trip_id,route_id,service_id,direction_id",
"LE1,L,mondays,0",
},
// The trips stop at all 4 stations
"stop_times.txt": {
"trip_id,stop_id,stop_sequence,departure_time,arrival_time",
"LE1,8a,1,9:0:0,9:0:0",
"LE1,6a,2,9:5:0,9:5:0",
"LE1,14,3,9:10:0,9:10:0",
"LE1,3a,4,9:15:0,9:15:0",
},
})
tzNYC, _ := time.LoadLocation("America/New_York")
// Querying using the transit agency's time zone
departures, err := g.Departures("6a", time.Date(2020, 2, 3, 9, 0, 0, 0, tzNYC), 20*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "6a",
RouteID: "L",
TripID: "LE1",
StopSequence: 2,
DirectionID: 0,
Time: time.Date(2020, 2, 3, 9, 5, 0, 0, tzNYC),
},
}, departures)
// Querying using UTC, which in February 2020 is NYC+5
departures, err = g.Departures("6a", time.Date(2020, 2, 3, 14, 0, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "6a",
RouteID: "L",
TripID: "LE1",
StopSequence: 2,
DirectionID: 0,
Time: time.Date(2020, 2, 3, 14, 5, 0, 0, time.UTC),
},
}, departures)
// This also works if we query for the preceding day, with a
// large enough window
departures, err = g.Departures("6a", time.Date(2020, 2, 2, 22, 0, 0, 0, time.UTC), 20*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "6a",
RouteID: "L",
TripID: "LE1",
StopSequence: 2,
DirectionID: 0,
Time: time.Date(2020, 2, 3, 14, 5, 0, 0, time.UTC),
},
}, departures)
}
func testStaticDeparturesOvernightTrip(t *testing.T, backend string) {
g := testutil.BuildStatic(t, backend, map[string][]string{
"agency.txt": {"agency_timezone,agency_name,agency_url", "America/New_York,MTA,http://example.com"},
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"weekend,20200101,20201231,0,0,0,0,0,1,1",
},
"routes.txt": {"route_id,route_short_name,route_type", "L,l,0"},
"stops.txt": {
"stop_id,stop_name,stop_lat,stop_lon",
"8a,8a,1,2",
"6a,6a,1,2",
"14,14,1,2",
"3a,3a,1,2",
"1a,1a,1,2",
},
"trips.txt": {
"trip_id,route_id,service_id,direction_id",
"LE1,L,weekend,0",
},
"stop_times.txt": {
"trip_id,stop_id,stop_sequence,departure_time,arrival_time",
"LE1,8a,1,23:00:0,23:00:0",
"LE1,6a,2,23:30:0,23:30:0",
"LE1,14,3,24:00:0,24:00:0",
"LE1,3a,4,24:30:0,24:30:0",
"LE1,1a,5,24:35:0,24:35:0",
},
})
tzNYC, _ := time.LoadLocation("America/New_York")
// Feb 9th is a Sunday. 3rd ave stop falls 00:30 on the 10th,
// but is still part of the feb 9 trip.
departures, err := g.Departures("3a", time.Date(2020, 2, 9, 23, 30, 0, 0, tzNYC), 2*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "3a",
RouteID: "L",
TripID: "LE1",
StopSequence: 4,
DirectionID: 0,
Time: time.Date(2020, 2, 10, 0, 30, 0, 0, tzNYC),
},
}, departures)
// It's also there if we query for departures on the 10th
departures, err = g.Departures("3a", time.Date(2020, 2, 10, 0, 15, 0, 0, tzNYC), 20*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "3a",
RouteID: "L",
TripID: "LE1",
StopSequence: 4,
DirectionID: 0,
Time: time.Date(2020, 2, 10, 0, 30, 0, 0, tzNYC),
},
}, departures)
// This works when we query with different timezone (UTC is
// NYC+5)
departures, err = g.Departures("3a", time.Date(2020, 2, 10, 4, 30, 0, 0, time.UTC), 2*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "3a",
RouteID: "L",
TripID: "LE1",
StopSequence: 4,
DirectionID: 0,
Time: time.Date(2020, 2, 10, 5, 30, 0, 0, time.UTC),
},
}, departures)
departures, err = g.Departures("3a", time.Date(2020, 2, 10, 5, 15, 0, 0, time.UTC), 20*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "3a",
RouteID: "L",
TripID: "LE1",
StopSequence: 4,
DirectionID: 0,
Time: time.Date(2020, 2, 10, 5, 30, 0, 0, time.UTC),
},
}, departures)
}
func testStaticDeparturesCalendarDateOverride(t *testing.T, backend string) {
g := testutil.BuildStatic(t, backend, map[string][]string{
"agency.txt": {"agency_timezone,agency_name,agency_url", "America/New_York,MTA,http://example.com"},
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"weekend,20200101,20201231,0,0,0,0,0,1,1",
},
"routes.txt": {"route_id,route_short_name,route_type", "L,L,4"},
"stops.txt": {
"stop_id,stop_name,stop_lat,stop_lon",
"8a,8a,1,1",
"6a,6a,2,2",
"14,14,3,3",
"3a,3a,4,4",
"1a,1a,5,5",
},
"trips.txt": {
"trip_id,route_id,service_id,direction_id",
"LE1,L,weekend,0",
},
"stop_times.txt": {
"trip_id,stop_id,stop_sequence,departure_time,arrival_time",
"LE1,8a,1,23:00:0,23:00:0",
"LE1,6a,2,23:30:0,23:30:0",
"LE1,14,3,24:00:0,24:00:0",
"LE1,3a,4,24:30:0,24:30:0",
"LE1,1a,5,24:35:0,24:35:0",
},
// This removes service from Saturday the 8th and
// Sunday the 16th. It adds service on Monday the
// 24th.
"calendar_dates.txt": {
"service_id,date,exception_type",
"weekend,20200208,2",
"weekend,20200216,2",
"weekend,20200224,1",
},
})
tzNYC, _ := time.LoadLocation("America/New_York")
// The 9th is still running, but the trips from the 8th
// (including the ones spilling over into the 9th) are
// disabled.
departures, err := g.Departures("8a", time.Date(2020, 2, 9, 22, 0, 0, 0, tzNYC), 2*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "8a",
RouteID: "L",
TripID: "LE1",
DirectionID: 0,
StopSequence: 1,
Time: time.Date(2020, 2, 9, 23, 0, 0, 0, tzNYC)},
}, departures)
departures, err = g.Departures("8a", time.Date(2020, 2, 8, 22, 0, 0, 0, tzNYC), 5*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{}, departures)
departures, err = g.Departures("3a", time.Date(2020, 2, 8, 22, 0, 0, 0, tzNYC), 5*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{}, departures)
// The trips from the 16th are also disabled, including spill
// over into the 17th. The 15th is still up though, including
// spill over into the 16th.
departures, err = g.Departures("8a", time.Date(2020, 2, 16, 22, 0, 0, 0, tzNYC), 5*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{}, departures)
departures, err = g.Departures("3a", time.Date(2020, 2, 16, 22, 0, 0, 0, tzNYC), 5*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{}, departures)
departures, err = g.Departures("8a", time.Date(2020, 2, 15, 22, 0, 0, 0, tzNYC), 5*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "8a",
RouteID: "L",
TripID: "LE1",
StopSequence: 1,
Time: time.Date(2020, 2, 15, 23, 0, 0, 0, tzNYC),
},
}, departures)
departures, err = g.Departures("3a", time.Date(2020, 2, 15, 22, 0, 0, 0, tzNYC), 5*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "3a",
RouteID: "L",
TripID: "LE1",
StopSequence: 4,
Time: time.Date(2020, 2, 16, 0, 30, 0, 0, tzNYC),
},
}, departures)
// The added Monday the 24th is enabled, including spill over
// into the the 25th. 25th remains disabled.
departures, err = g.Departures("8a", time.Date(2020, 2, 24, 22, 0, 0, 0, tzNYC), 5*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "8a",
RouteID: "L",
TripID: "LE1",
StopSequence: 1,
DirectionID: 0,
Time: time.Date(2020, 2, 24, 23, 0, 0, 0, tzNYC)},
}, departures)
departures, err = g.Departures("3a", time.Date(2020, 2, 24, 22, 0, 0, 0, tzNYC), 5*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "3a",
RouteID: "L",
TripID: "LE1",
StopSequence: 4,
DirectionID: 0,
Time: time.Date(2020, 2, 25, 0, 30, 0, 0, tzNYC)},
}, departures)
departures, err = g.Departures("8a", time.Date(2020, 2, 25, 22, 0, 0, 0, tzNYC), 5*time.Hour, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{}, departures)
}
// Real world schedules seem to provide departure_time for all stops,
// including the final stop on a trip. The spec doesn't seem to say
// much about this, but we have to make sure we're not interpreting
// these as actual departures. If the vehicle leaves it'll be on a
// different trip, possibly with the same block_id (which we're not
// using at the time of this writing).
func testStaticDeparturesNoDepartureFromFinalStop(t *testing.T, backend string) {
g := testutil.BuildStatic(t, backend, map[string][]string{
"agency.txt": {"agency_timezone,agency_name,agency_url", "America/New_York,MTA,http://example.com"},
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"everyday,20200101,20201231,1,1,1,1,1,1,1",
},
"routes.txt": {"route_id,route_long_name,route_type", "L,The L,1"},
"stops.txt": {
"stop_id,stop_name,stop_lat,stop_lon",
"8a,8a,1,1",
"6a,6a,2,2",
"14,14,3,3",
"3a,3a,4,4",
},
"trips.txt": {
"trip_id,route_id,service_id,direction_id",
"LE1,L,everyday,0",
},
"stop_times.txt": {
"trip_id,stop_id,stop_sequence,departure_time,arrival_time",
"LE1,8a,1,23:00:0,23:00:0",
"LE1,6a,2,23:30:0,23:30:0",
"LE1,14,3,24:00:0,24:00:0",
"LE1,3a,4,24:30:0,24:30:0",
},
})
tzNYC, _ := time.LoadLocation("America/New_York")
departures, err := g.Departures("14", time.Date(2020, 2, 9, 23, 0, 0, 0, tzNYC), 2*time.Hour, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, 1, len(departures))
assert.Equal(t, []model.Departure{
{
StopID: "14",
RouteID: "L",
TripID: "LE1",
StopSequence: 3,
DirectionID: 0,
Time: time.Date(2020, 2, 10, 0, 0, 0, 0, tzNYC)},
}, departures)
departures, err = g.Departures("3a", time.Date(2020, 2, 9, 23, 0, 0, 0, tzNYC), 2*time.Hour, -1, "", -1, nil)
assert.Equal(t, nil, err)
assert.Equal(t, []model.Departure{}, departures)
}
func testStaticDeparturesFiltering(t *testing.T, backend string) {
// This weekend schedule has RouteA running alpha-beta-gamma
// and gamma-beta-alpha a few times per day. Route B does a
// single run beta-epsilon-gamma.
g := testutil.BuildStatic(t, backend, map[string][]string{
"agency.txt": {"agency_timezone,agency_name,agency_url", "America/New_York,MTA,http://example.com"},
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"weekend,20200101,20201231,0,0,0,0,0,1,1",
},
"routes.txt": {"route_id,route_short_name,route_type", "RouteA,a,0", "RouteB,a,0"},
"stops.txt": {
"stop_id,stop_name,stop_lat,stop_lon",
"alpha,alpha,1,1",
"beta,beta,2,2",
"gamma,gamma,3,3",
"delta,delta,4,4",
"epsilon,epsilon,5,5",
},
"trips.txt": {
"trip_id,route_id,service_id,direction_id",
"A1,RouteA,weekend,0",
"A2,RouteA,weekend,0",
"A3,RouteA,weekend,0",
"a1,RouteA,weekend,1",
"a2,RouteA,weekend,1",
"a3,RouteA,weekend,1",
"B1,RouteB,weekend,0",
},
"stop_times.txt": {
"trip_id,stop_id,stop_sequence,departure_time,arrival_time",
"A1,alpha,1,5:30:0,5:30:0",
"A1,beta,2,6:0:0,6:0:0",
"A1,gamma,3,6:30:0,6:30:0",
"A2,alpha,1,12:30:0,12:30:0",
"A2,beta,2,13:0:0,13:0:0",
"A2,gamma,3,13:30:0,13:30:0",
"A3,alpha,1,23:30:0,23:30:0",
"A3,beta,2,24:0:0,24:0:0",
"A3,gamma,3,24:30:0,24:30:0",
"a1,gamma,1,5:31:0,5:31:0",
"a1,beta,2,6:1:0,6:1:0",
"a1,alpha,3,6:31:0,6:31:0",
"a2,gamma,1,12:31:0,12:31:0",
"a2,beta,2,13:1:0,13:1:0",
"a2,alpha,3,13:31:0,13:31:0",
"a3,gamma,1,23:31:0,23:31:0",
"a3,beta,2,24:1:0,24:1:0",
"a3,alpha,3,24:31:0,24:31:0",
"B1,beta,1,11:0:0,11:0:0",
"B1,epsilon,2,11:30:0,11:30:0",
"B1,gamma,3,12:0:0,12:0:0",
},
})
tzNYC, _ := time.LoadLocation("America/New_York")
longDuration := 100 * 24 * time.Hour
// March 14th was a Saturday
// Departures from alpha, in any direction, on any route
departures, err := g.Departures("alpha", time.Date(2020, 3, 14, 0, 0, 0, 0, tzNYC), longDuration, 1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, 1, len(departures))
assert.Equal(t, []model.Departure{
{
StopID: "alpha",
RouteID: "RouteA",
TripID: "A1",
StopSequence: 1,
DirectionID: 0,
Time: time.Date(2020, 3, 14, 5, 30, 0, 0, tzNYC)},
}, departures)
// Specifying non-existent route and/or direction -> no results
departures, err = g.Departures("alpha", time.Date(2020, 3, 14, 0, 0, 0, 0, tzNYC), longDuration, 1, "", 1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{}, departures)
departures, err = g.Departures("alpha", time.Date(2020, 3, 14, 0, 0, 0, 0, tzNYC), longDuration, 1, "RouteC", 0, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{}, departures)
departures, err = g.Departures("alpha", time.Date(2020, 3, 14, 0, 0, 0, 0, tzNYC), longDuration, 1, "RouteC", -1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{}, departures)
// The beta stop has departures in 2 direction
departures, err = g.Departures("beta", time.Date(2020, 3, 14, 0, 0, 0, 0, tzNYC), longDuration, 1, "", 0, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "beta",
RouteID: "RouteA",
TripID: "A1",
StopSequence: 2,
DirectionID: 0,
Time: time.Date(2020, 3, 14, 6, 0, 0, 0, tzNYC)},
}, departures)
departures, err = g.Departures("beta", time.Date(2020, 3, 14, 0, 0, 0, 0, tzNYC), longDuration, 1, "", 1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "beta",
RouteID: "RouteA",
TripID: "a1",
StopSequence: 2,
DirectionID: 1,
Time: time.Date(2020, 3, 14, 6, 1, 0, 0, tzNYC)},
}, departures)
// Pushing start time back discards earlier departures
departures, err = g.Departures("beta", time.Date(2020, 3, 14, 12, 0, 0, 0, tzNYC), longDuration, 1, "", 0, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "beta",
RouteID: "RouteA",
TripID: "A2",
StopSequence: 2,
DirectionID: 0,
Time: time.Date(2020, 3, 14, 13, 0, 0, 0, tzNYC)},
}, departures)
departures, err = g.Departures("beta", time.Date(2020, 3, 14, 12, 0, 0, 0, tzNYC), longDuration, 1, "RouteA", 1, nil)
assert.NoError(t, err)
assert.Equal(t, []model.Departure{
{
StopID: "beta",
RouteID: "RouteA",
TripID: "a2",
StopSequence: 2,
DirectionID: 1,
Time: time.Date(2020, 3, 14, 13, 1, 0, 0, tzNYC)},
}, departures)
// Requesting a whole lot of departures results in a whole lot of departures
departures, err = g.Departures("alpha", time.Date(2020, 3, 14, 0, 0, 0, 0, tzNYC), longDuration, 9, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, 9, len(departures))
assert.Equal(t, time.Date(2020, 3, 14, 5, 30, 0, 0, tzNYC), departures[0].Time)
assert.Equal(t, time.Date(2020, 3, 14, 12, 30, 0, 0, tzNYC), departures[1].Time)
assert.Equal(t, time.Date(2020, 3, 14, 23, 30, 0, 0, tzNYC), departures[2].Time)
assert.Equal(t, time.Date(2020, 3, 15, 5, 30, 0, 0, tzNYC), departures[3].Time)
assert.Equal(t, time.Date(2020, 3, 15, 12, 30, 0, 0, tzNYC), departures[4].Time)
assert.Equal(t, time.Date(2020, 3, 15, 23, 30, 0, 0, tzNYC), departures[5].Time)
assert.Equal(t, time.Date(2020, 3, 21, 5, 30, 0, 0, tzNYC), departures[6].Time)
assert.Equal(t, time.Date(2020, 3, 21, 12, 30, 0, 0, tzNYC), departures[7].Time)
assert.Equal(t, time.Date(2020, 3, 21, 23, 30, 0, 0, tzNYC), departures[8].Time)
}
// Headsign can be set on trips, and on stop_times. The latter
// overrides the former.
func testStaticDeparturesStopTimeWithHeadsignOverride(t *testing.T, backend string) {
// A single trip on Mondays, going through the alphabet
g := testutil.BuildStatic(t, backend, map[string][]string{
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"mondays,20200101,20201231,1,0,0,0,0,0,0",
},
"routes.txt": {"route_id,route_short_name,route_type", "alpha,alpha,3"},
"stops.txt": {
"stop_id,stop_name,stop_lat,stop_lon",
"A,A,1,2",
"B,B,1,1",
"C,C,2,2",
"D,D,3,3",
"E,E,4,4",
"F,F,5,5",
},
"trips.txt": {
"trip_id,route_id,service_id,direction_id,trip_headsign",
"alphabet,alpha,mondays,0,To Z",
},
"stop_times.txt": {
"trip_id,stop_id,departure_time,arrival_time,stop_headsign,stop_sequence",
"alphabet,A,6:10:0,6:10:0,,1",
"alphabet,B,6:11:0,6:11:0,,2",
"alphabet,C,6:12:0,6:12:0,,3",
"alphabet,D,6:13:0,6:13:0,To F,4",
"alphabet,E,6:14:0,6:14:0,To F,5",
"alphabet,F,6:14:0,6:14:0,To nowhere,6",
},
})
// Feb 3rd is a Monday.
for _, test := range []struct {
StopID string
ExpectedHeadsign string
}{
{"A", "To Z"},
{"B", "To Z"},
{"C", "To Z"},
{"D", "To F"},
{"E", "To F"},
} {
departures, err := g.Departures(
test.StopID,
time.Date(2020, 2, 3, 6, 0, 0, 0, time.UTC),
30*time.Minute,
-1,
"",
-1,
nil,
)
assert.NoError(t, err)
assert.Equal(t, 1, len(departures))
assert.Equal(t, test.StopID, departures[0].StopID)
assert.Equal(t, test.ExpectedHeadsign, departures[0].Headsign)
}
// And nothing departs from F
departures, err := g.Departures("F", time.Date(2020, 2, 3, 6, 0, 0, 0, time.UTC), 30*time.Minute, -1, "", -1, nil)
assert.NoError(t, err)
assert.Equal(t, 0, len(departures))
}
// Verifies that departures can be retrieved both for individual stops
// and for their parent stations (if any)
func testStaticDeparturesWithParentStations(t *testing.T, backend string) {
g := testutil.BuildStatic(t, backend, map[string][]string{
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"mondays,20200101,20201231,1,0,0,0,0,0,0",
},
"routes.txt": {"route_id,route_short_name,route_type", "alpha,a,0"},
"stops.txt": {
"stop_id,stop_name,location_type,parent_station,stop_lat,stop_lon",
"a,a,1,,1,1", // a is a station
"A,A,0,a,2,2", // A is a stop at a
"B,B,0,,3,3", // B is a stop, without parent
"c,c,1,,4,4", // c is a station
"C,C,0,c,5,5", // C is a stop at c
"d,d,1,,6,6", // d is a station
"D,D,0,d,7,7", // D is a stop at d
"E,E,0,,8,8", // E is a stop, without parent
},
"trips.txt": {
"trip_id,route_id,service_id,direction_id,trip_headsign",
"alphabet,alpha,mondays,0,To Z",
},
"stop_times.txt": {
"trip_id,stop_id,departure_time,arrival_time,stop_sequence",
"alphabet,A,6:10:0,6:10:0,1",
"alphabet,B,6:11:0,6:11:0,2",
"alphabet,C,6:12:0,6:12:0,3",
"alphabet,D,6:13:0,6:13:0,4",
"alphabet,E,6:14:0,6:14:0,5",
},
})
getDeps := func(stopID string) []model.Departure {
// Feb 3rd is a Monday.
departures, err := g.Departures(
stopID,
time.Date(2020, 2, 3, 6, 0, 0, 0, time.UTC),
30*time.Minute,
-1, "", -1, nil,
)
assert.NoError(t, err)
return departures
}
// IDentical result hitting parent stations or individual stop
assert.Equal(t, getDeps("A"), getDeps("a"))
assert.Equal(t, getDeps("C"), getDeps("c"))
assert.Equal(t, getDeps("D"), getDeps("d"))
assert.Equal(t, "A", getDeps("A")[0].StopID)
assert.Equal(t, "B", getDeps("B")[0].StopID)
assert.Equal(t, "C", getDeps("C")[0].StopID)
assert.Equal(t, "D", getDeps("D")[0].StopID)
}
func testStaticDeparturesDaylightsSavings(t *testing.T, backend string) {
// Two trips, one early in morning, one so late that it
// overflows into next day. Each on a separate route.
g := testutil.BuildStatic(t, backend, map[string][]string{
"agency.txt": {
"agency_id,agency_name,agency_url,agency_timezone",
"1,MTA,http://mta.com/,America/New_York",
},
"calendar.txt": {
"service_id,start_date,end_date,monday,tuesday,wednesday,thursday,friday,saturday,sunday",
"all,20200101,20201231,1,1,1,1,1,1,1",
},
"routes.txt": {
"route_id,route_long_name,route_type",
"L_early,The L Early,3",
"L_late,The L Late,3",
},
"stops.txt": {
"stop_id,stop_name,stop_lat,stop_lon",
"8av,8th Avenue,40.739777,-74.002578",
"6av,6th Avenue,40.737741,-73.996948",
"14st,14th Street,40.738228,-73.996209",
"3av,3rd Avenue,40.732849,-73.989433",
"1av,1st Avenue,40.730953,-73.981628",
"bedford,Bedford Avenue,40.717304,-73.956872",
},
"trips.txt": {
"trip_id,route_id,service_id,trip_headsign,direction_id,block_id",
"L_early,L_early,all,8av,0,1",
"L_late,L_late,all,8av,0,1",
},
"stop_times.txt": {
"trip_id,arrival_time,departure_time,stop_id,stop_sequence",
"L_early,00:00:00,00:00:00,8av,1",
"L_early,01:00:00,01:00:00,6av,2",
"L_early,02:00:00,02:00:00,14st,3",
"L_early,03:00:00,03:00:00,3av,4",
"L_early,04:00:00,04:00:00,1av,5",
"L_early,05:00:00,05:00:00,bedford,6",
"L_late,24:00:00,24:00:00,8av,1",
"L_late,25:00:00,25:00:00,6av,2",
"L_late,26:00:00,26:00:00,14st,3",
"L_late,27:00:00,27:00:00,3av,4",
"L_late,28:00:00,28:00:00,1av,5",
"L_late,29:00:00,29:00:00,bedford,6",
},
})
// Stuff can get weird around transition to and from daylight
// savings time. Times gets "pushed" 1h in some direction on
// day of the switch, as noon-12+time in the new "timezone" is
// different.
// In 2020, DST begun March 8 (at 2am, clocks went forward to
// 3am), and ended November 1 (at 2am, clocks went back to
// 1am).
for _, tc := range []struct {
name string
routeID string