Departure object changes, tests, small fix
[pywienerlinien] / gotovienna / tests / realtime.py
1 # -*- coding: utf-8 -*-
2
3 from nose.tools import assert_equal, assert_true, assert_false, assert_is_instance
4 import sys
5 import os
6 from datetime import datetime
7 sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
8 DATAPATH = os.path.join(os.path.dirname(__file__), 'data')
9
10 # bananas for the monkey
11 class datetime_static(datetime):
12     @classmethod
13     def now(cls):
14         return datetime(2000, 1, 1, 11, 50) 
15
16 from gotovienna import realtime
17 realtime.datetime = datetime_static
18 from gotovienna.realtime import *
19 # </bananas>
20
21 parser = ITipParser()
22
23 stationbased = open(os.path.join(DATAPATH, 'stationbased.html'), 'r').read()
24 line_station = open(os.path.join(DATAPATH, 'line_station.html'), 'r').read()
25 errorpage = open(os.path.join(DATAPATH, 'errorpage.html'), 'r').read()
26 nodepartures = open(os.path.join(DATAPATH, 'nodepartures.html'), 'r').read()
27 lines = open(os.path.join(DATAPATH, 'lines.html'), 'r').read()
28 stations1 = open(os.path.join(DATAPATH, 'stations1.html'), 'r').read()
29 stations2 = open(os.path.join(DATAPATH, 'stations2.html'), 'r').read()
30
31 parsed_lines = parser.parse_lines(lines)
32
33 def test_lines():
34     assert_is_instance(parsed_lines, dict)
35     assert_true(parsed_lines)
36
37 def test_line_amount():
38     assert_equal(104, len(parsed_lines.keys()))
39
40 def test_line_link():
41     assert_equal('http://www.wienerlinien.at/itip/linienwahl/linie.php?lng=de&lng=de&linie=1', parsed_lines['1'])
42
43 def test_line_links():
44     assert_true(filter(lambda x: x.startswith('http://'), parsed_lines.values()))
45
46 def test_stations1():
47     st1 = parser.parse_stations(stations1)
48     assert_true(st1.has_key(u'Gersthof, Herbeckstraße'))
49     assert_true(st1.has_key(u'Schottentor U'))
50     assert_equal(14, len(st1[u'Gersthof, Herbeckstraße']))
51     assert_equal(12, len(st1[u'Schottentor U']))
52
53 def test_stations2():
54     st2 = parser.parse_stations(stations2)
55     assert_true(st2.has_key(u'Stefan-Fadinger-Platz'))
56     assert_true(st2.has_key(u'Prater Hauptallee'))
57     assert_equal(31, len(st2[u'Stefan-Fadinger-Platz']))
58     assert_equal(30, len(st2[u'Prater Hauptallee']))
59
60 def test_departures_by_station():
61     dep = parser.parse_departures_by_station(stationbased)
62     # find all 34 departures
63     assert_equal(34, len(dep))
64     l = list(set(map(lambda x: x['line'], dep)))
65     # there are 8 different lines
66     assert_equal(8, len(l))
67     assert_equal(u'Leopoldau', dep[0]['direction'])
68     assert_equal(u'Karlsplatz', dep[0]['station'])
69
70 def test_departures_by_station_lowfloor():
71     dep = parser.parse_departures_by_station(stationbased)
72     assert_true(dep[0]['lowfloor'])
73     assert_false(dep[14]['lowfloor'])
74
75 def test_departures_by_station_datetime():
76     dep = parser.parse_departures_by_station(stationbased)
77     assert_is_instance(dep[13]['time'], int)
78     assert_is_instance(dep[14]['departure'], datetime)
79     assert_equal(4, dep[3]['time'])
80     assert_equal(2, dep[4]['time'])
81     assert_equal(18, dep[13]['time'])
82     assert_equal('59A', dep[-4]['line'])
83     assert_equal('WLB', dep[-1]['line'])
84     assert_equal(datetime(2000, 1, 1, 13, 5), dep[14]['departure'])
85
86 def test_departures():
87     dep = parser.parse_departures(line_station)
88     assert_equal(0, dep[0]['time'])
89     assert_equal(10, dep[2]['time'])
90     assert_equal(6, len(dep))
91     assert_equal(u'Stefan-Fadinger-Platz', dep[0]['direction'])
92     assert_equal(u'Kärntner Ring, Oper', dep[0]['station'])
93
94 def test_departures_lowfloor():
95     dep = parser.parse_departures(line_station)
96     assert_false(dep[1]['lowfloor'])
97     assert_true(dep[2]['lowfloor'])
98
99 def test_error_page():
100     dep = parser.parse_departures(errorpage)
101     assert_equal(0, len(dep))
102
103 def test_no_departures():
104     dep = parser.parse_departures(nodepartures)
105     assert_equal(0, len(dep))