def db(app, request):
    """Initialize the test database.

    Function scoped is required for the fixture, as the views logic
    does session.commit() calls, which can't be rolled back and would
    polute the remaining tests.
    """
    # Updates the database file to a temporary one
    TESTDB_PATH = pj(app.root_path, 'tmp.db')
    TEST_DATABASE_URI = 'sqlite:///' + TESTDB_PATH
    app.config.update(dict(TESTDB_PATH=TESTDB_PATH))
    app.config.update(dict(SQLALCHEMY_DATABASE_URI=TEST_DATABASE_URI))

    # Check if the file already exists and if so, delete it
    if os.path.exists(app.config['TESTDB_PATH']):
        os.unlink(app.config['TESTDB_PATH'])

    # Init db and make it accessible to the app
    _db.init_app(app)
    with app.app_context():
        _db.create_all()

    # Yield the db
    yield _db

    # Teardown code
    # Drop tables and delete the database file
    with app.app_context():
        _db.drop_all()
    os.unlink(app.config['TESTDB_PATH'])
Example #2
0
    # app.config.from_pyfile(cfg_file)
    app.config.from_pyfile(cfg_file)

    # Register all the blueprints
    blueprints = ['sampleapp']
    register_blueprints(app, blueprints)

    return app


if __name__ == '__main__':
    app = create_app()
    from os.path import join as pj
    app.config.update(
        dict(SQLALCHEMY_DATABASE_URI='sqlite:///' +
             pj(app.root_path, 'sampleapp.db')))

    # Init db and make it accessible to the app
    db.init_app(app)
    with app.app_context():
        db.create_all()

    port = app.config['PORT']
    ip = app.config['IP']
    app_name = app.config['APP_NAME']
    host_name = app.config['HOST_NAME']

    server = Flask(__name__)
    server.wsgi_app = app
    server.run(host=ip, port=port)
Example #3
0
    # app.config.from_pyfile(cfg_file)
    app.config.from_pyfile(cfg_file)

    # Register all the blueprints
    blueprints = ['sampleapp']
    register_blueprints(app, blueprints)

    return app

if __name__ == '__main__':
    app = create_app()
    from os.path import join as pj
    app.config.update(dict(
        SQLALCHEMY_DATABASE_URI='sqlite:///' +
                                pj(app.root_path, 'sampleapp.db')
    ))

    # Init db and make it accessible to the app
    db.init_app(app)
    with app.app_context():
        db.create_all()

    port = app.config['PORT']
    ip = app.config['IP']
    app_name = app.config['APP_NAME']
    host_name = app.config['HOST_NAME']

    server = Flask(__name__)
    server.wsgi_app = app
    server.run(host=ip, port=port)