def drop_database(): from project import app from project.extensions import db import project.models with app.app_context(): db.drop_all() db.session.commit()
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 tearDown(self): """ Drop it all! DO NOT FORGET TO CALL THIS METHOD WHEN OVERRIDING """ with self.app.app_context(): db.drop_all()
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 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 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 tearDown(self): db.session.remove() db.drop_all()
def syncdb(): """Init/reset database.""" db.drop_all() db.create_all()
def drop(): """Drops database tables""" if prompt_bool("Are you sure you want to lose all your data"): db.drop_all()
def init_db(): db.drop_all() db.create_all()
def recreate_db(): db.drop_all() db.create_all() db.session.commit() print('Database dropped and recreated.')
def drop_db(): """Drop a database.""" db.reflect() db.drop_all() db.session.commit()
def drop_db(): """Drop testing database.""" app.config.from_object('project.config.TestingConfig') db.reflect() db.drop_all() db.session.commit()
"""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 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()