def test_create_app(): try: create_app() except SystemExit: # Clean git repository doesn't have `local_config.py`, so it is fine # if we get SystemExit error. pass
def enter(context, install_dependencies=True, upgrade_db=True): """ Enter into IPython notebook shell with an initialized app. """ if install_dependencies: context.invoke_execute(context, 'app.deps.install') if upgrade_db: context.invoke_execute(context, 'app.db.upgrade') context.invoke_execute(context, 'app.db.init_dev_data', upgrade_db=False, skip_on_failure=True) import pprint from werkzeug import script import flask import catalog flask_app = catalog.create_app() def shell_context(): context = dict(pprint=pprint.pprint) context.update(vars(flask)) context.update(vars(catalog)) return context with flask_app.app_context(): script.make_shell(shell_context, use_ipython=True)()
def flask_app(): app = create_app(flask_config_name='testing') from catalog.extensions import db with app.app_context(): db.create_all() yield app db.drop_all()
def app(): os.environ['FLASK_ENV'] = 'test' test_app = create_app() # Init test db with test_app.app_context(): init_db() seed_db() yield test_app
def app(): app = create_app() with app.app_context(): db.engine.execute(_data_sql) yield app with app.app_context(): db.drop_all() db.session.remove()
def wrapper(*args, **kwargs): """ A wrapped which tries to get ``app`` from ``kwargs`` or creates a new ``app`` otherwise, and actives the application context, so the decorated function is run inside the application context. """ app = kwargs.pop('app', None) if app is None: from catalog import create_app app = create_app() with app.app_context(): return func(*args, **kwargs)
def export(context, output_format='json', quiet=False): """ Export swagger.json content """ # set logging level to ERROR to avoid [INFO] messages in result logging.getLogger().setLevel(logging.ERROR) from catalog import create_app app = create_app() swagger_content = app.test_client().get('/api/v1/swagger.%s' % output_format).data if not quiet: print(swagger_content.decode('utf-8')) return swagger_content
def run( context, host='0.0.0.0', port=4444, flask_config=None, install_dependencies=True, upgrade_db=True ): if flask_config is not None: os.environ['CATALOG_CONFIG'] = flask_config if install_dependencies: context.invoke_execute(context, 'app.deps.install') from catalog import create_app app = create_app() if upgrade_db: # After the installed dependencies the app.db.* tasks might need to be # reloaded to import all necessary dependencies. from . import db as db_tasks reload(db_tasks) context.invoke_execute(context, 'app.db.upgrade', app=app) if app.debug: context.invoke_execute( context, 'app.db.init_dev_data', app=app, upgrade_db=False, skip_on_failure=True ) use_reloader = app.debug if platform.system() == 'Windows': warnings.warn( "Auto-reloader feature doesn't work on Windows. " "Follow the issue for more details: " "https://github.com/frol/flask-restplus-server-example/issues/16" ) use_reloader = False app.run(host=host, port=port, debug=True, use_reloader=use_reloader)
def test_create_app_passing_flask_config_name(flask_config_name): create_app(flask_config_name=flask_config_name)
1) python manage.py runserver to launch application 2) python manage.py db migrate to detect changes in schema 3) python manage.py db upgrade to apply detected changes in schema 4) python manage.py test to run unit tests 5) python manage.py shell to launch python shell with key modules imported """ from catalog import create_app from flask.ext.script import Server, Manager, Shell from flask.ext.migrate import Migrate, MigrateCommand from catalog.model import User, Role, Item, Category from catalog import model from catalog import db import os # Configuration can be provided dynamically an environment variable app = create_app(os.getenv('FLASK_CONFIG') or None) manager = Manager(app) server = Server(host='0.0.0.0', port=5000) manager.add_command("runserver", server) # Initializes Flask-Migrate extension to automatically handle # modifications in schema migrate = Migrate(app, db) manager.add_command('db', MigrateCommand) # Default imports available after launching the shell def _make_context(): return dict(app=app, model=model, db=db, User=User, Role=Role, Item=Item, Category=Category)
import os from catalog import create_app, db from flask.ext.script import Manager, Server from flask.ext.migrate import Migrate, MigrateCommand app = create_app(os.getenv('CATALOG_ENV') or 'dev') manager = Manager(app) migrate = Migrate(app, db) manager.add_command("runserver", Server(port=5000, host='0.0.0.0')) manager.add_command('db', MigrateCommand) if __name__ == '__main__': manager.run()
import os from catalog import create_app from config import config_obj # check for environment variable config_env = os.getenv('FLASK_CONFIG', 'default') config = config_obj[config_env] # create flask app with the factory app = create_app(config) if app.debug: os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' # start the server if __name__ == '__main__': app.run()
def setUp(self): self.app = create_app('application.testing.cfg') self.app_context = self.app.app_context() self.app_context.push() db.create_all()
def test_create_app_passing_FLASK_CONFIG_env(monkeypatch, flask_config_name): monkeypatch.setenv('CATALOG_CONFIG', flask_config_name) create_app()
def setUp(self): self.app = create_app('application.testing.cfg') self.app_context = self.app.app_context() self.app_context.push() db.create_all() Role.insert_roles()
from catalog import create_app if __name__ == "__main__": app = create_app() app.run(debug=True)
def test_create_app_with_conflicting_config(monkeypatch): monkeypatch.setenv('CATALOG_CONFIG', 'production') with pytest.raises(AssertionError): create_app('development')
def test_create_app_with_non_existing_config(): with pytest.raises(KeyError): create_app('non-existing-config')