Skip to content

Commit

Permalink
tests only set up database once now, fixes #96
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsnolde committed Apr 1, 2020
1 parent d1ef2e7 commit 90217c1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
51 changes: 36 additions & 15 deletions tests/base.py
Original file line number Diff line number Diff line change
@@ -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()
7 changes: 3 additions & 4 deletions tests/test_category_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down

0 comments on commit 90217c1

Please sign in to comment.