def app(config): """Create and configure an app instance for tests""" test_app = create_app(config.FLASK, migrations_dir=config.MIGRATE_DIR) with test_app.app_context(): db.create_all() load_initial_data() register_blueprints(test_app) create_sitemap(test_app, config.DOMAIN) yield test_app
def db(app): """A database for the tests.""" _db.app = app with app.app_context(): _db.create_all() yield _db # Explicitly close DB connection _db.session.close() _db.drop_all()
def deploy(dropdb): if dropdb: db.drop_all() print("Database is droped.") db.create_all() print("---------- Start update. ----------") update_posts([], update_all=True) print("----------- End update. -----------") # 在 redis 中存储最后更新时间 redis.set("last_update_at", datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
def setUp(self): db.drop_all() db.create_all() tag1 = Tag(value="tag1") db.session.add(tag1) tag2 = Tag(value="tag2") db.session.add(tag2) user = User(user_name="user1", first_name="First Name", last_name="Last Name", password=generate_password_hash("1234")) db.session.add(user) post = Post( title="Post title", summary="""Post summary""", body="""Post body""", author=user, created=datetime(2020, 1, 15, 10, 30), updated=datetime(2020, 1, 15, 10, 30), ) post_with_tags = Post(title="Second post title", summary="""Second post summary""", body="""Second post body""", author=user, created=datetime(2020, 1, 15, 10, 30), updated=datetime(2020, 1, 15, 10, 30), tags=[tag1, tag2]) db.session.add(post) db.session.add(post_with_tags) db.session.commit() chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--no-sandbox") chrome_options.add_argument("--disable-dev-shm-usage") self.driver = webdriver.Chrome(options=chrome_options) self.driver.get(self.get_server_url())
def setUp(self): db.drop_all() db.create_all() user = User(user_name="user1", first_name="First Name", last_name="Last Name", password=generate_password_hash("1234")) db.session.add(user) post = Post( title="Post title", summary="""Post summary""", body="""Post body""", author=user, created=datetime(2020, 1, 15, 10, 30) + timedelta(days=1), updated=datetime(2020, 1, 15, 10, 30) + timedelta(days=1), ) db.session.add(post) db.session.commit()
def create_app(config_name='default'): app = Flask(__name__) app.config.from_object(config[config_name]) # config[config_name].init_app(app) mail.init_app(app) bootstrap.init_app(app) moment.init_app(app) login_manager.init_app(app) pagedown.init_app(app) db.init_app(app) with app.app_context(): db.create_all(app=app) from .main import main from .auth import auth app.register_blueprint(main) app.register_blueprint(auth, url_prefix='/auth') return app
def init_database(): # create the database and the database table db.create_all() # Insert user data user1 = User() user1.username = '******' user1.email = '*****@*****.**' user1.password = '******' db.session.add(user1) user2 = User() user2.username = '******' user2.email = '*****@*****.**' user2.password = '******' db.session.add(user2) # commit the changes for the users db.session.commit() yield db # this is where testing happens! db.drop_all()
def setUp(self): self.app = create_app('blog.configs.TestConfig', 'test') with self.app.app_context(): db.create_all() self.client = self.app.test_client()
def createdb(): from blog.models import db, User db.create_all() user = User('admin', '*****@*****.**', 'admin_pw1') db.session.add(user) db.session.commit()
def setUp(self): self.app = create_app('blog.configs.TestConfig') with self.app.app_context(): db.create_all() self.client = self.app.test_client()
from blog.models import User, Post, db # Create the database db.drop_all() # re-create db.create_all() # Create two users user1 = User(username = '******', email = '*****@*****.**', password = '******') user2 = User(username = '******', email = '*****@*****.**', password = '******') db.session.add(user1) db.session.add(user2) db.session.commit() # Query db print(User.query.all()) print(User.query.first()) print(User.query.filter_by(username = '******').all()) dsr = User.query.filter_by(username = '******').first() print(dsr) print(dsr.id, dsr.password, dsr.image_file, dsr.username, sep=":") ak = User.query.get(2) print(ak)
def createdb(): """ Initialize a db from models """ db.create_all()
def create_tables(): logger.info('Creating tables...') db.create_all() db.session.commit() logger.info('SUCCESSFULLY')