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 pathtestunusual_trip_filter.py
119 lines (107 loc) · 5.04 KB
/
testunusual_trip_filter.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
#!/usr/bin/python2.4
#
# Copyright (C) 2009 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.
"""Tests for unusual_trip_filter.py"""
from __future__ import absolute_import
__author__ = 'Jiri Semecky <jiri.semecky@gmail.com>'
import unusual_trip_filter
import transitfeed
import unittest
from tests import util
class UnusualTripFilterTestCase(util.TempDirTestCaseBase):
"""Test of unusual trip filter functionality."""
def testFilter(self):
"""Test if filtering works properly."""
expected_values = {
'CITY1':0, 'CITY2':0, 'CITY3':0, 'CITY4' :0, 'CITY5' :0, 'CITY6' :0,
'CITY7':0, 'CITY8':0, 'CITY9':0, 'CITY10':0, 'CITY11':1, 'CITY12':1,
}
filter = unusual_trip_filter.UnusualTripFilter(0.1, quiet=True)
input = self.GetPath('tests', 'data', 'filter_unusual_trips')
loader = transitfeed.Loader(input, extra_validation=True)
schedule = loader.Load()
filter.filter(schedule)
for trip_id, expected_trip_type in expected_values.items():
actual_trip_type = schedule.trips[trip_id]['trip_type']
try:
self.assertEquals(int(actual_trip_type), expected_trip_type)
except ValueError:
self.assertEquals(actual_trip_type, '')
def testFilterNoForceFilter(self):
"""Test that force==False doesn't set default values"""
filter = unusual_trip_filter.UnusualTripFilter(0.1, force=False, quiet=True)
input = self.GetPath('tests', 'data', 'filter_unusual_trips')
loader = transitfeed.Loader(input, extra_validation=True)
schedule = loader.Load()
schedule.trips['CITY2'].trip_type = 'odd-trip'
filter.filter(schedule)
trip1 = schedule.trips['CITY1']
self.assertEquals(trip1['trip_type'], '')
trip2 = schedule.trips['CITY2']
self.assertEquals(trip2['trip_type'], 'odd-trip')
def testFilterForceFilter(self):
"""Test that force==True does set default values"""
filter = unusual_trip_filter.UnusualTripFilter(0.1, force=True, quiet=False)
input = self.GetPath('tests', 'data', 'filter_unusual_trips')
loader = transitfeed.Loader(input, extra_validation=True)
schedule = loader.Load()
schedule.trips['CITY2'].trip_type = 'odd-trip'
filter.filter(schedule)
trip1 = schedule.trips['CITY1']
self.assertEquals(trip1['trip_type'], '0')
trip2 = schedule.trips['CITY2']
self.assertEquals(trip2['trip_type'], '0')
def testFilterAppliedForSpecifiedRouteType(self):
"""Setting integer route_type filters trips of this route type."""
filter = unusual_trip_filter.UnusualTripFilter(0.1, quiet=True,
route_type=3)
input = self.GetPath('tests', 'data', 'filter_unusual_trips')
loader = transitfeed.Loader(input, extra_validation=True)
schedule = loader.Load()
filter.filter(schedule)
actual_trip_type = schedule.trips['CITY11']['trip_type']
self.assertEquals(actual_trip_type, '1')
def testFilterNotAppliedForUnspecifiedRouteType(self):
"""Setting integer route_type filters trips of this route type."""
filter = unusual_trip_filter.UnusualTripFilter(0.1, quiet=True,
route_type=2)
input = self.GetPath('tests', 'data', 'filter_unusual_trips')
loader = transitfeed.Loader(input, extra_validation=True)
schedule = loader.Load()
filter.filter(schedule)
actual_trip_type = schedule.trips['CITY11']['trip_type']
self.assertEquals(actual_trip_type, '')
def testFilterAppliedForRouteTypeSpecifiedByName(self):
"""Setting integer route_type filters trips of this route type."""
filter = unusual_trip_filter.UnusualTripFilter(0.1, quiet=True,
route_type='Bus')
input = self.GetPath('tests', 'data', 'filter_unusual_trips')
loader = transitfeed.Loader(input, extra_validation=True)
schedule = loader.Load()
filter.filter(schedule)
actual_trip_type = schedule.trips['CITY11']['trip_type']
self.assertEquals(actual_trip_type, '1')
def testFilterNotAppliedForDifferentRouteTypeSpecifiedByName(self):
"""Setting integer route_type filters trips of this route type."""
filter = unusual_trip_filter.UnusualTripFilter(0.1, quiet=True,
route_type='Ferry')
input = self.GetPath('tests', 'data', 'filter_unusual_trips')
loader = transitfeed.Loader(input, extra_validation=True)
schedule = loader.Load()
filter.filter(schedule)
actual_trip_type = schedule.trips['CITY11']['trip_type']
self.assertEquals(actual_trip_type, '')
if __name__ == '__main__':
unittest.main()