Exemplo n.º 1
0
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()
Exemplo n.º 2
0
def create_database():
    from project import app
    from project.extensions import db
    import project.models
    with app.app_context():
        db.create_all()
        db.session.commit()
Exemplo n.º 3
0
    def create_app(self):
        self.app = app_factory()

        with self.app.app_context():
            db.create_all()

        self.app.testing = True
        return self.app
Exemplo n.º 4
0
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
Exemplo n.º 5
0
def do_initdb(application=None):
    if not application:
        global app
    else:
        app = application
    try:
        with app.app_context():
            db.create_all()
            click.echo("db is initialized")
    except Exception as e:
        click.echo(str(e))
Exemplo n.º 6
0
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')
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
def client():
    """flask testing client instance

    this function will try to create a db, return a testing client fixture.
    this function is called before every test and the lines after `yield` are
    being ran after the test.
    so we use this fixture as a setup and cleanup functionality for our tests.

    :return:
    """
    uri = "/tmp/testing-db.db"
    app = make_app(conf="testing")
    with get_app_context('testing'):
        db.create_all()
    yield app.test_client()
    os.remove(uri)
Exemplo n.º 9
0
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
Exemplo n.º 10
0
def create_app():
    app = Flask(__name__)
    used_config = environ.get('APP_SETTINGS', 'config.Config')
    app.config.from_object(used_config)
    db.init_app(app)

    for bp in all_blueprints:
        import_module(bp.import_name)
        app.register_blueprint(bp)

    with app.app_context():
        for module in app.config.get('DB_MODELS_IMPORT', list()):
            import_module(module)

    with app.app_context():
        db.create_all()

    return app
Exemplo n.º 11
0
 def setUp(self):
     db.create_all()
     db.session.commit()
Exemplo n.º 12
0
def syncdb():
    """Init/reset database."""
    db.drop_all()
    db.create_all()
Exemplo n.º 13
0
def recreate_db():
    db.drop_all()
    db.create_all()
    db.session.commit()
    print('Database dropped and recreated.')
Exemplo n.º 14
0
def create():
    """Creates database tables from sqlalchemy models"""
    db.create_all()
Exemplo n.º 15
0
def _db(request, app):
    db.create_all()
    return db
Exemplo n.º 16
0
def syncdb():
    """Init/reset database."""
    db.drop_all()
    db.create_all()
Exemplo n.º 17
0
def configure_extentions(app):
    db.init_app(app)
    moment.init_app(app)

    with app.test_request_context():
        db.create_all()
Exemplo n.º 18
0
 def create_app(self):
     self.app = app_factory()
     with self.app.app_context():
         db.create_all()
         load_fixtures(self.app.config['FIXTURES_DIR'])
     return self.app
Exemplo n.º 19
0
 def create_app(self):
     self.app = app_factory()
     with self.app.app_context():
         db.create_all()
         load_fixtures(self.app.config["FIXTURES_DIR"])
     return self.app
Exemplo n.º 20
0
"""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()
Exemplo n.º 21
0
def init_db():
    db.drop_all()
    db.create_all()
Exemplo n.º 22
0
def init_db():
    db.drop_all()
    db.create_all()
def recreate_db():
    db.drop_all()
    db.create_all()
    db.session.commit()
Exemplo n.º 24
0
def recreate_db():
    """Recreates a database."""
    db.reflect()
    db.drop_all()
    db.create_all()
    db.session.commit()