def test_get(django_cache, prepare_key): """Test get.""" django_cache.get.return_value = "def" cache = DjangoCache() assert cache.get("abc") == "def" prepare_key.assert_called_once_with("abc") django_cache.get.assert_called_once_with("prepared key")
def test_prepare_key(): """Test prepare_key.""" normal_cache = DjangoCache() hashing_cache = DjangoCache(key_hash_algorithm="sha512") previous_hashed_keys = [] for key in ["abc", "", "x" * 300]: assert normal_cache.prepare_key(key) == key hashed_key = hashing_cache.prepare_key(key) assert hashed_key != key assert hashed_key not in previous_hashed_keys previous_hashed_keys.append(hashed_key)
import requests from django.conf import settings from django.http import HttpResponse, JsonResponse from cachecontrol import CacheControl from cachecontrol.heuristics import ExpiresAfter from cachecontrol_django import DjangoCache from django.views.decorators.gzip import gzip_page from django.conf import settings cached_sess = CacheControl( requests.session(), cache=DjangoCache(), heuristic=ExpiresAfter(seconds=30) ) @gzip_page def variomes_single_ref(request): # proxy requests to variomes server response = cached_sess.get('%s/fetchLit.jsp' % settings.VARIOMES_BASE_URL, params=request.GET, verify=settings.VARIOMES_VERIFY_REQUESTS) if response.status_code != 200: return HttpResponse(response.content, status=response.status_code) return JsonResponse(response.json()) @gzip_page def variomes_search(request):
def test_delete(django_cache, prepare_key): """Test delete.""" cache = DjangoCache() cache.delete("abc") prepare_key.assert_called_once_with("abc") django_cache.delete.assert_called_once_with("prepared key")
def test_set(django_cache, prepare_key): """Test set.""" cache = DjangoCache() cache.set("abc", "def") prepare_key.assert_called_once_with("abc") django_cache.set.assert_called_once_with("prepared key", "def")