| 1 | import logging
|
|---|
| 2 |
|
|---|
| 3 | import flask
|
|---|
| 4 | import connexion
|
|---|
| 5 | from flask_cors import CORS
|
|---|
| 6 | from flask_testing import TestCase
|
|---|
| 7 | from flask_mail import Mail
|
|---|
| 8 | from sqlalchemy.event import listen
|
|---|
| 9 | from celery import Celery
|
|---|
| 10 | import tempfile
|
|---|
| 11 | import os
|
|---|
| 12 | from PIL import Image
|
|---|
| 13 | import shutil
|
|---|
| 14 |
|
|---|
| 15 | from {{packageName}}.encoder import JSONEncoder
|
|---|
| 16 | from {{packageName}}_impl.database.Model import (
|
|---|
| 17 | db, db_setup, load_spatialite
|
|---|
| 18 | )
|
|---|
| 19 | from {{packageName}}_impl.tests import db_test_setup
|
|---|
| 20 |
|
|---|
| 21 | class BaseTestCase(TestCase):
|
|---|
| 22 | def create_app(self):
|
|---|
| 23 | logging.getLogger('connexion.operation').setLevel('ERROR')
|
|---|
| 24 | app = connexion.App(__name__, specification_dir='../openapi/')
|
|---|
| 25 | application = app.app
|
|---|
| 26 | CORS(application)
|
|---|
| 27 | mail = Mail()
|
|---|
| 28 | application.json_encoder = JSONEncoder
|
|---|
| 29 | app.add_api('openapi.yaml')
|
|---|
| 30 |
|
|---|
| 31 | application.config.from_object('{{packageName}}_impl.Config.TestingConfig')
|
|---|
| 32 | application.template_folder = os.path.join(os.path.dirname(__file__), '../../mapservice_impl/templates')
|
|---|
| 33 | celery = Celery(__name__, broker=application.config['CELERY_BROKER_URL'])
|
|---|
| 34 | celery.config_from_object(application.config)
|
|---|
| 35 | mail.init_app(application)
|
|---|
| 36 | db.init_app(application)
|
|---|
| 37 | application.app_context().push()
|
|---|
| 38 |
|
|---|
| 39 | self._create_test_tile(application)
|
|---|
| 40 |
|
|---|
| 41 | return application
|
|---|
| 42 |
|
|---|
| 43 | def setUp(self):
|
|---|
| 44 | listen(db.engine, 'connect', load_spatialite)
|
|---|
| 45 | db.drop_all()
|
|---|
| 46 | # create the tables whether the database already exists or not
|
|---|
| 47 | db_setup(True)
|
|---|
| 48 | self.db_content = db_test_setup.DatabaseContent()
|
|---|
| 49 | self.db_content.setup()
|
|---|
| 50 |
|
|---|
| 51 | def tearDown(self):
|
|---|
| 52 | db.session.remove()
|
|---|
| 53 | db.drop_all()
|
|---|
| 54 | shutil.rmtree(self.tiles_cache)
|
|---|
| 55 |
|
|---|
| 56 | def _create_test_tile(self, application):
|
|---|
| 57 | self.tiles_cache = tempfile.mkdtemp(prefix='gnuboat-tests')
|
|---|
| 58 | application.config['TILES_CACHE_PATH'] = self.tiles_cache
|
|---|
| 59 |
|
|---|
| 60 | tile_path = os.path.join(self.tiles_cache, '100_100/256/15/17298/')
|
|---|
| 61 | os.makedirs(tile_path)
|
|---|
| 62 | img = Image.new('RGBA', (100, 100), (255, 0, 0, 0))
|
|---|
| 63 | img.save(os.path.join(tile_path, "21435.png"), "PNG")
|
|---|
| 64 |
|
|---|
| 65 | def check_key(self, key, value, data):
|
|---|
| 66 | assert key in data, "{} not in data {}".format(key, data)
|
|---|
| 67 | assert data[key] == value, "{} = {}".format(value, data[key])
|
|---|