Example #1
0
def setup_database_for_test():
    Database.init(debug=True)

    engine = Database.get_engine()
    connection = engine.connect()
    connection.execute("DROP SCHEMA IF EXISTS unittest CASCADE")
    connection.execute("CREATE SCHEMA unittest")
    connection.execute("SET search_path TO unittest, public")

    tx = connection.begin()
    try:
        # Create fresh schema
        with open(
                os.path.dirname(__file__) + "/../../src/rbb_server/schema.sql",
                'r') as sql_file:
            sql = sql_file.read()
        connection.execute(sql)

        # Insert all our testing data
        with open(os.path.dirname(__file__) + "/test-data.sql",
                  'r') as sql_file:
            sql = sql_file.read()

        statements = sql.split(";")
        for statement in statements:
            sql = statement.strip()
            if sql:
                connection.execute(sql)

        tx.commit()
    except Exception as e:
        tx.rollback()
        logging.error(e)
        print("ERROR: Cannot load unittest data into database!")
        raise e

    connection.close()

    # Make current session use the testing schema
    Database.get_session().execute("SET search_path TO unittest")
Example #2
0
def init_database_connection_for_test():
    Database._session = None
    Database._engine = None
    Database.init(debug=False)
    Database.get_session().execute("SET search_path TO unittest")
Example #3
0
from rbb_server.model.database import Database
from rbb_swagger_server.encoder import JSONEncoder

##########################################################
# This is the startup for the gunicorn production server #
##########################################################

# Setup the API Server
app = connexion.App(__name__, specification_dir='./../rbb_swagger_server/swagger/')
app.app.json_encoder = JSONEncoder
app.add_api('swagger.yaml', swagger_ui=False, arguments={'title': 'API to access the Rosbag Bazaar service'})

# Initialize logging
logging.basicConfig(level=logging.INFO)

if "RBB_BEHIND_PROXY" in os.environ and os.environ["RBB_BEHIND_PROXY"]:
    logging.info("NOTE: Configured to run behind a proxy, disable if not the case!")
    # Fix proxy header
    app.app.wsgi_app = ProxyFix(app.app.wsgi_app)

# Setup link to the database
Database.init()

@app.app.teardown_appcontext
def shutdown_session(exception=None):
    Database.get_session().remove()

print("Running production server...")

# Allow requests from everywhere
CORS(app.app)
Example #4
0
    # Initialize logging
    logging.basicConfig(level=logging.INFO)

    if "RBB_BEHIND_PROXY" in os.environ and os.environ["RBB_BEHIND_PROXY"]:
        logging.info(
            "NOTE: Configured to run behind a proxy, disable if not the case!")
        # Fix proxy header
        app.app.wsgi_app = ProxyFix(app.app.wsgi_app)

    debug_database = False
    if "DEBUG_MODE" in os.environ and os.environ["DEBUG_MODE"]:
        # Enable debug
        logging.info("RUNNING IN DEBUG MODE!")
        debug_database = True
        app.app.config.from_object(TestConfig)

    # Setup link to the database
    Database.init(debug=debug_database)

    @app.app.teardown_appcontext
    def shutdown_session(exception=None):
        logging.info("Shutdown session...")
        Database.get_session().remove()

    # Allow requests from everywhere
    CORS(app.app)

    print("Running local test server...")

    app.run(host='127.0.0.1', port=8080, threaded=True)