def init_celery(app=None): """ Setup celery with application factory """ if app is None: from project.server import create_app app = create_app() celery.conf.update(app.config) celery.conf.broker_url = app.config["CELERY_BROKER_URL"] celery.conf.result_backend = app.config["CELERY_RESULT_BACKEND"] celery.conf.broker_connection_max_retries = 3 celery.conf.broker_transport_options = { "max_retries": 3, "interval_start": 0, "interval_step": 0.2, "interval_max": 0.5 } celery.conf.task_publish_retry_policy = { "max_retries": 3, "interval_start": 0, "interval_step": 0.2, "interval_max": 0.5 } class ContextTask(celery.Task): """Make celery tasks work with Flask app context""" def __call__(self, *args, **kwargs): with app.app_context(): return self.run(*args, **kwargs) celery.Task = ContextTask return celery
def app(): """An application for the tests.""" _app = create_app("tests.settings") ctx = _app.test_request_context() ctx.push() yield _app ctx.pop()
def app(): """Create application for the tests.""" _app = create_app("project.server.config.TestingConfig") _app.logger.setLevel(logging.CRITICAL) ctx = _app.test_request_context() ctx.push() yield _app ctx.pop()
# manage.py import unittest from time import sleep import coverage from flask.cli import FlaskGroup from project.server import create_app, db from project.server.models import User from project.server.pickle_builder import DataScienceModelsPersistor import sys app = create_app() cli = FlaskGroup(create_app=create_app) # code coverage COV = coverage.coverage( branch=True, include="project/*", omit=[ "project/tests/*", "project/server/config__.py", "project/server/*/__init__.py", ], ) #COV.start() @cli.command() def create_db():
def create_app(self): app_settings = os.getenv('APP_SETTINGS', 'project.server.config.ProductionConfig') app = create_app(app_settings) return app
def create_app(self): app_settings = os.getenv('APP_SETTINGS', 'project.server.config.DevelopmentConfig') app = create_app(app_settings) return app
import os import coverage import unittest from flask_migrate import Migrate, MigrateCommand from project.server import create_app, db from dotenv import load_dotenv dotenv_path = os.path.join(os.path.dirname(__file__), '.env') if os.path.exists(dotenv_path): load_dotenv(dotenv_path) app_settings = os.getenv('APP_SETTINGS', 'project.server.config.DevelopmentConfig') app = create_app(app_settings) migrate = Migrate(app, db) COV = coverage.coverage(branch=True, include='project/*', omit=[ 'project/tests/*', 'project/server/config.py', 'project/server/*/__init__.py' ]) COV.start() @app.cli.command() def test(): """Run tests under without coverage.""" tests = unittest.TestLoader().discover('project/tests', pattern='test*.py')
# project/server/main/works.py import time from rq import get_current_job from project.server import create_app app = create_app() app.app_context().push() def _set_task_progress(task): """ This method will update the job progress using the task object :param task : Task :return: """ job = get_current_job() if job: job.meta['progress'] = task.export() job.save_meta() def long_work(tsk): """ This method will start uploading the images provided by the tsk object :param tsk: :return: """ tsk.executeAll(_set_task_progress)
def test_app(): app = create_app() app.config.from_object("project.server.config.TestingConfig") with app.app_context(): yield app # testing happens here