Example #1
0
def test_make_cache_objects():
    cache = make_cache(Cache(), ttl=42, fuzzy_ttl=False, fmt='unicode')
    called = [0]

    @cache.objects('user:{}')
    def get_users(ids):
        called[0] += 1
        return {r: u'user-{}'.format(r) for r in ids if r != 3}

    result = get_users([1, 2])
    assert result == {1: 'user-1', 2: 'user-2'}
    assert called == [1]
    assert get_users.cache.cache == {
        'user:1': (b'user-1', 42),
        'user:2': (b'user-2', 42)
    }

    result = get_users([1, 2])
    assert result == {1: 'user-1', 2: 'user-2'}
    assert called == [1]

    get_users.invalidate([1])
    assert get_users.cache.cache == {'user:2': (b'user-2', 42)}

    result = get_users(set((1, 2)))
    assert result == {1: 'user-1', 2: 'user-2'}
    assert called == [2]

    assert get_users.one(1) == 'user-1'
    assert get_users.one(3) is None
    assert get_users.one(3, _default='None') == 'None'
Example #2
0
def test_make_cache():
    cache = make_cache(Cache(), ttl=42, fuzzy_ttl=False, fmt='unicode')
    called = [0]

    @cache('user:{}')
    def get_user(user_id):
        called[0] += 1
        return u'user-{}'.format(user_id)

    result = get_user(10)
    assert result == 'user-10'
    assert called == [1]
    assert get_user.cache.cache['user:10'] == (b'user-10', 42)

    result = get_user(10)
    assert result == 'user-10'
    assert called == [1]

    assert get_user.get(10) == 'user-10'
    assert called == [1]

    get_user.invalidate(10)
    assert get_user.get(10) is None
    assert called == [1]

    result = get_user(10)
    assert result == 'user-10'
    assert called == [2]

    get_user.set('user-20', 10)
    result = get_user(10)
    assert result == 'user-20'
    assert called == [2]
Example #3
0
def test_make_cache_custom_expire():
    cache = make_cache(Cache(), ttl=42, fuzzy_ttl=False, fmt='unicode')

    @cache('user:{}')
    def get_user(user_id):
        return expire(u'user-{}'.format(user_id), 100)

    result = get_user(10)
    assert result == 'user-10'
    assert get_user.cache.cache['user:10'] == (b'user-10', 100)
Example #4
0
def test_make_cache_objects_custom_expire():
    cache = make_cache(Cache(), ttl=42, fuzzy_ttl=False, fmt='unicode')

    @cache.objects('user:{}')
    def get_users(ids):
        return {1: 'boo', 2: expire('foo', 100)}

    result = get_users([1, 2])
    assert result == {1: 'boo', 2: 'foo'}
    assert get_users.cache.cache == {
        'user:1': (b'boo', 42),
        'user:2': (b'foo', 100)
    }
Example #5
0
async def test_objects_async_cache():
    cache = make_cache(AsyncCache(), ttl=42, fuzzy_ttl=False, fmt='unicode')

    @cache.objects('val:{}')
    def get_val(ids):
        result = {1: 'boo', 2: expire('foo', 10)}
        return {it: result[it] for it in ids if it in result}

    await get_val([1, 2])
    await get_val([1])
    await get_val({1: None})
    await get_val.invalidate([1])
    assert await get_val.one(3) == None
Example #6
0
async def test_simple_async_full():
    cache = make_cache(AsyncCache(), ttl=42, fuzzy_ttl=False, fmt='unicode')

    @cache('val:{}')
    async def get_val(key):
        if key == 'exp':
            return expire(key, 10)
        else:
            return key

    assert await get_val('boo') == 'boo'
    assert await get_val('exp') == 'exp'
    assert await get_val('exp') == 'exp'

    assert await get_val.get('boo') == 'boo'
    await get_val.set('BAR', 'boo')
    assert await get_val.get('boo') == 'BAR'
    await get_val.invalidate('boo')
    assert await get_val.get('boo') == None
Example #7
0
def test_make_cache_for_unknown_serializer():
    c = make_cache(None, fmt='boo')
    with pytest.raises(Exception) as ei:
        c('boo')(lambda boo: None)

    assert 'Unknown serializer' in str(ei.value)