Exemplo n.º 1
0
 def set_constraints():
     self.state = 'checking'
     session = DBSession()
     session.flush()
     sp = self.connection.begin_nested()
     try:
         self.connection.execute('SET CONSTRAINTS ALL IMMEDIATE')
     except:
         sp.rollback()
         raise
     else:
         self.connection.execute('SET CONSTRAINTS ALL DEFERRED')
     finally:
         sp.commit()
         self.state = None
Exemplo n.º 2
0
def session(transaction):
    """ Returns a setup session

    Depends on transaction as storage relies on some interaction there.
    """
    from encoded.storage import DBSession
    return DBSession()
Exemplo n.º 3
0
def connection(request, engine_url):
    from encoded import configure_engine
    from encoded.storage import Base, DBSession
    from sqlalchemy.orm.scoping import ScopedRegistry

    # ``server`` thread must be in same scope
    if type(DBSession.registry) is not ScopedRegistry:
        DBSession.registry = ScopedRegistry(DBSession.session_factory,
                                            lambda: 0)

    engine_settings = {
        'sqlalchemy.url': engine_url,
    }

    engine = configure_engine(engine_settings, test_setup=True)
    connection = engine.connect()
    tx = connection.begin()
    try:
        Base.metadata.create_all(bind=connection)
        session = DBSession(scope=None, bind=connection)
        DBSession.registry.set(session)
        yield connection
    finally:
        tx.rollback()
        connection.close()
        engine.dispose()
Exemplo n.º 4
0
def connection_factory(config, name):
    from encoded import configure_engine
    from encoded.storage import Base, DBSession
    from sqlalchemy.orm.scoping import ScopedRegistry

    scopefunc = config.pluginmanager.getplugin('data').scopefunc

    if type(DBSession.registry) is not ScopedRegistry:
        DBSession.registry = ScopedRegistry(DBSession.session_factory,
                                            scopefunc)

    engine_settings = {
        'sqlalchemy.url': config.option.engine_url,
    }

    engine = configure_engine(engine_settings, test_setup=True)
    connection = engine.connect()
    tx = connection.begin()
    try:
        if engine.url.drivername == 'postgresql':
            # Create the different test sets in different schemas
            if name is None:
                schema_name = 'tests'
            else:
                schema_name = 'tests_%s' % name
            connection.execute('CREATE SCHEMA %s' % schema_name)
            connection.execute('SET search_path TO %s,public' % schema_name)
        Base.metadata.create_all(bind=connection)
        session = DBSession(scope=None, bind=connection)
        DBSession.registry.set(session)
        yield connection
    finally:
        tx.rollback()
        connection.close()
        engine.dispose()
Exemplo n.º 5
0
def app(request, app_settings):
    '''WSGI application level functional testing.
    '''
    from encoded.storage import DBSession

    DBSession.remove()
    DBSession.configure(bind=None)

    from encoded import main
    app = main({}, **app_settings)

    from encoded.commands import create_mapping
    create_mapping.run(app)

    @request.addfinalizer
    def teardown_app():
        # Dispose connections so postgres can tear down
        DBSession.bind.pool.dispose()
        DBSession.remove()
        DBSession.configure(bind=None)

    return app
Exemplo n.º 6
0
def app(app_settings):
    from encoded.storage import DBSession

    DBSession.remove()
    DBSession.configure(bind=None)

    from encoded import main
    app = main({}, **app_settings)

    yield app

    # Dispose connections so postgres can tear down
    DBSession.bind.pool.dispose()
    DBSession.remove()
    DBSession.configure(bind=None)
Exemplo n.º 7
0
def no_deps(request, connection):
    from encoded.storage import DBSession
    from sqlalchemy import event

    session = DBSession()

    @event.listens_for(session, 'after_flush')
    def check_dependencies(session, flush_context):
        #import pytest; pytest.set_trace()
        assert not flush_context.cycles

    @event.listens_for(connection, "before_execute", retval=True)
    def before_execute(conn, clauseelement, multiparams, params):
        #import pytest; pytest.set_trace()
        return clauseelement, multiparams, params

    @request.addfinalizer
    def remove():
        event.remove(session, 'before_flush', check_dependencies)
Exemplo n.º 8
0
 def teardown_app():
     # Dispose connections so postgres can tear down
     DBSession.bind.pool.dispose()
     DBSession.remove()
     DBSession.configure(bind=None)