def app(base_app): """Flask application fixture. Scope: function This fixture offers a Flask application with already a database connection and all the models created. When finished it will delete all models. .. code-block:: python def create_ninja_turtle() with app.test_client() as client: somedata = 'ninja turtle' res = client.post(url_for('api.create_object'), content_type='application/json', data=json.dumps(somedata)) assert res.status_code == 200 """ from reana_db.database import Session from reana_db.models import Base, Resource engine = create_engine(base_app.config["SQLALCHEMY_DATABASE_URI"]) base_app.session.bind = engine with base_app.app_context(): if not engine.dialect.has_schema(engine, "__reana"): engine.execute(CreateSchema("__reana")) if not database_exists(engine.url): create_database(engine.url) Base.metadata.create_all(bind=engine) Resource.initialise_default_resources() yield base_app Session.close() # close hanging connections Base.metadata.drop_all(bind=engine)
def create_default_resources(): """Create default quota resources.""" created_resources = Resource.initialise_default_resources() if created_resources: click.secho( "Created resources: {}".format([r.name for r in created_resources]), fg="green", ) else: click.secho("No action to be taken: default resources already exist.", fg="yellow") sys.exit(1)
def db(): """Initialize database fixture.""" from reana_db.database import init_db init_db() Resource.initialise_default_resources()