From baa9aaa29a24e74c1274d8f1df54f45096e933f3 Mon Sep 17 00:00:00 2001 From: Florian Schweikert Date: Fri, 16 Dec 2011 16:16:46 +0100 Subject: [PATCH] rewrote routing unittests tests for overview parser --- gotovienna/tests/routing.py | 127 ++++++++++++++++++++----------------------- 1 file changed, 58 insertions(+), 69 deletions(-) diff --git a/gotovienna/tests/routing.py b/gotovienna/tests/routing.py index 3f27286..635d814 100644 --- a/gotovienna/tests/routing.py +++ b/gotovienna/tests/routing.py @@ -1,74 +1,63 @@ # -*- coding: utf-8 -*- -import unittest +from nose.tools import assert_equal, assert_true, assert_false from datetime import datetime from gotovienna.routing import * - -dtime = datetime.now() -dtime = dtime.replace(hour=15, minute=0) - -origin = u'Börse'.encode('UTF-8') -destination = 'Handelskai' -s = search((origin, 'stop'), (destination, 'stop'), dtime).read() -parser = sParser(s) -p = rParser(s) - - -originc = 'Simmeri' -destinationc = 'Karlspla' -sc = search((originc, 'stop'), (destinationc, 'stop'), dtime).read() -parserc = sParser(sc) - - -origina = 'Zwicklgasse 1' -destinationa = 'Himmelstrasse 1' -ot = dt = 'address' - - -originac = 'Foobar Strasse 123' -destinationac = 'Bazgasse 321' - - -class SearchTest(unittest.TestCase): - - def test_state(self): - state = parser.check_page() - statec = parserc.check_page() - - self.assertEqual(PageType.RESULT, state) - self.assertEqual(PageType.CORRECTION, statec) - - def test_correction(self): - cor = parserc.get_correction() - s = search((cor[0][0], 'stop'), (cor[1][0], 'stop'), dtime).read() - parser = sParser(s) - self.assertEqual(PageType.RESULT, parser.check_page()) - - def test_overview_shouldFindMultipleItems(self): - # TODO Replace with assertGreater in new python version - self.assertTrue(len(p.overview) > 1) - - def test_detail_shouldFindMultipleItems(self): - # TODO Replace with assertGreater in new python version - self.assertTrue(len(p.details) > 1) - - def test_detail_shouldFindMultipleStations(self): - # TODO Replace with assertGreater in new python version - self.assertTrue(len(p.details[0]) > 1) - - def test_parser_overviewAndDetailsShouldHaveSameLength(self): - self.assertEqual(len(p.details), len(p.overview)) - - def test_parser_shouldFindMoreThanOneChange(self): - self.assertTrue(p.overview[0]['change'] > 0) - - def test_parser_shouldFindPriceGreaterZero(self): - self.assertTrue(p.overview[0]['price'] > 0.0) - - def test_parser_shouldFindDate(self): - self.assertTrue(p.overview[0]['date'] == dtime.date()) - - -if __name__ == '__main__': - unittest.main() +DATAPATH = 'data' + +underground_routing = open(os.path.join(DATAPATH, 'underground_routing.html'), 'r').read() +underground_foot_routing = open(os.path.join(DATAPATH, 'underground_foot_routing.html'), 'r').read() +foot_routing = open(os.path.join(DATAPATH, 'foot_routing.html'), 'r').read() + +def test_extract_city(): + assert_equal('Wien', extract_city('Börse, Wien')) + assert_equal('Wien', extract_city('Börse')) + +def test_extract_station(): + assert_equal(u'Börse', extract_station(u'Börse, Wien')) + assert_equal(u'Börse', extract_station(u'Börse')) + +def test_split_station(): + assert_equal((u'Börse', 'Wien'), split_station(u'Börse, Wien')) + assert_equal((u'Börse', 'Wien'), split_station(u'Börse')) + +def test_guess_location_type(): + assert_equal('address', guess_location_type(u'Rathausstraße 6')) + assert_equal('stop', guess_location_type(u'Börse')) + +def test_overview1(): + """ Normal distance + """ + router = rParser(underground_routing) + assert_equal(4, len(router.overview)) + assert_equal(1, router.overview[0]['change']) + assert_equal(1.8, router.overview[0]['price']) + assert_equal([datetime(2011, 12, 16, 15, 44), + datetime(2011, 12, 16, 16, 0)], + router.overview[0]['timespan']) + +def test_overview2(): + """ Mixed routing short distance (EUR 0.9) and normal distance + """ + router = rParser(underground_foot_routing) + assert_equal(8, len(router.overview)) + assert_equal(1, router.overview[0]['change']) + assert_equal(0, router.overview[1]['change']) + assert_equal(1.8, router.overview[0]['price']) + assert_equal(0.9, router.overview[1]['price']) + assert_equal([datetime(2011, 12, 16, 15, 44), + datetime(2011, 12, 16, 15, 54)], + router.overview[0]['timespan']) + assert_equal([datetime(2011, 12, 16, 15, 44), + datetime(2011, 12, 16, 15, 59)], + router.overview[1]['timespan']) + +def test_overview3(): + """ Short distance, routing by foot + """ + router = rParser(foot_routing) + assert_equal(1, len(router.overview)) + assert_equal(0, router.overview[0]['change']) + assert_equal(.0, router.overview[0]['price']) + assert_equal([], router.overview[0]['timespan']) -- 1.7.9.5