def database(app): with app.app_context(): db.drop_all() db.create_all() yield db
def create_test_users(): with app.app_context(): print('Committing to database ...') db.create_all() user_datastore.find_or_create_role(name='superadmin', description='Superadministrator') user_datastore.find_or_create_role(name='master', description='Master') user_datastore.find_or_create_role(name='leed', description='Leed') encrypted_password = utils.encrypt_password('iddqd3133122') db.session.commit() user_datastore.create_user(email='*****@*****.**', password=encrypted_password, confirmed_at=datetime.datetime.now()) db.session.commit() user_datastore.create_user(email='*****@*****.**', password=encrypted_password, confirmed_at=datetime.datetime.now()) db.session.commit() user_datastore.create_user(email='*****@*****.**', password=encrypted_password, confirmed_at=datetime.datetime.now()) db.session.commit() user_datastore.add_role_to_user('*****@*****.**', 'superadmin') user_datastore.add_role_to_user('*****@*****.**', 'master') user_datastore.add_role_to_user('*****@*****.**', 'leed') db.session.commit() print('SuperUser was created!')
def upgrade_db(): os.system('python manage.py db upgrade') a = create_test_users() with app.app_context(): print('Creating all ...') db.create_all() print('All is created!!')
def setup(self): app = create_test_app() self.app = app self.client = app.test_client() with app.app_context(): db.drop_all() db.create_all()
def init_empty_database(app): with app.app_context(): # Create the database db.create_all() yield # tests run here # Destroy the database db.drop_all()
def create_db(): app = Flask(__name__) app.config.from_object(os.environ['APP_SETTINGS']) db.init_app(app) with app.app_context(): db.create_all() return None
def setup(self): os.environ['MODE'] = 'TESTING' app = create_app() self.app = app self.client = app.test_client() with app.app_context(): db.drop_all() db.create_all()
def create_db(app): """ A test fixture for creating a database table, with the schema defined in model, once per test class and deleted after exiting the scope of a test class. """ db.app = app with app.app_context(): db.create_all() yield db db.session.close() db.drop_all()
def app_old(): """ Fixture app returning an instance of Flask app with created models in the database """ app = create_app("testing") with app.app_context(): db.create_all() yield app db.drop_all()
def init_database(app, colors_list): with app.app_context(): # Create the database db.create_all() # Insert color data for color in colors_list: db.session.add(Color(color=color['color'], value=color['value'])) # Commit the changes db.session.commit() yield # tests run here # Destroy the database db.drop_all()
def client(): client = app.test_client() with app.app_context(): for table in reversed(db.metadata.sorted_tables): try: db.engine.execute(table.delete()) except: pass db.create_all() load_model_fixtures(db, Company, 'company.csv') load_model_fixtures(db, Tags, 'tags.csv') load_model_fixtures(db, Company_Tags_Map, 'company_tags_map.csv') yield client
def clear(): """Clear DB""" db.drop_all() db.create_all() # add admin user admin_user = User(username='******', email='*****@*****.**', password='******', admin=True) db.session.add(admin_user) db.session.commit() flash("Database refreshed") return redirect(url_for('main_bp.chat'))
def run(): with app.app_context(): db.drop_all() db.create_all() # Administrator admin = User(email="*****@*****.**") db.session.add(admin) # First user user1 = User(email="*****@*****.**") db.session.add(user1) # Second user user2 = User(email="*****@*****.**") db.session.add(user2) db.session.commit()
def run(): with app.app_context(): print(f"ran users script. DB: {db}") db.drop_all() db.create_all() # Admin user admin = User(email="*****@*****.**") db.session.add(admin) # First base user user1 = User(email="*****@*****.**") db.session.add(user1) # Second base user user2 = User(email="*****@*****.**") db.session.add(user2) print(f"user.first(): {User.query.first()}") db.session.commit()
from datetime import datetime from flask import Flask, jsonify, request app = Flask(__name__) config = app.config.from_envvar('APP_SETTINGS') from application.models import db, Task with app.app_context(): db.init_app(app) db.create_all() def get_task(task_id): return db.session.query(Task).filter_by(id=task_id).first_or_404() @app.route("/tasks") def tasks_list(): tasks = db.session.query(Task).all() return jsonify(tasks=[i.serialize for i in tasks]) @app.route("/tasks/<int:task_id>") def task_by_id(task_id): task = get_task(task_id) return jsonify(task=task.serialize) @app.route("/tasks/post", methods=['POST']) def task_post():
def create_all(): with app.app_context(): print('Creating all ...') db.create_all() print('All is created!!')
def create_db(): """Create database.""" print 'creating db...' print db.engine.url db.create_all()
def db_rebuild(): db.drop_all() db.create_all() return "Database rebuilt!"
def syncdb(): with app.app_context(): db.create_all()
def app(): """Flask application fixture """ _app = build_app(env='test') db.create_all() yield _app db.drop_all()
def createdb(): """Create database.""" db.create_all()
def syncdb(): """根据model创建数据库tables""" db.create_all()
from application.models import db, UserAdmin db.drop_all() db.create_all() # create admin first user adm_user = UserAdmin(email='*****@*****.**', password='******') db.session.add(adm_user) db.session.commit()
def initdb(): """create database tables""" db.create_all()