Пример #1
0
def test_cache_get(global_request):
    cache = InRequestCache(location=None, params={})
    expire_at = time.time()
    cache_data = {
        cache.cache_key('key'): CacheItem('value', expire_at),
        cache.cache_key('key2'): CacheItem('value2', expire_at + 2),
    }
    setattr(global_request, cache.cache_name, cache_data)

    assert cache.get('key', default='default') == 'default'
    assert cache.get('key2') == 'value2'
Пример #2
0
def test_cache_set_timeout(global_request):
    cache = InRequestCache(location=None, params={})
    timeout = 2
    start = time.time()
    cache.set('key', 'value', timeout=timeout)
    end = time.time()
    item = getattr(global_request, cache.cache_name)[cache.cache_key('key')]
    assert isinstance(item, CacheItem)
    assert item.expire_at >= start + timeout
    assert item.expire_at <= end + timeout
Пример #3
0
def test_cache_delete(global_request):
    cache = InRequestCache(location=None, params={})
    cache_key = cache.cache_key('key')
    cache_data = {
        cache_key: CacheItem('value', time.time() + 10)
    }
    setattr(global_request, cache.cache_name, cache_data)
    assert cache_key in cache_data
    cache.delete('key')
    assert cache_key not in cache_data
Пример #4
0
def test_cache_add(global_request):
    cache = InRequestCache(location=None, params={})
    cache_data = {
        cache.cache_key('key'): CacheItem('value', time.time() + 10)
    }
    setattr(global_request, cache.cache_name, cache_data)

    assert cache.add('key', 'value2') == False
    assert cache.get('key') == 'value'

    assert cache.add('key2', 'value2') == True
    assert cache.get('key2') == 'value2'
Пример #5
0
def test_cache_set(global_request):
    cache = InRequestCache(location=None, params={})
    cache.set('key', 'value')
    item = getattr(global_request, cache.cache_name)[cache.cache_key('key')]
    assert isinstance(item, CacheItem)
    assert item.value == 'value'