From 90217c14c917b3ffe3098345bf4455131aeff3be Mon Sep 17 00:00:00 2001 From: nilsnolde Date: Wed, 1 Apr 2020 14:30:37 +0200 Subject: [PATCH] tests only set up database once now, fixes #96 --- tests/base.py | 51 ++++++++++++++++++++++++++----------- tests/test_category_list.py | 7 +++-- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/tests/base.py b/tests/base.py index 7cfff5e..f287673 100755 --- a/tests/base.py +++ b/tests/base.py @@ -1,28 +1,49 @@ # openpoiservice/server/tests/base.py - -from flask_testing import TestCase -from pathlib import Path -from openpoiservice import db, create_app, config_map -from openpoiservice.utils import parser +import unittest import os +from pathlib import Path -app = create_app('testing') +from openpoiservice import db, create_app +from openpoiservice.utils import parser +from openpoiservice.logger import logger +this_path = Path(os.path.dirname(__file__)) -class BaseTestCase(TestCase): +class BaseTestCase(unittest.TestCase): - def create_app(self): - app.config.from_object(config_map['testing']) + @classmethod + def setUpClass(cls): + app = create_app('testing') + app.app_context().push() + cls.app = app - return app + cls.db = db + cls.db.app = cls.app - def setUp(self): - db.create_all() + logger.info(f"""The following database settings are active: +\tHost: {cls.db.engine.url.host}:{cls.db.engine.url.port} +\tDatabase: {cls.db.engine.url.database} +\tUser: {cls.db.engine.url.username}""") - test_file = Path(os.path.join('tests', 'data', 'bremen-tests.osm.pbf')) + cls.db.create_all() + logger.info("Created tables:\n\t{}".format("\n\t".join(cls.db.metadata.tables.keys()))) + # Import test data + test_file = Path(os.path.join(this_path, 'data', 'bremen-tests.osm.pbf')) parser.parse_import(test_file) + super().setUpClass() - def tearDown(self): - db.session.remove() + @classmethod + def tearDownClass(cls): + super().tearDownClass() db.drop_all() + + def setUp(self): + super().setUp() + self.client = self.app.test_client() + self.app_context = self.app.app_context() + self.app_context.push() + + def tearDown(self): + super().tearDown() + self.app_context.pop() diff --git a/tests/test_category_list.py b/tests/test_category_list.py index d7ca006..adc5354 100755 --- a/tests/test_category_list.py +++ b/tests/test_category_list.py @@ -10,11 +10,10 @@ class TestCategoryListBlueprint(BaseTestCase): def test_category_list(self): - response = self.client.post('/pois', data=json.dumps(dict(request='list')), - content_type='application/json') + response = self.client.get('/list') self.assertEqual(response.status_code, 200) - self.assertIn(b'accomodation', response.data) - self.assertIn(b'animals', response.data) + self.assertIn('accomodation', response.json) + self.assertIn('animals', response.json) if __name__ == '__main__':