Add testcase for non-empty recommendations

This commit is contained in:
Mauro Doglio 2017-09-28 12:57:22 +01:00 коммит произвёл Mauro Doglio
Родитель 5d198f3e43
Коммит 134d737976
2 изменённых файлов: 61 добавлений и 2 удалений

Просмотреть файл

@ -2,7 +2,7 @@ from django.core.cache import cache
from django.conf import settings
from django.http import JsonResponse
from taar.recommenders import RecommendationManager
from taar import recommenders
from taar.profile_fetcher import ProfileFetcher
from taar.hbase_client import HBaseClient
@ -13,7 +13,7 @@ def recommendations(request, client_id):
if recommendation_manager is None:
hbase_client = HBaseClient(settings.HBASE_HOST)
profile_fetcher = ProfileFetcher(hbase_client)
recommendation_manager = RecommendationManager(profile_fetcher)
recommendation_manager = recommenders.RecommendationManager(profile_fetcher)
# Cache the recommendation manager for 1h
cache.set("recommendation_manager",
recommendation_manager,

59
tests/test_views.py Normal file
Просмотреть файл

@ -0,0 +1,59 @@
import uuid
import pytest
from django.core.urlresolvers import reverse
@pytest.fixture
def dummy_cache(settings):
settings.CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
class FakeRecommendationManager(object):
def __init__(self, *args, **kwargs):
pass
class EmptyRecommendationManager(FakeRecommendationManager):
def recommend(self, *args, **kwargs):
return []
class StaticRecommendationManager(FakeRecommendationManager):
def recommend(self, *args, **kwargs):
return [
'addon-1',
'addon-2',
'addon-N',
]
@pytest.fixture
def empty_recommendation_manager(monkeypatch):
monkeypatch.setattr('taar.recommenders.RecommendationManager', EmptyRecommendationManager)
@pytest.fixture
def static_recommendation_manager(monkeypatch):
monkeypatch.setattr('taar.recommenders.RecommendationManager', StaticRecommendationManager)
def test_empty_recommendation(dummy_cache, client, empty_recommendation_manager):
response = client.get(reverse('recommendations', kwargs={'client_id': str(uuid.uuid4())}))
assert response.status_code == 200
assert response['Content-Type'] == 'application/json'
assert response.content == b'{"results": []}'
def test_static_recommendation(dummy_cache, client, static_recommendation_manager):
response = client.get(reverse('recommendations', kwargs={'client_id': str(uuid.uuid4())}))
assert response.status_code == 200
assert response['Content-Type'] == 'application/json'
assert response.content == b'{"results": ["addon-1", "addon-2", "addon-N"]}'