class TestCase(unittest.TestCase): def setUp(self): self.app = Create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() def tearDown(self): db.session.remove() db.drop_all() self.app_context.pop() def test_current_app(self): self.assertFalse(current_app is None) def test_testing_env(self): self.assertTrue(current_app.config['TESTING'])
# from app import app from app import Create_app from flask_migrate import Migrate, MigrateCommand from flask_script import Manager app, db = Create_app('SonConfig') manager = Manager(app) Migrate(app, db) manager.add_command('db', MigrateCommand) if __name__ == '__main__': print(app.url_map) manager.run()
#!/usr/bin/env python """ run.py ---------- This module calls necessary parameters and configuration variables to execute the flask application. """ __author__ = "Arian Gallardo" from app import Create_app app = Create_app('config') if __name__ == '__main__': app.run(host=app.config['HOST'], port=app.config['PORT'], debug=app.config['DEBUG'])
import os from app import Create_app, db from app. models import Library, User from flask_migrate import Migrate app = Create_app(os.getenv('FLASK_CONFIG') or 'default') migrate = Migrate(app, db) @app.shell_context_processor def make_shell_context(): return dict(db=db, User=User, Library=Library) @app.cli.command() def test(): """run the unit test""" import unittest tests = unittest.TestLoader().discover('tests') unittest.TextTestRunner(verbosity=2).run(tests)
# 命令行文件 from app import models,Create_app from flask_script import Manager #增加命令行功能 from flask_migrate import Migrate #同步数据库可用 from flask_migrate import MigrateCommand #同步数据库可用命令行 app = Create_app() manager = Manager(app) migrate = Migrate(app,models)#连接数据库与app的,使用的是命令行,同步用 manager.add_command('db',MigrateCommand) if __name__ == '__main__': manager.run()
def setUp(self): self.app = Create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all()
from app import Create_app, db from flask_migrate import Migrate from flask_script import Manager from app.models import Roles from flask import current_app app = Create_app('default') app_context = app.app_context() app_context.push() Roles.insert_roles() db.create_all() migrate = Migrate(app, db) manager = Manager(app) # @manager.command # def test(): # '''runs the unit tests''' # import unittest # test = unittest.TestLoader().discover('tests') # unittest.TextTestRunner(verbosity=2).run(tests) if __name__ == "__main__": app.run()
# app对象 from app import Create_app # 配置 from config import Developconfig # 拓展命令 from flask_script import Manager # db对象 from models import db # 迁移 from flask_migrate import Migrate, MigrateCommand app = Create_app(Developconfig) if __name__ == '__main__': manager = Manager(app) migrate = Migrate(app, db) # 添加数据库迁移命令 manager.add_command('db', MigrateCommand) # 添加创建管理员用户的拓展命令 from views_admin import CreateAdmin, CreateUser manager.add_command('createadmin', CreateAdmin) # 添加创建多条用户的命令 manager.add_command('createuser', CreateUser) manager.run()