def connection(request, settings): """ sets up a SQLAlchemy engine and returns a connection to the database. :param settings: the settings of the test (given by the testing fixture) :returns: a sqlalchemy connection object """ # the following setup is based on `kotti.resources.initialize_sql`, # except that it explicitly binds the session to a specific connection # enabling us to use savepoints independent from the orm, thus allowing # to `rollback` after using `transaction.commit`... initialize_test_database(settings) from autonomie_base.models.base import DBSESSION, DBBASE engine = engine_from_config(settings, prefix='sqlalchemy.') _connection = engine.connect() DBSESSION.registry.clear() DBSESSION.configure(bind=_connection) DBBASE.metadata.bind = engine def drop_db(): """ drop the test database """ print("DROPPING DB") if __current_test_ini_file().endswith('travis.ini'): return db_settings = get_test_options_from_settings(settings) launch_cmd(db_settings['drop']) request.addfinalizer(drop_db) return _connection
def initialize_sql(engine): """ Initialize the database engine """ DBSESSION.configure(bind=engine) DBBASE.metadata.bind = engine import logging logger = logging.getLogger(__name__) logger.debug("Setting the metadatas") DBBASE.metadata.create_all(engine) from transaction import commit commit() return DBSESSION