def initdb(drop): """Initialize the database.""" if drop: db.drop_all() db.create_all() # 输出提示信息 click.echo('Initialized database')
def recreate_database(): from blog import db from blog import models db.reflect() # hacky solution # ref: http://jrheard.tumblr.com/post/12759432733/dropping-all-tables-on-postgres-using db.drop_all() db.create_all()
def initdb(drop): """Initialize the database.""" if drop: click.confirm( 'This operation will delete the database, do you want to continue?', abort=True) db.drop_all() click.echo('Drop tables.') db.create_all() click.echo('Initialized database.')
def test_create_admin(self): db.drop_all() db.create_all() result = self.runner.invoke( args=['admin', '--username', 'test2', '--password', '123456']) self.assertIn('Create admin...', result.output) self.assertIn('Done', result.output) self.assertEqual(User.query.count(), 1) self.assertEqual(User.query.first().username, 'test2') self.assertTrue(User.query.first().validate_password('123456'))
def init_db(migration): db.drop_all() if migration: # create database using migrations print('applying migration') upgrade() else: # create database from model schema directly db.create_all() db.session.commit() cfg = alembic.config.Config('var/migrations/alembic.ini') alembic.command.stamp(cfg, "head")
def tearDown(self): db.session.remove() db.drop_all()
def tearDown(self): '''每个测试之后执行''' db.session.remove() db.drop_all() # 删除所有数据库表 self.app_context.pop() # 退出Flask应用上下文
from blog import db from blog.models import User #Create DB and tables db.drop_all() db.create_all() kyle = User("Kyle","*****@*****.**", "hong0322") #INSERT User db.session.add(kyle) db.session.add(User("admin","*****@*****.**", "admin")) db.session.commit()
def drop_db(): db.drop_all() return ("All tables droppped")
from blog import db, create_app app = create_app() app.app_context().push() db.drop_all() db.create_all()
def tearDown(self): db.session.remove() # 清除数据库会话 db.drop_all() # 删除数据库表
def initdb(drop): if drop: db.drop_all() click.echo('重新创建') db.create_all()
def forge(count, init): """Generate fake messages.""" click.echo('Working...') if init: db.drop_all() db.create_all() from faker import Faker fake = Faker('zh_CN') tag_background_fields = [] category_background_field = [] def fake_users(count): admin = User(username='******', password='******', email='*****@*****.**', create_time=fake.unix_time(), info=fake.text(20), nickname=fake.name(), phone_number=fake.phone_number()) db.session.add(admin) for i in range(int(count / 2)): username_test = fake.user_name() user = User(username=username_test, password=username_test, email=fake.email(), create_time=fake.unix_time(), info=fake.text(20), nickname=fake.name(), phone_number=fake.phone_number()) db.session.add(user) db.session.commit() click.echo('Created {} users'.format(int(count / 2 + 1))) def fake_categorys(): for i in range(5): background_field = 'fl' + str(i) name = '分类' + str(i) category_background_field.append(background_field) category = Category(name=name, background_field=background_field, create_time=fake.unix_time()) db.session.add(category) db.session.commit() click.echo('Created 5 categorys') def fake_tags(): for i in range(5): tag_background_filed = 'tag' + str(i) name = '标签' + str(i) tag_background_fields.append(tag_background_filed) tag = Tag( name=name, background_field=tag_background_filed, # author_id=random.randint(1,9), create_time=fake.unix_time()) db.session.add(tag) db.session.commit() click.echo('Created 5 tags') def fake_messages(count): for i in range(count * 5): message = Message(user_id=random.randint(1, 9), body=fake.text(), create_time=fake.unix_time()) db.session.add(message) db.session.commit() click.echo('Created {} messages'.format(count * 5)) def fake_comments(count): for i in range(count * 20): comment = Comment(user_id=random.randint(1, 9), body=fake.text(), create_time=fake.unix_time()) db.session.add(comment) db.session.commit() click.echo('created {} comments'.format(count * 20)) def fake_articles(count): for i in range(count * 12): article = Article(title=fake.text(20), body=fake.text(1000), summary=fake.text(50), category_field=random.randint(1, 5), tag_field=random.randint(1, 5), author_id=random.randint(1, 10), create_time=fake.unix_time()) db.session.add(article) db.session.commit() click.echo('created {} articles'.format(count * 12)) fake_users(count) fake_categorys() fake_tags() fake_messages(count) fake_articles(count) fake_comments(count) click.echo('finished...')
def tearDown(self): # tearDown 每一个测试程序运行之后 db.session.remove() # 清除数据库会话 db.drop_all() # 删除数据库表
def dropdb(): if prompt_bool("Drop database?"): db.drop_all() print('Drop the db')
def tearDown(self): db.drop_all() db.session.close()
def tearDown(self) -> None: """Remove all settings for app after tests""" db.session.remove() db.drop_all() self.app_context.pop()
def dropdb(): """Drops all database tables.""" db.drop_all()
def initdb(drop): if drop: db.drop_all() db.create_all() click.echo('初始化数据库。')
def tearDownClass(cls): db.drop_all()
def tearDown(self): db.session.remove() db.drop_all() self.app_contaxt.pop()
def testDown(self): #数据库清除 db.session.remove() db.drop_all()