Exemplo n.º 1
0
def test_mget_cache_miss(mocker):
    t = Team(id=0, team_name="hello")

    mocker.patch.object(_RedisWrapper, "mget", mocker.Mock(return_value=None))
    mocker.patch.object(Team, "_db_session", MockSession([t]))

    incr = mocker.MagicMock()
    mocker.patch.object(Team, "_statsd_incr", incr)
    Team.mget([0])
    incr.assert_called_with("miss", 1)
Exemplo n.º 2
0
def test_new_cache_mget(mocker):
    t1 = Team(id=0, team_name="hello")
    t2 = Team(id=1, team_name="world")

    mocker.patch.object(_RedisWrapper, "mget", mocker.Mock(return_value=None))
    mocker.patch.object(Team, "_db_session", MockSession([t1, t2]))

    mset_raw = mocker.MagicMock()
    mocker.patch.object(Team, "_mset_raw", mset_raw)
    Team.mget([0, 1])
    mset_raw.assert_called_with([t1, t2], nx=True)
Exemplo n.º 3
0
def test_mget_cache_hit(mocker):
    mocker.patch.object(CacheMixin, "_db_session", DBSession)

    t1 = Team(id=0, team_name="hello")
    t2 = Team(id=1, team_name="world")
    raw = [t.__rawdata__ for t in (t1, t2)]

    mocker.patch.object(_RedisWrapper, "mget", mocker.Mock(return_value=raw))

    # get
    incr = mocker.MagicMock()
    mocker.patch.object(Team, "_statsd_incr", incr)
    Team.mget([0, 1])
    incr.assert_called_with("hit", 2)
Exemplo n.º 4
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
Exemplo n.º 5
0
def test_mget_get_primary_key(mocker):
    t = Team(id=0, team_name="hello")

    mocker.patch.object(_RedisWrapper, "mget", mocker.Mock(return_value=None))
    mocker.patch.object(Team, "_db_session", MockSession([t]))

    pk = mocker.MagicMock()
    mocker.patch.object(Team, "pk", pk)
    a = Team.mget([0], as_dict=True)
    assert a == {pk: t}
Exemplo n.º 6
0
def test_mget_from_db(mocker):
    t1 = Team(id=0, team_name="hello")
    t2 = Team(id=1, team_name="world")

    mocker.patch.object(_RedisWrapper, "mget", mocker.Mock(return_value=None))
    mocker.patch.object(Team, "_db_session", MockSession([t1, t2]))

    r = Team.mget([0, 1, 2])
    assert len(r) == 2
    assert r[0] is t1 and r[1] is t2
Exemplo n.º 7
0
def test_mget_from_cache(mocker):
    mocker.patch.object(CacheMixin, "_db_session", DBSession)

    t1 = Team(id=0, team_name="hello")
    t2 = Team(id=1, team_name="world")

    with mocker.patch.object(_RedisWrapper, "mget",
                             return_value=[t1.__rawdata__, t2.__rawdata__]):
        m = Team.mget([0, 1])
        assert all([t._cached for t in m])
Exemplo n.º 8
0
def test_mget_from_session(mocker):
    session = DBSession
    mocker.patch.object(CacheMixin, "_db_session", DBSession)

    t1 = Team(id=0, team_name="hello")
    t2 = Team(id=1, team_name="world")

    for t in (t1, t2):
        session.add(t)
        session.commit()

    r = Team.mget([0, 1])
    assert r[0] is t1 and r[1] is t2
    session.close()