b61aecbbc1a5014ef65660a74fb040f70f285665
[pywienerlinien] / gotovienna / tests / realtime.py
1 # -*- coding: utf-8 -*-
2
3 from nose.tools import assert_equal, assert_true, assert_false
4 import sys
5 import os
6 from datetime import time
7 sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
8 DATAPATH = 'data'
9
10 from gotovienna.realtime import *
11
12 parser = ITipParser()
13
14 stationbased = open(os.path.join(DATAPATH, 'stationbased.html'), 'r').read()
15 line_station = open(os.path.join(DATAPATH, 'line_station.html'), 'r').read()
16 errorpage = open(os.path.join(DATAPATH, 'errorpage.html'), 'r').read()
17 nodepartures = open(os.path.join(DATAPATH, 'nodepartures.html'), 'r').read()
18 lines = open(os.path.join(DATAPATH, 'lines.html'), 'r').read()
19 stations1 = open(os.path.join(DATAPATH, 'stations1.html'), 'r').read()
20 stations2 = open(os.path.join(DATAPATH, 'stations2.html'), 'r').read()
21
22 parsed_lines = parser.parse_lines(lines)
23
24 def test_lines():
25     assert_equal(dict, type(parsed_lines))
26     assert_true(parsed_lines)
27
28 def test_line_amount():
29     assert_equal(104, len(parsed_lines.keys()))
30
31 def test_line_link():
32     assert_equal('http://www.wienerlinien.at/itip/linienwahl/linie.php?lng=de&lng=de&linie=1', parsed_lines['1'])
33
34 def test_line_links():
35     assert_true(filter(lambda x: x.startswith('http://'), parsed_lines.values()))
36
37 def test_stations1():
38     st1 = parser.parse_stations(stations1)
39     assert_true(st1.has_key(u'Gersthof, Herbeckstraße'))
40     assert_true(st1.has_key(u'Schottentor U'))
41     assert_equal(14, len(st1[u'Gersthof, Herbeckstraße']))
42     assert_equal(12, len(st1[u'Schottentor U']))
43
44 def test_stations2():
45     st2 = parser.parse_stations(stations2)
46     assert_true(st2.has_key(u'Stefan-Fadinger-Platz'))
47     assert_true(st2.has_key(u'Prater Hauptallee'))
48     assert_equal(31, len(st2[u'Stefan-Fadinger-Platz']))
49     assert_equal(30, len(st2[u'Prater Hauptallee']))
50
51 def test_departures_by_station():
52     dep = parser.parse_departures_by_station(stationbased)
53     # find all 34 departures
54     assert_equal(34, len(dep))
55     l = list(set(map(lambda x: x['line'], dep)))
56     # there are 8 different lines
57     assert_equal(8, len(l))
58
59 def test_departures_by_station_lowfloor():
60     dep = parser.parse_departures_by_station(stationbased)
61     assert_true(dep[0]['lowfloor'])
62     assert_false(dep[14]['lowfloor'])
63
64 def test_departures_by_station_datetime():
65     dep = parser.parse_departures_by_station(stationbased)
66     assert_equal(int, type(dep[13]['time']))
67     assert_equal(time, type(dep[14]['time']))
68
69 def test_departures():
70     dep = parser.parse_departures(line_station)
71
72 def test_error_page():
73     dep = parser.parse_departures(errorpage)
74     assert_equal(0, len(dep))
75
76 def test_no_departures():
77     dep = parser.parse_departures(nodepartures)
78     assert_equal(0, len(dep))