Skip to content
Snippets Groups Projects
Commit 5b3d8b00 authored by Martin Burchell's avatar Martin Burchell
Browse files

Modified transport itemcounts to handle 0 counts

The transport layer term_itemcount when called with a date range
now returns terms that fall outside the range with count=0.
parent 8de649e1
No related branches found
No related tags found
1 merge request!4Transport itemcount time
......@@ -5,7 +5,7 @@ from rest_framework.test import APIRequestFactory
from rest_framework import status
from .exceptions import TransportException
from .terms import list as terms_list
request_factory = APIRequestFactory()
......@@ -34,14 +34,38 @@ def list(**kwargs):
return view(request).data
def _add_zero_counts_for_missing_terms(slug, itemcounts):
# We want to return zero counts for terms that fall outside the date range,
# which the API doesn't do for us. So we need to get all the terms for the
# taxonomy and set any that aren't in the date range results to count: 0
all_terms = terms_list(taxonomy=slug)
itemcounts_by_name = {t['name']: t for t in itemcounts}
new_itemcounts = []
for term in all_terms:
if term['name'] not in itemcounts_by_name:
term['count'] = 0
else:
term = itemcounts_by_name[term['name']]
new_itemcounts.append(term)
return new_itemcounts
def term_itemcount(slug, **kwargs):
view = get_view(actions={'get': 'itemcount'})
request = request_factory.get(itemcount_url(slug),
kwargs)
response = view(request, slug=slug)
if status.is_success(response.status_code):
if not status.is_success(response.status_code):
response.data['status_code'] = response.status_code
raise TransportException(response.data)
if 'start_time' not in kwargs or 'end_time' not in kwargs:
return response.data
response.data['status_code'] = response.status_code
raise TransportException(response.data)
return _add_zero_counts_for_missing_terms(slug, response.data)
......@@ -92,3 +92,31 @@ def test_term_itemcount_returns_terms_and_counts_for_range(
end_time=end_time)
assert term['count'] == 7
@pytest.mark.django_db
def test_term_itemcount_returns_zero_term_counts_for_range(
questions_category,
questions_term):
now = time_now()
one_day_ago = now - timedelta(days=1)
one_week_ago = now - timedelta(weeks=1)
eight_days_ago = now - timedelta(days=8)
item_too_recent = item_data(timestamp=now)
item_too_old = item_data(timestamp=eight_days_ago)
transport.items.add_term(item_too_recent['id'],
questions_category.slug,
questions_term.name)
transport.items.add_term(item_too_old['id'],
questions_category.slug,
questions_term.name)
[term] = transport.taxonomies.term_itemcount(
slug=questions_category.slug,
start_time=one_week_ago,
end_time=one_day_ago)
assert term['count'] == 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment