Пример #1
0
def test_cache_mixin(mocker):
    assert 'team|0' in repr(Team(id=0, team_name="hello"))
    assert Team(id=0, team_name='hello').pk_name() == 'id'
    assert Team(id=0, team_name='hello').pk_attribute().key == 'id'

    t = Team(team_name='test_cache_mixin')
    DBSession.add(t)
    DBSession.commit()
    assert len(Team.mget([t.id], force=True)) == 1

    with pytest.raises(NotImplementedError):
        CacheMixinBase._cache_client.error

    with pytest.raises(NotImplementedError):
        CacheMixinBase._db_session.error

    with mocker.patch.object(CacheMixinBase, 'RAWDATA_VERSION',
                             mocker.MagicMock(__str__=lambda x: '233')):
        assert Team.gen_raw_key(1) == 'team|1|233'

    with mocker.patch.object(Team.__mapper__, 'primary_key',
                             mocker.MagicMock(return_value=[],
                                              __nonzero__=mocker.MagicMock(
                                                  return_value=False))):
        assert Team(id=2, team_name='hello').pk_name() is None
        assert Team(id=3, team_name='hello').pk_attribute() is None
Пример #2
0
def test_comfirm_close_when_exception(mocker):
    with mocker.patch.object(
            Session, 'commit', mocker.MagicMock(
            side_effect=[gevent.Timeout(), ])):
        session = DBSession()
        session.execute("select 1")
        session.flush()
        with pytest.raises(gevent.Timeout):
            session.commit()
        for engine in session.engines.itervalues():
            for parent in session.transaction._iterate_parents():
                conn = parent._connections.get(engine)
                if conn:
                    assert conn[0].invalidated
        session.close()
Пример #3
0
def test_with_statement_on_exc():
    class ThisExc(Exception):
        pass

    trans_orig = DBSession().transaction

    with pytest.raises(ThisExc):
        with DBSession() as session:
            session.execute('select 1')
            _invalidate_connections(session)
            raise ThisExc('boom!')

    session = DBSession()
    assert session.transaction.is_active
    assert session.transaction is not trans_orig
    session.commit()
    session.close()