Exemplo n.º 1
0
def initdb(drop):
    """Initialize the database."""
    if drop:
        db.drop_all()
    db.create_all()
    # 输出提示信息
    click.echo('Initialized database')
Exemplo n.º 2
0
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()
Exemplo n.º 3
0
 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.')
Exemplo n.º 4
0
 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'))
Exemplo n.º 5
0
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")
Exemplo n.º 6
0
 def tearDown(self):
     db.session.remove()
     db.drop_all()
Exemplo n.º 7
0
 def tearDown(self):
     '''每个测试之后执行'''
     db.session.remove()
     db.drop_all()  # 删除所有数据库表
     self.app_context.pop()  # 退出Flask应用上下文
Exemplo n.º 8
0
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")
Exemplo n.º 10
0
from blog import db, create_app

app = create_app()
app.app_context().push()

db.drop_all()
db.create_all()
Exemplo n.º 11
0
 def tearDown(self):
     db.session.remove()  # 清除数据库会话
     db.drop_all()  # 删除数据库表
Exemplo n.º 12
0
def initdb(drop):
    if drop:
        db.drop_all()
        click.echo('重新创建')
    db.create_all()
Exemplo n.º 13
0
    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...')
Exemplo n.º 14
0
 def tearDown(self):
     # tearDown 每一个测试程序运行之后
     db.session.remove()  # 清除数据库会话
     db.drop_all()  # 删除数据库表
Exemplo n.º 15
0
def dropdb():
    if prompt_bool("Drop database?"):
        db.drop_all()
        print('Drop the db')
Exemplo n.º 16
0
 def tearDown(self):
     db.drop_all()
     db.session.close()
Exemplo n.º 17
0
    def tearDown(self) -> None:
        """Remove all settings for app after tests"""

        db.session.remove()
        db.drop_all()
        self.app_context.pop()
Exemplo n.º 18
0
def dropdb():
    """Drops all database tables."""
    db.drop_all()
Exemplo n.º 19
0
def initdb(drop):
    if drop:
        db.drop_all()
    db.create_all()
    click.echo('初始化数据库。')
Exemplo n.º 20
0
def dropdb():
    """Drops all database tables."""
    db.drop_all()
Exemplo n.º 21
0
 def tearDown(self):
     db.session.remove()
     db.drop_all()
Exemplo n.º 22
0
 def tearDownClass(cls):
     db.drop_all()
Exemplo n.º 23
0
 def tearDown(self):
     db.session.remove()
     db.drop_all()
     self.app_contaxt.pop()
Exemplo n.º 24
0
    def testDown(self):
        #数据库清除

        db.session.remove()
        db.drop_all()