-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_integration_test.go
210 lines (195 loc) · 6.29 KB
/
static_integration_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
package gtfs_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"tidbyt.dev/gtfs/model"
"tidbyt.dev/gtfs/testutil"
)
func testGTFSStaticIntegrationNearbyStops(t *testing.T, backend string) {
if testing.Short() {
t.Skip("loading MTA dump is slow")
}
// This is a giant GTFS file from the MTA
g := testutil.LoadStaticFile(t, backend, "testdata/mta_static.zip")
// The 4 nearest stops for 544 Park Ave, BK. There are other
// stops with the same coordinates, but they all have
// location_type==0 and reference one of these 4 as parent
// station.
stops, err := g.NearbyStops(40.6968986, -73.955555, 4, nil)
assert.NoError(t, err)
stopMap := make(map[string]model.Stop)
for _, s := range stops {
stopMap[s.ID] = s
}
assert.Equal(t, map[string]model.Stop{
"G31": {
ID: "G31",
Lat: 40.700377,
Lon: -73.950234,
Name: "Flushing Av",
LocationType: 1,
ParentStation: "",
},
"G32": {
ID: "G32",
Lat: 40.694568,
Lon: -73.949046,
Name: "Myrtle - Willoughby Avs",
LocationType: 1,
ParentStation: "",
},
"G33": {
ID: "G33",
Lat: 40.689627,
Lon: -73.953522,
Name: "Bedford - Nostrand Avs",
LocationType: 1,
ParentStation: "",
},
"G34": {
ID: "G34",
Name: "Classon Av",
Lat: 40.688873,
Lon: -73.96007,
LocationType: 1,
ParentStation: "",
},
}, stopMap)
}
func testGTFSStaticIntegrationDepartures(t *testing.T, backend string) {
if testing.Short() {
t.Skip("loading MTA dump is slow")
}
// This is a giant GTFS file from the MTA
g := testutil.LoadStaticFile(t, backend, "testdata/mta_static.zip")
// Let's look at the G33S stop, also known as "Bedford -
// Nostrand Avs". Between 22:50 and 23:10 there are are 6
// stop_time records:
//
// trip_id,arrival_time,departure_time,stop_id,stop_sequence
// BFA19GEN-G035-Saturday-00_136750_G..S16R,23:02:00,23:02:30,G33S,9
// BFA19GEN-G035-Saturday-00_135750_G..S16R,22:52:00,22:52:00,G33S,9
// BFA19GEN-G036-Sunday-00_136550_G..S16R,23:00:00,23:00:00,G33S,9
// BFA19GEN-G051-Weekday-00_135800_G..S14R,22:50:30,22:50:30,G33S,9
// BFA19GEN-G051-Weekday-00_136700_G..S14R,22:59:30,22:59:30,G33S,9
// BFA19GEN-G051-Weekday-00_137600_G..S14R,23:08:30,23:08:30,G33S,9
//
// The first two share a prefix in their trip_id, as do the
// last final three. Inspecting the corresponding trip records
// reveals that these share service_id. These three have
// calendar records as follows:
//
// service_id,monday,tuesday,wednesday,thursday,friday,saturday,sunday,start_date,end_date
// BFA19GEN-G035-Saturday-00,0,0,0,0,0,1,0,20200104,20200502
// BFA19GEN-G036-Sunday-00,0,0,0,0,0,0,1,20200105,20200426
// BFA19GEN-G051-Weekday-00,1,1,1,1,1,0,0,20200102,20200501
//
// So during most of Jan - Apr, these stop_times should
// apply. However, there are also calendar_date records for
// two of these services:
//
// BFA19GEN-G035-Saturday-00,20200217,1
// BFA19GEN-G051-Weekday-00,20200217,2
//
// Together, these remove the Weekday schedule and add the
// Saturday schedule for February 17th (President's Day).
//
// The 6 trips all share a single headsign: "Church Av".
//
// Armed with this knowledge, we can now run some test
// queries.
tz, err := time.LoadLocation("America/New_York")
require.NoError(t, err)
// Feb 3rd is a Monday
departures, _ := g.Departures("G33S", time.Date(2020, 2, 3, 22, 50, 0, 0, tz), 10*time.Minute, -1, "", -1, nil)
assert.Equal(t, []model.Departure{
{
StopID: "G33S",
RouteID: "G",
TripID: "BFA19GEN-G051-Weekday-00_135800_G..S14R",
StopSequence: 9,
DirectionID: 1,
Time: time.Date(2020, 2, 3, 22, 50, 30, 0, tz),
Headsign: "Church Av",
},
{
StopID: "G33S",
RouteID: "G",
TripID: "BFA19GEN-G051-Weekday-00_136700_G..S14R",
StopSequence: 9,
DirectionID: 1,
Time: time.Date(2020, 2, 3, 22, 59, 30, 0, tz),
Headsign: "Church Av",
},
}, departures)
// Feb 17 is also a Monday, but President's Day
departures, _ = g.Departures("G33S", time.Date(2020, 2, 17, 22, 50, 0, 0, tz), 10*time.Minute, -1, "", -1, nil)
assert.Equal(t, []model.Departure{
{
StopID: "G33S",
RouteID: "G",
TripID: "BFA19GEN-G035-Saturday-00_135750_G..S16R",
StopSequence: 9,
DirectionID: 1,
Time: time.Date(2020, 2, 17, 22, 52, 0, 0, tz),
Headsign: "Church Av",
},
}, departures)
// So to get 2 stops w need a larger window. These appear in
// reverse order in stop_times.txt, but will be still be
// returned ordered by departure time.
departures, _ = g.Departures("G33S", time.Date(2020, 2, 17, 22, 50, 0, 0, tz), 13*time.Minute, -1, "", -1, nil)
assert.Equal(t, []model.Departure{
{
StopID: "G33S",
RouteID: "G",
TripID: "BFA19GEN-G035-Saturday-00_135750_G..S16R",
StopSequence: 9,
DirectionID: 1,
Time: time.Date(2020, 2, 17, 22, 52, 0, 0, tz),
Headsign: "Church Av",
},
{
StopID: "G33S",
RouteID: "G",
TripID: "BFA19GEN-G035-Saturday-00_136750_G..S16R",
StopSequence: 9,
DirectionID: 1,
Time: time.Date(2020, 2, 17, 23, 2, 30, 0, tz),
Headsign: "Church Av",
},
}, departures)
// Feb 16 is a Sunday
departures, _ = g.Departures("G33S", time.Date(2020, 2, 16, 22, 50, 0, 0, tz), 10*time.Minute, -1, "", -1, nil)
assert.Equal(t, []model.Departure{
{
StopID: "G33S",
RouteID: "G",
TripID: "BFA19GEN-G036-Sunday-00_136550_G..S16R",
StopSequence: 9,
DirectionID: 1,
Time: time.Date(2020, 2, 16, 23, 0, 0, 0, tz),
Headsign: "Church Av",
},
}, departures)
}
func TestStaticIntegration(t *testing.T) {
for _, test := range []struct {
Name string
Test func(*testing.T, string)
}{
{"GTFSStaticIntegrationNearbyStops", testGTFSStaticIntegrationNearbyStops},
{"GTFSStaticIntegrationDepartures", testGTFSStaticIntegrationDepartures},
} {
t.Run(test.Name+"_SQLite", func(t *testing.T) {
test.Test(t, "sqlite")
})
if testutil.PostgresConnStr != "" {
t.Run(test.Name+"_postgres", func(t *testing.T) {
test.Test(t, "postgres")
})
}
}
}