This repository has been archived by the owner on Nov 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathtestgoogletransitextension.py
359 lines (306 loc) · 14.8 KB
/
testgoogletransitextension.py
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
#!/usr/bin/python2.5
# Copyright (C) 2011 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Unit tests for the googletransit extension (extensions.googletransit)
from __future__ import absolute_import
import extensions.googletransit
import os
import re
import transitfeed
from tests.testfeedvalidator import FullTests
from tests.util import MemoryZipTestCase, ValidationTestCase
class ExtensionFullTests(FullTests):
"""Inherits FullTests from testfeedvalidator.py to test the extension
executable feedvalidator_googletransit.py. Tests the extension executable with
new good_feed test data which uses extension capabilities."""
feedvalidator_executable = 'feedvalidator_googletransit.py'
extension_name = 'extensions.googletransit'
additional_arguments = ['--error_types_ignore_list',
'DeprecatedColumn']
def testGoogleTransitGoodFeed(self):
(out, err) = self.CheckCallWithPath(
[self.GetPath(self.feedvalidator_executable), '-n', '--latest_version',
transitfeed.__version__] + self.additional_arguments +
[self.GetPath('tests', 'data', 'googletransit', 'good_feed')])
self.assertTrue(re.search(r'feed validated successfully', out))
self.assertFalse(re.search(r'ERROR', out))
htmlout = open('validation-results.html').read()
self.assertMatchesRegex(
self.extension_message + self.extension_name, htmlout)
self.assertTrue(re.search(r'feed validated successfully', htmlout))
self.assertFalse(re.search(r'ERROR', htmlout))
self.assertFalse(os.path.exists('transitfeedcrash.txt'))
class ExtensionMemoryZipTestCase(MemoryZipTestCase):
"""ExtendMemoryZipTestCase to also ignore DeprecatedColumn errors.
In this extension a couple of columns are set to be 'Deprecated'.
The 'normal' transitfeed test data used in some of the test cases here
however still uses these columns. As we can/should not modify the 'normal'
test data we are adding the 'DeprecatedColumn' to the _IGNORE_TYPES list.
"""
_IGNORE_TYPES = MemoryZipTestCase._IGNORE_TYPES + ["DeprecatedColumn"]
class FareAttributeAgencyIdTestCase(ExtensionMemoryZipTestCase):
gtfs_factory = extensions.googletransit.GetGtfsFactory()
def testNoErrorsWithOneAgencyAndNoIdAndAgencyIdColumnNotPresent(self):
self.SetArchiveContents(
"fare_attributes.txt",
"fare_id,price,currency_type,payment_method,transfers\n"
"fare1,1,EUR,1,0\n")
self.SetArchiveContents(
"agency.txt",
"agency_id,agency_name,agency_url,agency_timezone,agency_lang\n"
",Demo Agency,http://google.com,America/Los_Angeles,en\n")
self.SetArchiveContents(
"routes.txt",
"route_id,agency_id,route_short_name,route_long_name,route_type\n"
"AB,,,Airport Bullfrog,3\n")
self.MakeLoaderAndLoad(self.problems, gtfs_factory=self.gtfs_factory)
self.accumulator.AssertNoMoreExceptions()
def testNoErrorsWithOneAgencyAndNoIdAndAgencyIdColumnPresent(self):
self.SetArchiveContents(
"fare_attributes.txt",
"fare_id,price,currency_type,payment_method,transfers,agency_id\n"
"fare1,1,EUR,1,0,\n")
self.SetArchiveContents(
"agency.txt",
"agency_id,agency_name,agency_url,agency_timezone,agency_lang\n"
",Demo Agency,http://google.com,America/Los_Angeles,en\n")
self.SetArchiveContents(
"routes.txt",
"route_id,agency_id,route_short_name,route_long_name,route_type\n"
"AB,,,Airport Bullfrog,3\n")
self.MakeLoaderAndLoad(self.problems, gtfs_factory=self.gtfs_factory)
self.accumulator.AssertNoMoreExceptions()
def testNoErrorsWithSeveralAgencies(self):
self.SetArchiveContents(
"fare_attributes.txt",
"fare_id,price,currency_type,payment_method,transfers,agency_id\n"
"fare1,1,EUR,1,0,DTA\n"
"fare2,2,EUR,0,0,ATD\n")
self.SetArchiveContents(
"agency.txt",
"agency_id,agency_name,agency_url,agency_timezone,agency_lang\n"
"DTA,Demo Agency,http://google.com,America/Los_Angeles,en\n"
"ATD,Another Demo Agency,http://example.com,America/Los_Angeles,en\n")
self.MakeLoaderAndLoad(self.problems, gtfs_factory=self.gtfs_factory)
self.accumulator.AssertNoMoreExceptions()
def testWrongIdWithOneAgencyWithNoId(self):
self.SetArchiveContents(
"fare_attributes.txt",
"fare_id,price,currency_type,payment_method,transfers,agency_id\n"
"fare1,1,EUR,1,0,DOESNOTEXIST\n")
self.SetArchiveContents(
"routes.txt",
"route_id,agency_id,route_short_name,route_long_name,route_type\n"
"AB,,,Airport Bullfrog,3\n")
self.SetArchiveContents(
"agency.txt",
"agency_id,agency_name,agency_url,agency_timezone,agency_lang\n"
",Demo Agency,http://google.com,America/Los_Angeles,en\n")
self.MakeLoaderAndLoad(self.problems, gtfs_factory=self.gtfs_factory)
e = self.accumulator.PopException("InvalidAgencyID")
self.assertEquals('agency_id', e.column_name)
self.accumulator.AssertNoMoreExceptions()
def testWrongIdWithOneAgencyWithId(self):
self.SetArchiveContents("fare_attributes.txt",
"fare_id,price,currency_type,payment_method,transfers,agency_id\n"
"fare1,1,EUR,1,0,DOESNOTEXIST\n")
self.SetArchiveContents(
"agency.txt",
"agency_id,agency_name,agency_url,agency_timezone,agency_lang\n"
"DTA,Demo Agency,http://google.com,America/Los_Angeles,en\n")
self.MakeLoaderAndLoad(self.problems, gtfs_factory=self.gtfs_factory)
e = self.accumulator.PopException("InvalidAgencyID")
self.assertEquals('agency_id', e.column_name)
self.accumulator.AssertNoMoreExceptions()
def testWrongIdWithSeveralAgencies(self):
self.SetArchiveContents(
"fare_attributes.txt",
"fare_id,price,currency_type,payment_method,transfers,"
"agency_id\n"
"fare1,1,EUR,1,0,DTA\n"
"fare2,2,EUR,0,1,ATD\n"
"fare3,2,EUR,0,2,DOESNOTEXIST\n")
self.SetArchiveContents(
"agency.txt",
"agency_id,agency_name,agency_url,agency_timezone,agency_lang\n"
"DTA,Demo Agency,http://google.com,America/Los_Angeles,en\n"
"ATD,Another Demo Agency,http://example.com,America/Los_Angeles,en\n")
self.MakeLoaderAndLoad(self.problems, gtfs_factory=self.gtfs_factory)
e = self.accumulator.PopException("InvalidAgencyID")
self.assertEquals('agency_id', e.column_name)
self.accumulator.AssertNoMoreExceptions()
class StopExtensionIntegrationTestCase(ExtensionMemoryZipTestCase):
gtfs_factory = extensions.googletransit.GetGtfsFactory()
def testNoErrors(self):
self.SetArchiveContents("stops.txt",
"stop_id,stop_name,stop_lat,stop_lon,stop_timezone,location_type,"
"parent_station,vehicle_type\n"
"BEATTY,Beatty,36.868446,-116.784582,,1,,1100\n"
"BEATTY_AIRPORT,Airport West,36.868446,-116.784582,,2,BEATTY,\n"
"BULLFROG,Bullfrog,36.88108,-116.81797,,,,3\n"
"STAGECOACH,Stagecoach Hotel,36.915682,-116.751677,America/Los_Angeles,"
",,204\n")
self.MakeLoaderAndLoad(self.problems, gtfs_factory=self.gtfs_factory)
self.accumulator.AssertNoMoreExceptions()
def testInvalidVehicleType(self):
self.SetArchiveContents("stops.txt",
"stop_id,stop_name,stop_lat,stop_lon,stop_timezone,location_type,"
"parent_station,vehicle_type\n"
"BEATTY,Beatty,36.868446,-116.784582,,1,,2557\n" # bad vehicle type
"BEATTY_AIRPORT,Airport West,36.868446,-116.784582,,2,BEATTY,\n"
"BULLFROG,Bullfrog,36.88108,-116.81797,,,,3\n"
"STAGECOACH,Stagecoach Hotel,36.915682,-116.751677,America/Los_Angeles,"
",,204\n")
self.MakeLoaderAndLoad(self.problems, gtfs_factory=self.gtfs_factory)
self.accumulator.PopInvalidValue("vehicle_type")
self.accumulator.AssertNoMoreExceptions()
class StopExtensionTestCase(ValidationTestCase):
gtfs_factory = extensions.googletransit.GetGtfsFactory()
def setUp(self):
super(StopExtensionTestCase, self).setUp()
self._stop = self.gtfs_factory.Stop(
lng=1.00, lat=48.1, name="a stop", stop_id="stop")
self._stop._gtfs_factory = self.gtfs_factory
self._parent_stop = self.gtfs_factory.Stop(
lng=1.00, lat=48.2, name="parent stop", stop_id="parent_stop")
self._parent_stop._gtfs_factory = self.gtfs_factory
self._child_stop = self.gtfs_factory.Stop(
lng=1.00, lat=48.2, name="child stop", stop_id="child stop")
self._child_stop.parent_station = self._parent_stop.stop_id
self._child_stop._gtfs_factory = self.gtfs_factory
self._entrance = self.gtfs_factory.Stop(
lng=1.00, lat=48.2, name="an entrance", stop_id="entrance")
self._entrance.location_type = 2
self._entrance.parent_station = self._parent_stop.stop_id
self._entrance._gtfs_factory = self.gtfs_factory
def testValidateVehicleType(self):
# Test with non-integer value
self._stop.vehicle_type = 'abc'
self._stop.Validate(self.problems)
self.accumulator.PopInvalidValue('vehicle_type')
self.accumulator.AssertNoMoreExceptions()
# Test with not known value
self._stop.vehicle_type = 2547
self._stop.Validate(self.problems)
self.accumulator.PopInvalidValue('vehicle_type')
self.accumulator.AssertNoMoreExceptions()
def testEntranceExceptions(self):
# There should be no error validating _entrance
self._entrance.Validate(self.problems)
self.accumulator.AssertNoMoreExceptions()
# An entrance must not have a stop_timezone
self._entrance.stop_timezone = 'America/Los_Angeles'
self._entrance.Validate(self.problems)
e = self.accumulator.PopInvalidValue('stop_timezone')
self.assertMatchesRegex(r'stop_timezone', e.FormatProblem())
self.accumulator.AssertNoMoreExceptions()
self._entrance.stop_timezone = None
# An entrance must not have a vehicle type
self._entrance.vehicle_type = 200
self._entrance.Validate(self.problems)
e = self.accumulator.PopInvalidValue('vehicle_type')
self.accumulator.AssertNoMoreExceptions()
self._entrance.vehicle_type = None
# An entrance should have a parent station
self._entrance.parent_station = None
self._entrance.Validate(self.problems)
e = self.accumulator.PopInvalidValue('location_type')
self.assertMatchesRegex(r'parent_station', e.FormatProblem())
self.accumulator.AssertNoMoreExceptions()
def testChildExceptions(self):
# There should be no error validating _child_stop
self._child_stop.Validate(self.problems)
self.accumulator.AssertNoMoreExceptions()
# A _child_stop must not have a stop_timezone
self._child_stop.stop_timezone = 'America/Los_Angeles'
self._child_stop.Validate(self.problems)
e = self.accumulator.PopInvalidValue('stop_timezone')
self.assertMatchesRegex(r'stop_timezone', e.FormatProblem())
self.assertTrue(e.IsWarning())
self.accumulator.AssertNoMoreExceptions()
self._child_stop.stop_timezone = None
# Adding vehicle_type, Google transit doesn't read child stop vehicle types
self._child_stop.vehicle_type = 200
self._child_stop.Validate(self.problems)
e = self.accumulator.PopInvalidValue('vehicle_type')
self.assertTrue(e.IsWarning())
self.accumulator.AssertNoMoreExceptions()
self._child_stop.vehicle_type = None
def testAllowEmptyStopNameIfEntrance(self):
# Empty stop_name with default location_type=0 should report MissingValue
self._stop.stop_name = ''
self._stop.Validate(self.problems)
self.accumulator.PopMissingValue('stop_name')
self.accumulator.AssertNoMoreExceptions()
# Empty stop_name in a child stop should report MissingValue
self._child_stop.stop_name = ''
self._child_stop.Validate(self.problems)
self.accumulator.PopMissingValue('stop_name')
self.accumulator.AssertNoMoreExceptions()
# Empty stop_name with location_type=2 should report no errors
self._entrance.stop_name = ''
self._entrance.Validate(self.problems)
self.accumulator.AssertNoMoreExceptions()
class RouteExtensionIntegrationTestCase(ExtensionMemoryZipTestCase):
gtfs_factory = extensions.googletransit.GetGtfsFactory()
def testNoErrors(self):
self.SetArchiveContents(
"routes.txt",
"route_id,agency_id,route_short_name,route_long_name,route_type,"
"co2_per_km\n"
"AB,DTA,,Airport Bullfrog,201,15.5\n")
self.MakeLoaderAndLoad(self.problems, gtfs_factory=self.gtfs_factory)
self.accumulator.AssertNoMoreExceptions()
def testInvalidCo2PerKm(self):
self.SetArchiveContents(
"routes.txt",
"route_id,route_short_name,route_long_name,route_type,co2_per_km\n"
"AB,,Airport Bullfrog,201,15.5mg\n")
self.MakeLoaderAndLoad(self.problems, gtfs_factory=self.gtfs_factory)
self.accumulator.PopInvalidValue("co2_per_km")
self.accumulator.AssertNoMoreExceptions()
def testInvalidRouteType(self):
self.SetArchiveContents(
"routes.txt",
"route_id,route_short_name,route_long_name,route_type,co2_per_km\n"
"AB,,Airport Bullfrog,2557,15.5\n")
self.MakeLoaderAndLoad(self.problems, gtfs_factory=self.gtfs_factory)
self.accumulator.PopInvalidValue("route_type")
self.accumulator.AssertNoMoreExceptions()
class AgencyLangTestCase(ExtensionMemoryZipTestCase):
gtfs_factory = extensions.googletransit.GetGtfsFactory()
def testNotWellFormedAgencyLang(self):
self.SetArchiveContents(
"agency.txt",
"agency_id,agency_name,agency_url,agency_timezone,agency_lang\n"
"DTA,Demo Agency,http://google.com,America/Los_Angeles,lang123456789\n")
self.MakeLoaderAndLoad(self.problems,
gtfs_factory=self.gtfs_factory)
e = self.accumulator.PopInvalidValue("agency_lang")
e_msg = e.FormatProblem()
self.assertTrue(e_msg.find('not well-formed') != -1,
'%s should not be well-formed, is: %s' % (e.value, e_msg))
self.accumulator.AssertNoMoreExceptions()
def testNotValidAgencyLang(self):
self.SetArchiveContents(
"agency.txt",
"agency_id,agency_name,agency_url,agency_timezone,agency_lang\n"
"DTA,Demo Agency,http://google.com,America/Los_Angeles,fra-XY\n")
self.MakeLoaderAndLoad(self.problems,
gtfs_factory=self.gtfs_factory)
e = self.accumulator.PopInvalidValue("agency_lang")
e_msg = e.FormatProblem()
self.assertTrue(e_msg.find('not valid') != -1,
'%s should not be valid, is: %s' % (e.value, e_msg))
self.accumulator.AssertNoMoreExceptions()