]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/commitdiff
Do not delete and recreate the database at each tests. MySql doesn't seem to like it.
authorphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Fri, 20 Oct 2017 22:09:45 +0000 (22:09 +0000)
committerphilipp <philipp@7aebc617-e5e2-0310-91dc-80fb5f6d2477>
Fri, 20 Oct 2017 22:09:45 +0000 (22:09 +0000)
git-svn-id: http://www.winterrodeln.org/svn/wradmin/trunk@2711 7aebc617-e5e2-0310-91dc-80fb5f6d2477

tests/test_wradmin.py

index 3ae1861591635e44a326471950517f510c1193c4..ce2135c179f1eb9aca5c15ca257834037480cea1 100644 (file)
@@ -3,9 +3,6 @@
 WRADMIN_SETTINGS=../wradmin/test.cfg python3 -m unittest tests.test_wradmin
 """
 import unittest
-import xml.etree.ElementTree as et
-import sqlalchemy
-from sqlalchemy.engine.url import URL, make_url
 import bs4  # beautiful soup
 import wradmin
 
@@ -29,32 +26,21 @@ class TestDbWradmin(WradminTestBase):
     def setUp(self):
         super().setUp()
         with wradmin.app.app_context():
-            # create database
-            uri = make_url(wradmin.app.config['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)
-            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()
-            con.execute(sql)
-
-            # update dates
-            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_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,))
+            with wradmin.get_db() as con:
+                con.execute(sql)
+
+                # update dates
+                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_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 test_reports(self):
         result = self.app.get('/bericht/list')
@@ -62,7 +48,3 @@ class TestDbWradmin(WradminTestBase):
         self.assertTrue(result.data.startswith(b'<!DOCTYPE html'))
         soup = bs4.BeautifulSoup(result.data, 'html.parser')
         self.assertEqual(soup.title.text, 'Rodelbahnberichte')
-
-    def tearDown(self):
-        with wradmin.app.app_context():
-            self.engine.execute("DROP DATABASE {}".format(self.uri.database))