コード例 #1
0
def test_restart_fails(test_dsn):
    '''This isn't so much a test, as a demonstration of how the connection pooling works (and how it doesn't)
       in the context of a sqlalchmemy engine
       First we execute a query, then we restart the server, drop the connection and attempt to execute another query.
       The second query fails and the connection pool drops all of it's connections and reconnects.
       The third query succeeds, since the pool and been reestablished.
    '''
    with session_scope(test_dsn) as sesh:
        sesh.execute('select 1')
    
    print("\nManually restart the server in another terminal")
    raw_input()

    # NOTE HERE that the first operation on the session raises an exception...
    with pytest.raises(exc.OperationalError) as excinfo:
        sesh = get_session(test_dsn)()
        sesh.execute('select 1')
        sesh.close()
    assert "terminating connection due to administrator command" in excinfo.value.message

    # ... but subsequent operations do not
    for _ in xrange(10):
        with session_scope(test_dsn) as sesh:
            sesh.execute('select 1')

    assert True
コード例 #2
0
def session_scope(dsn):
    '''Provide a transactional scope around a series of operations. But, for the purposes of testing,
       does not re-raise exceptions (unlike pdp_util.session_scope
    '''
    factory = get_session(dsn)
    session = factory()
    try:
        yield session
        session.commit()
    except:
        session.rollback()
        #raise
    finally:
        session.close()
コード例 #3
0
def test_session():
    sesh = pdp_util.get_session(pycds.test_dsn)()
    return sesh
コード例 #4
0
ファイル: test_pcic.py プロジェクト: pacificclimate/pdp_util
def test_dsn_saving():
    cp = {'database': 'crmp', 'user': '******', 'host': 'monsoon.pcic'}
    s1 = get_session(cp)
    s2 = get_session(cp)
    assert s1 == s2
    assert s1() != s2()