def test_index_page(): file_path = find_dotenv('.env.test', usecwd=True) load_dotenv(file_path, override=True) database_config = DatabaseConfig() auth_config = AuthConfig() flask_config = FlaskConfig() mocked_client = mongomock.MongoClient() mock_db = mocked_client.get_database(database_config.db_name) client = AtlasClient(database_config, mocked_client) login_manager = LoginManager() login_manager.anonymous_user = TestUser #Create the new app. test_app = create_app(client, auth_config, login_manager) test_app.config.from_object(flask_config) test_app.config['LOGIN_DISABLED'] = True mock_item_response = ToDoItem.new_item_as_dict( "Hello form the integration tests") mock_db.get_collection("test_collection_name").insert_one( mock_item_response) response = test_app.test_client().get("/") assert 200 == response.status_code assert "Hello form the integration tests" in response.data.decode()
def app(): app = create_app() app.config['TESTING'] = True app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' app.config['MAX_PASTE_LENGTH'] = 20 app.debug = False yield app
def test_app(): # construct the new application try: file_path = find_dotenv('.env') load_dotenv(file_path, override=True) except: print("Could not find .env") database_config = DatabaseConfig() auth_config = AuthConfig() flask_config = FlaskConfig() database_config._todo_collection_name = os.environ.get('TEST_COLLECTION') mongo_client = MongoClient(database_config.db_url) client = AtlasClient(database_config, mongo_client) client._collection.delete_many({}) login_manager = LoginManager() login_manager.anonymous_user = TestUser application = create_app(client, auth_config, login_manager) application.config.from_object(flask_config) application.config['LOGIN_DISABLED'] = True # start the app in its own thread. thread = Thread(target=lambda: application.run(use_reloader=False)) thread.daemon = True thread.start() yield application # Tear Down thread.join(1)
def client(): # Use our test integration config instead of the 'real' version file_path = find_dotenv('.env.test') load_dotenv(file_path, override=True) #Create the new app. test_app = create_app() # Use the app to create a test_client that can be used in our tests. with test_app.test_client() as client: yield client
def app(request): from app.create_app import create_app app = create_app(__name__) context = app.app_context() context.push() @request.addfinalizer def pop(): context.pop() return app
def test_app(): # construct the new application file_path = find_dotenv('.env') load_dotenv(file_path, override=True) application = create_app() # start the app in its own thread. thread = Thread(target=lambda: application.run(use_reloader=False)) thread.daemon = True thread.start() yield application # Tear Down thread.join(1)
def make_celery(app=None): """ :param app: :return: """ app = app or create_app(app_name='worker') celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask return celery
def mock_app(): """ Setup our flask test app, this only gets executed once. :return: Flask app """ params = { 'DEBUG': False, 'TESTING': True, 'WTF_CSRF_ENABLED': False, 'SERVER_NAME': 'test' } _app = create_app.create_app() _app.config.update(params) # Establish an application context before running the tests. ctx = _app.app_context() ctx.push() yield _app ctx.pop()
from app import create_app # 导入Manager用来设置应用程序可通过指令操作 from flask_script import Manager, Server, Command app = create_app.create_app() # 构建指令,设置当前app受指令控制(即将指令绑定给指定app对象) manage = Manager(app) manage.add_command( 'runserver', Server(host='0.0.0.0', port=5061, threaded=True, use_debugger=True)) #以下为当指令操作runserver时,开启服务。 if __name__ == '__main__': manage.run()
def app(): app = create_app(TestingConfig) with app.app_context() as ctx: yield app
"""Entry point for Flask""" from flask_login import LoginManager from pymongo import MongoClient from app.create_app import create_app from app.flask_config import DatabaseConfig, AuthConfig, FlaskConfig from app.atlas_client import AtlasClient database_config = DatabaseConfig() auth_config = AuthConfig() flask_config = FlaskConfig() mongo_client = MongoClient(database_config.db_url) atlas_client = AtlasClient(database_config, mongo_client) login_manager = LoginManager() app = create_app(atlas_client, auth_config, login_manager) app.config.from_object(flask_config) if __name__ == '__main__': app.run(host='0.0.0.0')
import os from app import create_app if __name__ == '__main__': verify_token = os.getenv("VERIFY_TOKEN", None) access_token = os.getenv("ACCESS_TOKEN", None) url = os.getenv("URL", None) if not verify_token: raise Exception("verify_token not set") if not access_token: raise Exception("access_token not set") env = { "VERIFY_TOKEN": verify_token, "ACCESS_TOKEN": access_token, "URL": url } app = create_app.create_app(env=env) app.logger.info("Initializing") app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 8080)))
#!/usr/bin/env python3 # pylint: disable=missing-docstring import config as cfg from app.create_app import create_app from app.site import routes SITE_SERVICE = create_app(cfg, [(routes.SITE_BP, {})]) if __name__ == "__main__": SITE_SERVICE.run(host='127.0.0.1', port=8080, debug=True)
def app(): app = create_app() return app
# -*- coding: utf-8 -*- """ This is just example app to learn how to write best practice code in Python Flask Framework """ import os from app.create_app import create_app from app.blueprint import register_blueprint APP = create_app(os.getenv("APP_MODE")) # Register All Function to app register_blueprint(APP)
import os from app.create_app import create_app app = create_app(os.environ['APP_SETTINGS'])
from flask_admin import Admin from flask_admin.contrib.sqla import ModelView from flask_admin.form import Select2TagsField from flask_babelex import Babel from flask_login import LoginManager, current_user from flask_marshmallow import Marshmallow from flask_migrate import MigrateCommand from flask_script import Manager, Server from flask_script import Shell from werkzeug.utils import redirect from app.create_app import create_app from app.models import Coronavirus, User, MultipleSelect2Field from app.views import main_blueprint app, SESSION = create_app(os.getenv('FLASK_CONFIG') or 'default') manager = Manager(app) ma = Marshmallow(app) admin = Admin(app, name='开课统计', template_mode='bootstrap3') login = LoginManager(app) babel = Babel(app) app.config['BABEL_DEFAULT_LOCALE'] = 'zh_CN' app.register_blueprint(main_blueprint) @login.user_loader def load_user(u_id): return SESSION().query(User).filter(User.id == u_id).first()
from app.create_app import create_app app = create_app(__name__)
from flask.ext.script import Manager, Server from app import db from app.create_app import create_app from app.computer.models import ComputerCheckin manager = Manager( create_app( '/vagrant/terrapin_server/config/dev.cfg', '/vagrant/terrapin_server/config/logging.yaml' ) ) @manager.command def create_db(): print('Creating Database ... ') db.create_all() @manager.command def recreate_db(): print("Dropping Database ... ") db.drop_all() create_db() manager.add_command('runserver', Server(host="0.0.0.0", port=8100)) if __name__ == '__main__': manager.run()
#!/usr/bin/env python3 # pylint: disable=missing-docstring # pylint can't find flask_cors for some reason # pylint: disable=import-error import config as cfg from app.search import search from app.api.models import init_db from app.create_app import create_app from app.api import routes from app.api.test import routes as test_routes from flask_cors import CORS API_SERVICE = create_app(cfg, [(routes.API_BP, {}), (test_routes.TEST_BP, { "url_prefix": "/test" })]) CORS(API_SERVICE) init_db(API_SERVICE) search.init_search_index() if __name__ == "__main__": API_SERVICE.run(host='127.0.0.1', port=8080, debug=True)
def app(): app = create_app() app.config['TESTING'] = True app.debug = True app.config['WTF_CSRF_ENABLED'] = False yield app
def app(): app = create_app('testing') yield app
def create_app(self): # pass in test configuration return create_app()