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