def recreate_test_db(): """Recreates testing database.""" app.config.from_object('project.config.TestingConfig') db.reflect() db.drop_all() db.create_all() db.session.commit()
def create_database(): from project import app from project.extensions import db import project.models with app.app_context(): db.create_all() db.session.commit()
def create_app(self): self.app = app_factory() with self.app.app_context(): db.create_all() self.app.testing = True return self.app
def app(): """Create and configure a new app instance for each test.""" # create the app with common test config app = create_app() # create the database and load test data with app.app_context(): db.drop_all() db.create_all() db.session.commit() yield app
def do_initdb(application=None): if not application: global app else: app = application try: with app.app_context(): db.create_all() click.echo("db is initialized") except Exception as e: click.echo(str(e))
def users(): """ generate users and data """ user_emails = [] data =[] for i in range(300): user_emails.append(fake.email()) while True: email = user_emails.pop() user_set = { 'username': fake.name(), 'email': email, 'password': User.encryptpassword('password'), 'ct': fake.iso8601(tzinfo=None, end_datetime=None), 'last_login_ip': fake.ipv4_private(), 'current_login_ip': fake.ipv4_private() '' } data.append(user_set) if len(user_emails) <=0: break fisrt_admin = { 'username': fake.name(), 'email': app.config['SEED_ADMIN_EMAIL'], 'password': User.encryptpassword(app.config['SEED_ADMIN_PASSWORD']), 'ct': fake.iso8601(tzinfo=None, end_datetime=None), 'last_login_ip': fake.ipv4_private(), 'current_login_ip': fake.ipv4_private() } data.append(fisrt_admin) with app.app_context(): db.drop_all() db.create_all() # User.query.delete() #批量删除 # db.session.commit() print('删除了') db.engine.execute(User.__table__.insert(), data) all = User.query.all() add_role_account() admin = Role.query.filter_by(name='Admin').first() user = Role.query.filter_by(name='Normal_Users').first() for u in all: i = random.random() if i <= 0.05: admin.users.append(u) else: user.users.append(u) db.session.commit() print('insert success')
def setUp(self): with app.app_context(): app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['DEBUG'] = False app.config['SECRET_KEY'] = 'testing' #app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(app.config['BASEDIR'], TEST_DB) app.config[ 'SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join( BASEDIR, TEST_DB) self.app = app.test_client() db.drop_all() db.create_all() self.assertEquals(app.debug, False)
def client(): """flask testing client instance this function will try to create a db, return a testing client fixture. this function is called before every test and the lines after `yield` are being ran after the test. so we use this fixture as a setup and cleanup functionality for our tests. :return: """ uri = "/tmp/testing-db.db" app = make_app(conf="testing") with get_app_context('testing'): db.create_all() yield app.test_client() os.remove(uri)
def init(with_testdb): """ 初始化,如果传入参数则创建测试数据库 :param with_testdb: Create a test database :return: None """ db.drop_all() db.create_all() if with_testdb: db_uri = '{0}_test'.format(app.config['SQLALCHEMY_DATABASE_URI']) if not database_exists(db_uri): create_database(db_uri) print('创建了测试数据库') return None
def create_app(): app = Flask(__name__) used_config = environ.get('APP_SETTINGS', 'config.Config') app.config.from_object(used_config) db.init_app(app) for bp in all_blueprints: import_module(bp.import_name) app.register_blueprint(bp) with app.app_context(): for module in app.config.get('DB_MODELS_IMPORT', list()): import_module(module) with app.app_context(): db.create_all() return app
def setUp(self): db.create_all() db.session.commit()
def syncdb(): """Init/reset database.""" db.drop_all() db.create_all()
def recreate_db(): db.drop_all() db.create_all() db.session.commit() print('Database dropped and recreated.')
def create(): """Creates database tables from sqlalchemy models""" db.create_all()
def _db(request, app): db.create_all() return db
def configure_extentions(app): db.init_app(app) moment.init_app(app) with app.test_request_context(): db.create_all()
def create_app(self): self.app = app_factory() with self.app.app_context(): db.create_all() load_fixtures(self.app.config['FIXTURES_DIR']) return self.app
def create_app(self): self.app = app_factory() with self.app.app_context(): db.create_all() load_fixtures(self.app.config["FIXTURES_DIR"]) return self.app
"""Create an admin user in the database.""" from project.extensions import db from project.app import create_app app = create_app() # Keep database tables but delete all information with app.app_context(): db.drop_all() db.create_all() db.session.commit()
def init_db(): db.drop_all() db.create_all()
def recreate_db(): db.drop_all() db.create_all() db.session.commit()
def recreate_db(): """Recreates a database.""" db.reflect() db.drop_all() db.create_all() db.session.commit()