]> ToastFreeware Gitweb - philipp/winterrodeln/wrfeed.git/blobdiff - tests/test_wrfeed.py
PEP 8 improvements.
[philipp/winterrodeln/wrfeed.git] / tests / test_wrfeed.py
index b24be969d1e00fe6915ddeb1a2b9332c88da5e8c..d4ccc5d945f71e6b956c675a977e63fb78637a59 100644 (file)
@@ -1,3 +1,11 @@
+"""How to test wrfeed:
+
+To prepare a system for testing, create a database philipp_winterrodeln_wiki_wrfeed_test with mysql:
+echo 'create database philipp_winterrodeln_wiki_wrfeed_test;' | mysql
+mysql philipp_winterrodeln_wiki_wrfeed_test < testdb.sql
+
+WRFEED_SETTINGS=../wrfeed/test.cfg python3 -m unittest tests/test_wrfeed.py
+"""
 import unittest
 import xml.etree.ElementTree as et
 import sqlalchemy
@@ -23,40 +31,92 @@ class TestDbWrfeed(WrfeedTestBase):
         super().setUp()
         with wrfeed.app.app_context():
             # create database
-            uri = make_url(wrfeed.app.config['DATABASE_URI'])
+            uri = make_url(wrfeed.app.config['SQLALCHEMY_DATABASE_URI'])
             self.uri = uri
-            uri_nodb = URL(uri.drivername, host=uri.host, port=uri.port, username=uri.username, password=uri.password, query=uri.query)
+            uri_nodb = URL(uri.drivername, host=uri.host, port=uri.port, username=uri.username,
+                           password=uri.password, query=uri.query)
             engine_nodb = sqlalchemy.create_engine(uri_nodb)
             engine_nodb.execute("DROP DATABASE IF EXISTS {}".format(uri.database))
             engine_nodb.execute("CREATE DATABASE {} CHARACTER SET = '{}'".format(uri.database, 'UTF8'))
 
             # connect to database
             self.engine = sqlalchemy.create_engine(uri)
+            con = self.engine.connect()
 
             # fill database
             with open('tests/testdb.sql', 'r') as f:
                 sql = f.read()
-            self.engine.execute(sql)
+            con.execute(sql)
 
             # update dates
-            con = self.engine.connect()
             seconds_diff, = con.execute("SELECT TIMESTAMPDIFF(SECOND, '2017-03-31 07:00', now())").fetchone()
             seconds_diff = int(seconds_diff)
-            con.execute("UPDATE wrreport SET date_report = DATE(date_report + INTERVAL %s SECOND) WHERE time_report IS NULL", (seconds_diff,))
-            con.execute("UPDATE wrreport SET date_report = DATE(TIMESTAMP(date_report, time_report) + INTERVAL %s SECOND) WHERE time_report IS NOT NULL", (seconds_diff,))
-            con.execute("UPDATE wrreport SET time_report = TIME(TIMESTAMP(date_report, time_report) + INTERVAL %s SECOND) WHERE time_report IS NOT NULL", (seconds_diff,))
+            con.execute("UPDATE wrreport SET date_report = DATE(date_report + INTERVAL %s SECOND) "
+                        "WHERE time_report IS NULL", (seconds_diff,))
+            con.execute("UPDATE wrreport SET date_report = "
+                        "DATE(TIMESTAMP(date_report, time_report) + INTERVAL %s SECOND) "
+                        "WHERE time_report IS NOT NULL", (seconds_diff,))
+            con.execute("UPDATE wrreport SET time_report = "
+                        "TIME(TIMESTAMP(date_report, time_report) + INTERVAL %s SECOND) "
+                        "WHERE time_report IS NOT NULL", (seconds_diff,))
             con.execute("UPDATE wrreport SET date_entry = date_entry + INTERVAL %s SECOND", (seconds_diff,))
             con.execute("UPDATE wrreport SET date_invalid = date_invalid + INTERVAL %s SECOND", (seconds_diff,))
             con.execute("UPDATE wrreport SET delete_date = delete_date + INTERVAL %s SECOND", (seconds_diff,))
 
-
-    def tearDown(self):
-        with wrfeed.app.app_context():
-            self.engine.execute("DROP DATABASE {}".format(self.uri.database))
-
     def test_alle(self):
         result = self.app.get('/berichte/alle')
         self.assertTrue(result.data.startswith(b'<?xml'))
         root = et.fromstring(result.data)
         self.assertIn('Rodelbahnberichte', root.find('{http://www.w3.org/2005/Atom}title').text)
         self.assertEqual(len(root.findall('{http://www.w3.org/2005/Atom}entry')), 7)
+
+    def test_bahn(self):
+        bahn_count = {
+            'haunold': 1,
+            'pleisenhütte_axamer_lizum': 2,
+            'prantner_alm': 0,
+            'birgitzer_alm_(vom_adelshof)': 1,
+        }
+        for bahn, count in bahn_count.items():
+            with self.subTest(bahn=bahn):
+                result = self.app.get('/berichte/bahn/{}'.format(bahn))
+                self.assertTrue(result.data.startswith(b'<?xml'))
+                root = et.fromstring(result.data)
+                self.assertIn('Rodelbahnberichte', root.find('{http://www.w3.org/2005/Atom}title').text)
+                self.assertEqual(len(root.findall('{http://www.w3.org/2005/Atom}entry')), count,
+                                 '"{}" not found {} times'.format(bahn, count))
+
+    def test_bahnen(self):
+        bahnen_count = {
+            '761': 2,
+            '793': 0,
+            '100': 0,
+            '1417': 1,
+            '5': 1,
+            '228': 1,
+            '793+100': 0,
+            '761+1417+5+228': 5,
+        }
+        for bahnen, count in bahnen_count.items():
+            with self.subTest(bahnen=bahnen):
+                result = self.app.get('/berichte/bahnen/{}'.format(bahnen))
+                self.assertTrue(result.data.startswith(b'<?xml'))
+                root = et.fromstring(result.data)
+                self.assertIn('Rodelbahnberichte', root.find('{http://www.w3.org/2005/Atom}title').text)
+                self.assertEqual(len(root.findall('{http://www.w3.org/2005/Atom}entry')), count,
+                                 f'bahnen "{bahnen}" not found {count} times')
+
+    def test_region(self):
+        region_count = {
+            'tirol': 6,
+            'schweiz': 1,
+            'innsbruck': 3,
+        }
+        for region, count in region_count.items():
+            with self.subTest(region=region):
+                result = self.app.get('/berichte/region/{}'.format(region))
+                self.assertTrue(result.data.startswith(b'<?xml'))
+                root = et.fromstring(result.data)
+                self.assertIn('Rodelbahnberichte', root.find('{http://www.w3.org/2005/Atom}title').text)
+                self.assertEqual(len(root.findall('{http://www.w3.org/2005/Atom}entry')), count,
+                                 f'"{region}" not found {count} times')