metra_schedule 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,82 @@
1
+ require 'test/unit'
2
+ require 'fileutils'
3
+
4
+ require File.join(File.dirname(__FILE__), "../lib", "metra")
5
+
6
+ class TestLine < Test::Unit::TestCase
7
+
8
+ def cleanup_dir
9
+ FileUtils.rmtree(MetraSchedule::Cacher.new.cache_dir)
10
+ end
11
+
12
+ def test_init
13
+ assert_nothing_raised do
14
+ MetraSchedule::Cacher.new
15
+ end
16
+ end
17
+
18
+ def test_check_for_and_create_metra_cache_dir
19
+ cleanup_dir
20
+ c = MetraSchedule::Cacher.new
21
+ assert_equal(false, c.check_for_metra_cache_dir)
22
+ c.create_metra_cache_dir
23
+ assert_equal(true, c.check_for_metra_cache_dir)
24
+ cleanup_dir
25
+ end
26
+
27
+ def test_create_cache_dir_if_not_exists
28
+ cleanup_dir
29
+ c = MetraSchedule::Cacher.new
30
+ assert_equal(false, c.check_for_metra_cache_dir)
31
+ assert_equal(true, c.create_cache_dir_if_not_exists)
32
+ cleanup_dir
33
+ end
34
+
35
+ def test_check_if_line_cache_file_exists
36
+ cleanup_dir
37
+ c = MetraSchedule::Cacher.new
38
+ l = Metra.new.line(:up_nw)
39
+ c.create_cache_dir_if_not_exists
40
+ assert_equal(false, c.line_exists?(l))
41
+ cleanup_dir
42
+ end
43
+
44
+ def test_create_engine_cache
45
+ cleanup_dir
46
+ c = MetraSchedule::Cacher.new
47
+ line = Metra.new.line(:up_nw)
48
+ c.create_cache_dir_if_not_exists
49
+
50
+ stop1 = MetraSchedule::Stop.new :station => :barrington, :time => Time.parse('12:30')
51
+ stop2 = MetraSchedule::Stop.new :station => :arlington_heights, :time => Time.parse('12:30')
52
+ stop3 = MetraSchedule::Stop.new :station => :ogilve, :time => Time.parse('13:30')
53
+ train1 = MetraSchedule::Train.new :stops => [stop1, stop2, stop3], :direction => :outbound, :schedule => :weekday
54
+ train2 = MetraSchedule::Train.new :stops => [stop1, stop3], :direction => :outbound, :schedule => :weekday
55
+ train3 = MetraSchedule::Train.new :stops => [stop2, stop3], :direction => :outbound, :schedule => :weekday
56
+ line.engines = [train1, train2, train3]
57
+
58
+ assert_equal(true, c.persist_line(line))
59
+ assert_equal(line.engines.count, c.retrieve_line(line).engines.count)
60
+ cleanup_dir
61
+ end
62
+
63
+ def test_load_cache
64
+ cleanup_dir
65
+ c = MetraSchedule::Cacher.new
66
+ line = Metra.new.line(:up_nw)
67
+ c.create_cache_dir_if_not_exists
68
+
69
+ stop1 = MetraSchedule::Stop.new :station => :barrington, :time => Time.parse('12:30')
70
+ stop2 = MetraSchedule::Stop.new :station => :arlington_heights, :time => Time.parse('12:30')
71
+ stop3 = MetraSchedule::Stop.new :station => :ogilve, :time => Time.parse('13:30')
72
+ train1 = MetraSchedule::Train.new :stops => [stop1, stop2, stop3], :direction => :outbound, :schedule => :weekday
73
+ train2 = MetraSchedule::Train.new :stops => [stop1, stop3], :direction => :outbound, :schedule => :weekday
74
+ train3 = MetraSchedule::Train.new :stops => [stop2, stop3], :direction => :outbound, :schedule => :weekday
75
+ line.engines = [train1, train2, train3]
76
+
77
+ assert_equal(true, MetraSchedule::Cacher.store_to_cache(line))
78
+ assert_equal(c.retrieve_line(line).engines.count, MetraSchedule::Cacher.load_from_cache(line).engines.count)
79
+ cleanup_dir
80
+ end
81
+
82
+ end
data/test/test_line.rb ADDED
@@ -0,0 +1,253 @@
1
+ require 'test/unit'
2
+ require File.join(File.dirname(__FILE__), "../lib", "metra")
3
+
4
+ class TestLine < Test::Unit::TestCase
5
+
6
+ # INTEGRATION:
7
+ # def test_update_schedule
8
+ # line = Metra.new.line(:up_nw).outbound
9
+ # line.update_schedule
10
+ # assert_not_nil(line.trains)
11
+ # end
12
+
13
+ def test_initialize_with_line_name
14
+ assert_nothing_raised do
15
+ Metra.new.line(:up_nw)
16
+ end
17
+ assert_raise ArgumentError do
18
+ Metra.new.line("blah")
19
+ end
20
+ end
21
+
22
+ def test_initialize_with_incorrect_line_symbol
23
+ assert_raise ArgumentError do
24
+ Metra.new.line(:blah)
25
+ end
26
+ end
27
+
28
+ def test_has_name_and_url
29
+ line = Metra.new.line(:up_nw)
30
+ assert_equal("Union Pacific Northwest", line.name)
31
+ assert_equal("http://metrarail.com/metra/en/home/maps_schedules/metra_system_map/up-nw/schedule.full.html", line.url)
32
+ end
33
+
34
+ def test_direction
35
+ line = Metra.new.line(:up_nw)
36
+ assert_nothing_raised do
37
+ line.direction :outbound
38
+ end
39
+ assert_equal(:outbound, line.dir)
40
+ assert_nothing_raised do
41
+ line.direction :inbound
42
+ end
43
+ assert_equal(:inbound, line.dir)
44
+ assert_equal(line.direction.class, Metra.new.line(:up_nw).class)
45
+ end
46
+
47
+ def test_direction_incorrect_argument
48
+ assert_raise ArgumentError do
49
+ line = Metra.new.line(:up_nw).direction(:blah)
50
+ end
51
+ end
52
+
53
+ def test_outbound
54
+ line = Metra.new.line(:up_nw)
55
+ assert_equal(line.direction(:outbound), line.outbound)
56
+ end
57
+
58
+ def test_inbound
59
+ line = Metra.new.line(:up_nw)
60
+ assert_equal(line.direction(:inbound), line.inbound)
61
+ end
62
+
63
+ def test_schedule
64
+ line = Metra.new.line(:up_nw)
65
+ assert_nothing_raised do
66
+ line.schedule :weekday
67
+ end
68
+ assert_equal(:weekday, line.sched)
69
+ assert_nothing_raised do
70
+ line.schedule :saturday
71
+ end
72
+ assert_equal(:saturday, line.sched)
73
+ assert_nothing_raised do
74
+ line.schedule :sunday
75
+ end
76
+ assert_equal(:sunday, line.sched)
77
+ assert_nothing_raised do
78
+ line.schedule :holiday
79
+ end
80
+ assert_equal(:holiday, line.sched)
81
+ assert_equal(line.schedule.class, Metra.new.line(:up_nw).class)
82
+ end
83
+
84
+ def test_weekday
85
+ line = Metra.new.line(:up_nw)
86
+ assert_equal(line.schedule(:weekday), line.weekday)
87
+ end
88
+
89
+ def test_saturday
90
+ line = Metra.new.line(:up_nw)
91
+ assert_equal(line.schedule(:saturday), line.saturday)
92
+ end
93
+
94
+ def test_sunday
95
+ line = Metra.new.line(:up_nw)
96
+ assert_equal(line.schedule(:sunday), line.sunday)
97
+ end
98
+
99
+ def test_holiday
100
+ line = Metra.new.line(:up_nw)
101
+ assert_equal(line.schedule(:holiday), line.holiday)
102
+ end
103
+
104
+ def test_set_station
105
+ line = Metra.new.line(:up_nw)
106
+ line.set_station(:start, :barrington)
107
+ assert_equal(:barrington, line.start)
108
+ line.set_station(:destination, :barrington)
109
+ assert_equal(:barrington, line.destination)
110
+ end
111
+
112
+ def test_set_station_invalid_station
113
+ line = Metra.new.line(:up_nw)
114
+ assert_raises ArgumentError do
115
+ line.set_station(:start, :bleepy_bleepy)
116
+ end
117
+ end
118
+
119
+ def test_from
120
+ line = Metra.new.line(:up_nw)
121
+ assert_equal(line.set_station(:start, :barrington), line.from(:barrington))
122
+ end
123
+
124
+ def test_to
125
+ line = Metra.new.line(:up_nw)
126
+ assert_equal(line.set_station(:destination, :ogilve), line.to(:ogilve))
127
+ end
128
+
129
+ def test_at
130
+ line = Metra.new.line(:up_nw)
131
+ test_time = "12:30PM"
132
+ assert_nothing_raised do
133
+ line.at(test_time)
134
+ line.at(Time.now)
135
+ end
136
+ assert_equal(line.time, Time.parse(test_time))
137
+ assert_raises ArgumentError do
138
+ line.at(:bloopybloop)
139
+ end
140
+ end
141
+
142
+ def test_deduce_direction
143
+ line = Metra.new.line(:up_nw)
144
+
145
+ assert_equal(:outbound, line.from(:ogilve).to(:barrington).deduce_direction)
146
+ assert_equal(:inbound, line.from(:barrington).to(:ogilve).deduce_direction)
147
+ end
148
+
149
+ def test_trains_filter_by_station
150
+ line = Metra.new.line(:up_nw)
151
+
152
+ stop1 = MetraSchedule::Stop.new :station => :barrington, :time => Time.parse('12:30')
153
+ stop2 = MetraSchedule::Stop.new :station => :arlington_heights, :time => Time.parse('12:30')
154
+ stop3 = MetraSchedule::Stop.new :station => :ogilve, :time => Time.parse('13:30')
155
+ train1 = MetraSchedule::Train.new :stops => [stop1, stop2, stop3], :direction => :outbound, :schedule => :weekday
156
+ train2 = MetraSchedule::Train.new :stops => [stop1, stop3], :direction => :outbound, :schedule => :weekday
157
+ train3 = MetraSchedule::Train.new :stops => [stop2, stop3], :direction => :outbound, :schedule => :weekday
158
+ line.engines = [train1, train2, train3]
159
+
160
+ valid_trains = line.to(:barrington).from(:ogilve).trains
161
+ assert_equal([train1, train2], valid_trains)
162
+ end
163
+
164
+ def test_trains_filter_by_start
165
+ line = Metra.new.line(:up_nw)
166
+
167
+ stop1 = MetraSchedule::Stop.new :station => :arlington_heights, :time => Time.parse('12:30')
168
+ stop2 = MetraSchedule::Stop.new :station => :arlington_heights, :time => Time.parse('12:40')
169
+ stop3 = MetraSchedule::Stop.new :station => :ogilve, :time => Time.parse('13:30')
170
+ train1 = MetraSchedule::Train.new :stops => [stop1, stop3], :direction => :inbound, :schedule => :weekday
171
+ train2 = MetraSchedule::Train.new :stops => [stop2, stop3], :direction => :inbound, :schedule => :weekday
172
+ line.engines = [train1, train2]
173
+
174
+ valid_trains = line.to(:ogilve).from(:arlington_heights).at('12:35').trains
175
+ assert_equal([train2], valid_trains)
176
+ end
177
+
178
+ def test_trains_just_direction_and_time
179
+ line = Metra.new.line(:up_nw)
180
+
181
+ stop1 = MetraSchedule::Stop.new :station => :barrington, :time => Time.parse('12:30')
182
+ stop2 = MetraSchedule::Stop.new :station => :arlington_heights, :time => Time.parse('12:30')
183
+ stop3 = MetraSchedule::Stop.new :station => :ogilve, :time => Time.parse('13:30')
184
+ train1 = MetraSchedule::Train.new :stops => [stop1, stop2, stop3], :direction => :outbound, :schedule => :weekday
185
+ train2 = MetraSchedule::Train.new :stops => [stop1, stop3], :direction => :outbound, :schedule => :weekday
186
+ train3 = MetraSchedule::Train.new :stops => [stop2, stop3], :direction => :outbound, :schedule => :weekday
187
+ line.engines = [train1, train2, train3]
188
+
189
+ valid_trains = line.outbound.at('12:00').trains
190
+ assert_equal([train1, train2, train3], valid_trains)
191
+ end
192
+
193
+ def test_filter_by_direction
194
+ line = Metra.new.line(:up_nw)
195
+
196
+ stop1 = MetraSchedule::Stop.new :station => :barrington, :time => Time.parse('12:30')
197
+ stop2 = MetraSchedule::Stop.new :station => :ogilve, :time => Time.parse('13:30')
198
+
199
+ train1 = MetraSchedule::Train.new :direction => :outbound, :stops => [stop1, stop2], :schedule => :weekday
200
+ train2 = MetraSchedule::Train.new :direction => :inbound, :stops => [stop1, stop2], :schedule => :weekday
201
+ train3 = MetraSchedule::Train.new :direction => :outbound, :stops => [stop1, stop2], :schedule => :weekday
202
+ line.engines = [train1, train2, train3]
203
+
204
+ valid_trains = line.to(:barrington).from(:ogilve).trains
205
+ assert_equal([train1, train3], valid_trains)
206
+ end
207
+
208
+ def test_filter_by_schedule
209
+ line = Metra.new.line(:up_nw)
210
+
211
+ stop1 = MetraSchedule::Stop.new :station => :barrington, :time => Time.parse('12:30')
212
+ stop2 = MetraSchedule::Stop.new :station => :ogilve, :time => Time.parse('13:30')
213
+
214
+ train1 = MetraSchedule::Train.new :direction => :outbound, :stops => [stop1, stop2], :schedule => :weekday
215
+ train2 = MetraSchedule::Train.new :direction => :outbound, :stops => [stop1, stop2], :schedule => :holiday
216
+ train3 = MetraSchedule::Train.new :direction => :outbound, :stops => [stop1, stop2], :schedule => :saturday
217
+ train4 = MetraSchedule::Train.new :direction => :outbound, :stops => [stop1, stop2], :schedule => :sunday
218
+ line.engines = [train1, train2, train3, train4]
219
+
220
+ valid_weekday = line.to(:barrington).from(:ogilve).weekday.trains
221
+ assert_equal([train1], valid_weekday)
222
+ valid_saturday = line.to(:barrington).from(:ogilve).saturday.trains
223
+ assert_equal([train3], valid_saturday)
224
+ valid_holiday = line.to(:barrington).from(:ogilve).holiday.trains
225
+ assert_equal([train2, train4], valid_holiday)
226
+ valid_sunday = line.to(:barrington).from(:ogilve).sunday.trains
227
+ assert_equal([train2, train4], valid_sunday)
228
+ end
229
+
230
+ def test_no_engines
231
+ line = Metra.new.line(:up_nw)
232
+
233
+ assert_nothing_raised do
234
+ line.outbound.trains
235
+ end
236
+ end
237
+
238
+ def test_all_trains_on_line
239
+ line = Metra.new.line(:up_nw)
240
+
241
+ stop1 = MetraSchedule::Stop.new :station => :barrington, :time => Time.parse('12:30')
242
+ stop2 = MetraSchedule::Stop.new :station => :ogilve, :time => Time.parse('13:30')
243
+
244
+ train1 = MetraSchedule::Train.new :direction => :inbound, :stops => [stop1, stop2], :schedule => :weekday
245
+ train2 = MetraSchedule::Train.new :direction => :outbound, :stops => [stop1, stop2], :schedule => :holiday
246
+ train3 = MetraSchedule::Train.new :direction => :inbound, :stops => [stop1, stop2], :schedule => :saturday
247
+ train4 = MetraSchedule::Train.new :direction => :outbound, :stops => [stop1, stop2], :schedule => :sunday
248
+ line.engines = [train1, train2, train3, train4]
249
+
250
+ assert_equal([train1, train2, train3, train4], line.trains)
251
+ end
252
+
253
+ end
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+ require File.join(File.dirname(__FILE__), "../lib", "metra")
3
+
4
+ class TestMetra < Test::Unit::TestCase
5
+ def test_metra_init
6
+ assert_nothing_raised do
7
+ @m = Metra.new
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,137 @@
1
+ require 'test/unit'
2
+ require File.join(File.dirname(__FILE__), "../lib", "metra")
3
+
4
+ class TestLine < Test::Unit::TestCase
5
+ include MetraSchedule::TrainData
6
+
7
+ def up_nw_stub
8
+ f = File.open(File.join(File.dirname(__FILE__), 'fixture/UP_NW.html'), 'r')
9
+ parser = MetraSchedule::Parser.new f
10
+ parser.line = LINES[:up_nw]
11
+ parser.scrape
12
+ parser
13
+ end
14
+
15
+ def test_init
16
+ assert_nothing_raised do
17
+ @@p = MetraSchedule::Parser.new 'http://blake.com'
18
+ end
19
+ assert_not_nil(up_nw_stub.html_doc)
20
+ assert_not_nil(@@p.html_doc)
21
+ end
22
+
23
+ def test_seperate_tables
24
+ p = up_nw_stub
25
+ assert_equal(2, p.tables.find_all {|t| t[:schedule] == :weekday}.count)
26
+ assert_equal(2, p.tables.find_all {|t| t[:schedule] == :saturday}.count)
27
+ assert_equal(2, p.tables.find_all {|t| t[:schedule] == :sunday}.count)
28
+ end
29
+
30
+ def test_sanitize
31
+ p = up_nw_stub
32
+ list = [1, 2, 3]
33
+ assert_equal(2, p.sanitize(list).count)
34
+ end
35
+
36
+ def test_train_count
37
+ p = up_nw_stub
38
+ assert_equal(16, p.train_count(p.tables[0][:tables][0]))
39
+ assert_equal(16, p.train_count(p.tables[0][:tables][1]))
40
+ assert_equal(12, p.train_count(p.tables[1][:tables][0]))
41
+ assert_equal(7, p.train_count(p.tables[2][:tables][0]))
42
+ assert_equal(16, p.train_count(p.tables[3][:tables][0]))
43
+ assert_equal(16, p.train_count(p.tables[3][:tables][1]))
44
+ assert_equal(1, p.train_count(p.tables[3][:tables][2]))
45
+ assert_equal(12, p.train_count(p.tables[4][:tables][0]))
46
+ end
47
+
48
+ def test_stop_count
49
+ p = up_nw_stub
50
+ assert_equal(23, p.stop_count(p.tables[0][:tables][0]))
51
+ end
52
+
53
+ def test_make_stop_inbound
54
+ p = up_nw_stub
55
+
56
+ node = p.tables[0][:tables][0].xpath("tbody[4]/tr/td[1]")[0]
57
+ expected = MetraSchedule::Stop.new :station => :crystal_lake, :time => Time.parse("4:47AM")
58
+ assert_equal(expected.station, p.make_stop(node, 3, :inbound).station)
59
+ assert_equal(expected.time, p.make_stop(node, 3, :inbound).time)
60
+ end
61
+
62
+ def test_make_stop_outbound
63
+ p = up_nw_stub
64
+
65
+ node = p.tables[3][:tables][0].xpath("tbody[4]/tr/td[1]")[0]
66
+ expected = MetraSchedule::Stop.new :station => :jefferson_park, :time => Time.parse("6:13AM")
67
+ assert_equal(expected.station, p.make_stop(node, 3, :outbound).station)
68
+ assert_equal(expected.time, p.make_stop(node, 3, :outbound).time)
69
+ end
70
+
71
+ def test_make_stop_nil
72
+ p = up_nw_stub
73
+
74
+ node = p.tables[0][:tables][0].xpath("tbody[1]/tr/td[1]")[0]
75
+ assert_nil(p.make_stop(node, 0, :inbound))
76
+ end
77
+
78
+ def test_make_stop_am
79
+ p = up_nw_stub
80
+
81
+ node = p.tables[0][:tables][1].xpath("tbody[18]/tr/td[5]")[0]
82
+ expected = MetraSchedule::Stop.new :station => :norwood_park, :time => Time.parse("11:57AM")
83
+ assert_equal(expected.station, p.make_stop(node, 17, :inbound).station)
84
+ assert_equal(expected.time, p.make_stop(node, 17, :inbound).time)
85
+ end
86
+
87
+ def test_make_stop_pm
88
+ p = up_nw_stub
89
+
90
+ node = p.tables[0][:tables][1].xpath("tbody[20]/tr/td[5]")[0]
91
+ expected = MetraSchedule::Stop.new :station => :jefferson_park, :time => Time.parse("12:01PM")
92
+ assert_equal(expected.station, p.make_stop(node, 19, :inbound).station)
93
+ assert_equal(expected.time, p.make_stop(node, 19, :inbound).time)
94
+ end
95
+
96
+ def test_find_stops
97
+ p = up_nw_stub
98
+
99
+ table = p.tables[0][:tables][0]
100
+ assert_equal(19, p.find_stops(table, 1, :inbound).count)
101
+ end
102
+
103
+ def test_find_stops_express
104
+ p = up_nw_stub
105
+
106
+ table = p.tables[0][:tables][0]
107
+ stops = p.find_stops(table, 5, :inbound)
108
+ assert_equal(Time.parse("6:43AM").to_f , stops[6].time.to_f)
109
+ assert_equal(:arlington_park, stops[6].station)
110
+ assert_equal(10, stops.count)
111
+ end
112
+
113
+ def test_make_trains_count
114
+ p = up_nw_stub
115
+ trains = p.make_trains
116
+ assert_equal(104, p.make_trains.count)
117
+ end
118
+
119
+ def test_find_train_num
120
+ p = up_nw_stub
121
+ train_602 = p.make_trains.first
122
+ train_632 = p.make_trains[15]
123
+
124
+ assert_equal(602, train_602.train_num)
125
+ assert_equal(632, train_632.train_num)
126
+ end
127
+
128
+ def test_find_bike_count
129
+ p = up_nw_stub
130
+ train_602 = p.make_trains.first
131
+ train_640 = p.make_trains[18]
132
+
133
+ assert_equal(nil, train_602.bike_limit)
134
+ assert_equal(12, train_640.bike_limit)
135
+ end
136
+
137
+ end