3 WRFEED_SETTINGS=../wrfeed/test.cfg python3 -m unittest tests/test_wrfeed.py
6 import xml.etree.ElementTree as et
8 from sqlalchemy.engine.url import URL, make_url
12 class WrfeedTestBase(unittest.TestCase):
14 wrfeed.app.config['TESTING'] = True
15 self.app = wrfeed.app.test_client()
18 class TestNoDbWrfeed(WrfeedTestBase):
20 result = self.app.get('/')
21 self.assertTrue(result.data.startswith(b'<?xml version='))
22 self.assertIn(b'page about feeds', result.data)
25 class TestDbWrfeed(WrfeedTestBase):
28 with wrfeed.app.app_context():
30 uri = make_url(wrfeed.app.config['DATABASE_URI'])
32 uri_nodb = URL(uri.drivername, host=uri.host, port=uri.port, username=uri.username, password=uri.password, query=uri.query)
33 engine_nodb = sqlalchemy.create_engine(uri_nodb)
34 engine_nodb.execute("DROP DATABASE IF EXISTS {}".format(uri.database))
35 engine_nodb.execute("CREATE DATABASE {} CHARACTER SET = '{}'".format(uri.database, 'UTF8'))
38 self.engine = sqlalchemy.create_engine(uri)
39 con = self.engine.connect()
42 with open('tests/testdb.sql', 'r') as f:
47 seconds_diff, = con.execute("SELECT TIMESTAMPDIFF(SECOND, '2017-03-31 07:00', now())").fetchone()
48 seconds_diff = int(seconds_diff)
49 con.execute("UPDATE wrreport SET date_report = DATE(date_report + INTERVAL %s SECOND) WHERE time_report IS NULL", (seconds_diff,))
50 con.execute("UPDATE wrreport SET date_report = DATE(TIMESTAMP(date_report, time_report) + INTERVAL %s SECOND) WHERE time_report IS NOT NULL", (seconds_diff,))
51 con.execute("UPDATE wrreport SET time_report = TIME(TIMESTAMP(date_report, time_report) + INTERVAL %s SECOND) WHERE time_report IS NOT NULL", (seconds_diff,))
52 con.execute("UPDATE wrreport SET date_entry = date_entry + INTERVAL %s SECOND", (seconds_diff,))
53 con.execute("UPDATE wrreport SET date_invalid = date_invalid + INTERVAL %s SECOND", (seconds_diff,))
54 con.execute("UPDATE wrreport SET delete_date = delete_date + INTERVAL %s SECOND", (seconds_diff,))
58 with wrfeed.app.app_context():
59 self.engine.execute("DROP DATABASE {}".format(self.uri.database))
62 result = self.app.get('/berichte/alle')
63 self.assertTrue(result.data.startswith(b'<?xml'))
64 root = et.fromstring(result.data)
65 self.assertIn('Rodelbahnberichte', root.find('{http://www.w3.org/2005/Atom}title').text)
66 self.assertEqual(len(root.findall('{http://www.w3.org/2005/Atom}entry')), 7)
71 'pleisenhütte_axamer_lizum': 2,
73 'birgitzer_alm_(vom_adelshof)': 1,
75 for bahn, count in bahn_count.items():
76 with self.subTest(bahn=bahn):
77 result = self.app.get('/berichte/bahn/{}'.format(bahn))
78 self.assertTrue(result.data.startswith(b'<?xml'))
79 root = et.fromstring(result.data)
80 self.assertIn('Rodelbahnberichte', root.find('{http://www.w3.org/2005/Atom}title').text)
81 self.assertEqual(len(root.findall('{http://www.w3.org/2005/Atom}entry')), count, '"{}" not found {} times'.format(bahn, count))
83 def test_bahnen(self):
94 for bahnen, count in bahnen_count.items():
95 with self.subTest(bahnen=bahnen):
96 result = self.app.get('/berichte/bahnen/{}'.format(bahnen))
97 self.assertTrue(result.data.startswith(b'<?xml'))
98 root = et.fromstring(result.data)
99 self.assertIn('Rodelbahnberichte', root.find('{http://www.w3.org/2005/Atom}title').text)
100 self.assertEqual(len(root.findall('{http://www.w3.org/2005/Atom}entry')), count, 'bahnen "{}" not found {} times'.format(bahnen, count))
102 def test_region(self):
108 for region, count in region_count.items():
109 with self.subTest(region=region):
110 result = self.app.get('/berichte/region/{}'.format(region))
111 self.assertTrue(result.data.startswith(b'<?xml'))
112 root = et.fromstring(result.data)
113 self.assertIn('Rodelbahnberichte', root.find('{http://www.w3.org/2005/Atom}title').text)
114 self.assertEqual(len(root.findall('{http://www.w3.org/2005/Atom}entry')), count, '"{}" not found {} times'.format(region, count))