-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvvs_test.go
289 lines (254 loc) · 5.94 KB
/
vvs_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
package govvs
import (
"testing"
"time"
"github.com/ownerofglory/govvs/station"
)
func TestGetJourney(t *testing.T) {
testLimit := 4
testCases := []struct {
name string
orig string
dst string
limit *int
timeAt *time.Time
errExpected bool
}{
{
name: "When required params are given then journeys are returned",
orig: station.BOPSER_STUTTGART,
dst: station.HAUPTBAHNHOF_TIEF_STUTTGART,
errExpected: false,
},
{
name: "When required params and limit are given then journeys no more than limit are returned",
orig: station.BOPSER_STUTTGART,
dst: station.HAUPTBAHNHOF_TIEF_STUTTGART,
errExpected: false,
limit: &testLimit,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := JourneyRequest{
OrigId: tc.orig,
DstId: tc.dst,
}
if tc.limit != nil {
req.Limit = tc.limit
}
if tc.timeAt != nil {
req.TimeAt = tc.timeAt
}
res, err := GetJourney(req)
if !tc.errExpected && err != nil {
t.Fatalf("Error occured: %v\n", err)
}
if tc.limit != nil && len(res.Journeys) > *tc.limit {
t.Fatalf("Error occured: %v\n", err)
}
if res == nil {
t.Fatalf("Response is nil")
}
})
}
}
func TestGetArrivals(t *testing.T) {
testLimit := 4
testCases := []struct {
name string
stationId string
limit *int
timeAt *time.Time
errExpected bool
}{
{
name: "When required params are given then arrivals are returned",
stationId: station.BOPSER_STUTTGART,
errExpected: false,
},
{
name: "When required params and limit are given then arrivals no more than limit are returned",
stationId: station.BOPSER_STUTTGART,
errExpected: false,
limit: &testLimit,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := ArrivalRequest{
StationId: tc.stationId,
}
if tc.limit != nil {
req.Limit = tc.limit
}
if tc.timeAt != nil {
req.TimeAt = tc.timeAt
}
res, err := GetArrivals(req)
if !tc.errExpected && err != nil {
t.Fatalf("Error occured: %v\n", err)
}
if tc.limit != nil && len(res.ArrivalList) > *tc.limit {
t.Fatalf("Error occured: %v\n", err)
}
if res == nil {
t.Fatalf("Response is nil")
}
})
}
}
func TestGetDepartures(t *testing.T) {
testLimit := 4
testCases := []struct {
name string
stationId string
limit *int
timeAt *time.Time
errExpected bool
}{
{
name: "When required params are given then departures are returned",
stationId: station.BOPSER_STUTTGART,
errExpected: false,
},
{
name: "When required params and limit are given then departures no more then limit are returned",
stationId: station.BOPSER_STUTTGART,
errExpected: false,
limit: &testLimit,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := DepartureRequest{
StationId: tc.stationId,
}
if tc.limit != nil {
req.Limit = tc.limit
}
if tc.timeAt != nil {
req.TimeAt = tc.timeAt
}
res, err := GetDepartures(req)
if !tc.errExpected && err != nil {
t.Fatalf("Error occured: %v\n", err)
}
if tc.limit != nil && len(res.DepartureList) > *tc.limit {
t.Fatalf("Error occured: %v\n", err)
}
if res == nil {
t.Fatalf("Response is nil")
}
})
}
}
func TestGetStopFinder(t *testing.T) {
testCases := []struct {
name string
stopName string
stopType string
lang *string
useRealtime *bool
errExpected bool
}{
{
name: "When required params are given then stop information is returned",
stopName: "Königstr. 1",
stopType: "any",
errExpected: false,
},
{
name: "When real-time data is requested then real-time information is returned",
stopName: "Königstr. 1",
stopType: "any",
useRealtime: boolPointer(true),
errExpected: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := StopFinderRequest{
Name: tc.stopName,
Type: tc.stopType,
}
if tc.lang != nil {
req.Language = tc.lang
}
if tc.useRealtime != nil {
req.UseRealtime = tc.useRealtime
}
res, err := GetStopFinder(req)
// Check for errors
if !tc.errExpected && err != nil {
t.Fatalf("Error occurred: %v\n", err)
}
// Check that response is not nil
if res == nil && !tc.errExpected {
t.Fatalf("Expected response but got nil")
}
// Check that if an error is expected, no result should be returned
if tc.errExpected && res != nil {
t.Fatalf("Expected error but got valid response")
}
})
}
}
func TestGetGeoObject(t *testing.T) {
testCases := []struct {
name string
lineID string
expLineName string
}{
{
name: "if valid line id is given geo object is returned",
lineID: "vvs:20007:+:H:j24:1",
expLineName: "U7",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := GeoObjectRequest{
LineID: tc.lineID,
}
geoObj, err := GetGeoObject(req)
if err != nil {
t.Fatalf("Error occured: %v\n", err)
}
if geoObj.Transportations[0].Number != tc.expLineName {
t.Fatalf("Line doesn't match")
}
})
}
}
func TestGetServingLines(t *testing.T) {
testCases := []struct {
name string
lineName string
expLineName string
}{
{
name: "if valid line id is given serving line is returned",
lineName: "U7",
expLineName: "U7",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := ServingLinesRequest{
LineName: tc.lineName,
}
srvLines, err := GetServingLines(req)
if err != nil {
t.Fatalf("Error occured: %v\n", err)
}
if srvLines == nil {
t.Fatalf("Returned value is nil")
}
})
}
}
// Helper function to create a pointer for boolean values
func boolPointer(b bool) *bool {
return &b
}