-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstation_data_test.go
190 lines (173 loc) · 6.89 KB
/
station_data_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
package njtapi
import (
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func ExampleClient_StationData() {
// Update these values.
username := "your username"
password := "your password"
client := NewClient("http://njttraindata_tst.njtransit.com:8090/njttraindata.asmx/", username, password)
station, err := client.StationData(context.Background(), "NY")
if err != nil {
log.Fatalf("StationData() error: %v", err)
}
for _, departures := range station.Departures {
fmt.Printf("Train to %s at %s", departures.Destination, departures.ScheduledDepartureDate)
}
}
func TestStationList(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "testdata/getStationList.xml")
}))
defer ts.Close()
c := NewClient(ts.URL, "username", "pa$$word")
got, err := c.StationList(context.Background())
if err != nil {
t.Errorf("StationList() got unexpected error: %v", err)
}
want := []Station{
{ID: "NY", Name: "New York", Aliases: []string{"New York Penn Station"}},
{ID: "NP", Name: "Newark Penn", Aliases: []string{"Newark Penn Station"}},
{ID: "SE", Name: "Secaucus", Aliases: []string{"Secaucus Upper Lvl"}},
{ID: "TS", Name: "Secaucus", Aliases: []string{"Secaucus Lower Lvl"}},
{ID: "WL", Name: "Woodcliff Lake"},
{ID: "SC", Name: ""},
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("StationList() mismatch (-want +got):\n%s", diff)
}
}
func TestStationListError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer ts.Close()
c := NewClient(ts.URL, "username", "pa$$word")
if _, err := c.StationList(context.Background()); err == nil {
t.Error("StationList() expected error, got none.")
}
}
func TestStationData(t *testing.T) {
loc, err := time.LoadLocation("America/New_York")
if err != nil {
t.Fatalf("Error loading timezones: %v", err)
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
t.Errorf("Error parsing request: %v", err)
}
if u, p := r.Form.Get("username"), r.Form.Get("password"); u != "username" || p != "pa$$word" {
t.Errorf("Missing expected username & password: %v", r.Form)
}
s := r.Form.Get("station")
switch s {
case "SE":
http.ServeFile(w, r, "testdata/getTrainSchedule1.xml")
case "NY":
http.ServeFile(w, r, "testdata/getTrainSchedule2.xml")
}
}))
defer ts.Close()
c := NewClient(ts.URL, "username", "pa$$word")
for _, r := range []struct {
station string
want *Station
}{
{
station: "SE",
want: &Station{
ID: "SE",
Name: "Secaucus",
Departures: []StationTrain{
{
Index: 0,
TrainID: 3883,
Line: "Northeast Corridor Line",
LineAbbrv: "NEC",
Destination: "Trenton ✈",
ScheduledDepartureDate: time.Date(2019, 11, 18, 20, 17, 0, 0, loc),
Track: "B",
Status: "in 4 Min",
SecondsLate: 4 * time.Minute,
LatLng: &LatLng{Lat: 40.7706, Lng: -74.0403},
LatLngTimestamp: time.Date(2019, 11, 18, 20, 16, 45, 0, loc),
InlineMsg: "",
Stops: []StationStop{
{Name: "New York Penn Station", Time: time.Date(2019, 11, 18, 20, 7, 0, 0, loc), Departed: true},
{Name: "Secaucus Upper Lvl", Time: time.Date(2019, 11, 18, 20, 20, 30, 0, loc), Departed: false},
{Name: "Newark Penn Station", Time: time.Date(2019, 11, 18, 20, 28, 45, 0, loc), Departed: false},
{Name: "Newark Airport", Time: time.Date(2019, 11, 18, 20, 35, 0, 0, loc), Departed: false},
{Name: "North Elizabeth", Time: time.Date(2019, 11, 18, 20, 38, 45, 0, loc), Departed: false},
{Name: "Elizabeth", Time: time.Date(2019, 11, 18, 20, 41, 30, 0, loc), Departed: false},
{Name: "Linden", Time: time.Date(2019, 11, 18, 20, 46, 45, 0, loc), Departed: false},
{Name: "Rahway", Time: time.Date(2019, 11, 18, 20, 51, 00, 0, loc), Departed: false},
{Name: "Metropark", Time: time.Date(2019, 11, 18, 20, 59, 45, 0, loc), Departed: false},
{Name: "Metuchen", Time: time.Date(2019, 11, 18, 21, 04, 15, 0, loc), Departed: false},
{Name: "Edison", Time: time.Date(2019, 11, 18, 21, 9, 15, 0, loc), Departed: false},
{Name: "New Brunswick", Time: time.Date(2019, 11, 18, 21, 13, 30, 0, loc), Departed: false},
{Name: "Jersey Avenue", Time: time.Date(2019, 11, 18, 21, 18, 15, 0, loc), Departed: false},
{Name: "Princeton Junction", Time: time.Date(2019, 11, 18, 21, 30, 45, 0, loc), Departed: false},
{Name: "Hamilton", Time: time.Date(2019, 11, 18, 21, 37, 15, 0, loc), Departed: false},
{Name: "Trenton", Time: time.Date(2019, 11, 18, 21, 50, 15, 0, loc), Departed: false},
},
}, {
Index: 1,
TrainID: 3283,
Line: "North Jersey Coast Line",
LineAbbrv: "NJCL",
Destination: "Long Branch-BH ✈",
ScheduledDepartureDate: time.Date(2019, 11, 18, 20, 31, 30, 0, loc),
Track: "B",
LatLngTimestamp: time.Date(2019, 11, 18, 20, 05, 33, 0, loc),
InlineMsg: "",
Stops: []StationStop{
{Name: "New York Penn Station", Time: time.Date(2019, 11, 18, 20, 22, 0, 0, loc), Departed: false},
{Name: "Secaucus Upper Lvl", Time: time.Date(2019, 11, 18, 20, 31, 0, 0, loc), Departed: false},
},
},
},
},
}, {
station: "NY",
want: &Station{
ID: "NY",
Name: "New York",
Departures: []StationTrain{
// The firsr train, index 0, is skipped because it's an Amtrak train.
{
Index: 1,
TrainID: 3283,
Line: "North Jersey Coast Line",
LineAbbrv: "NJCL",
Destination: "Long Branch-BH -SEC ✈",
ScheduledDepartureDate: time.Date(2019, 11, 18, 20, 22, 0, 0, loc),
Track: "7",
Status: "BOARDING",
SecondsLate: -1 * time.Minute,
LatLngTimestamp: time.Date(2019, 11, 18, 20, 05, 33, 0, loc),
InlineMsg: "",
Stops: []StationStop{
{Name: "New York Penn Station", Time: time.Date(2019, 11, 18, 20, 22, 0, 0, loc), Departed: false},
{Name: "Secaucus Upper Lvl", Time: time.Date(2019, 11, 18, 20, 31, 0, 0, loc), Departed: false},
},
},
},
},
},
} {
got, err := c.StationData(context.Background(), r.station)
if err != nil {
t.Errorf("StationData(%s) unexpected error: %v", r.station, err)
}
if diff := cmp.Diff(r.want, got); diff != "" {
t.Errorf("StationData(%s) mismatch (-want +got):\n%s", r.station, diff)
}
}
}