Skip to content

Commit

Permalink
Merge pull request #57 from andela/bg-fix-search-responses-165962764
Browse files Browse the repository at this point in the history
#165962764 Fix-search-error-messages
  • Loading branch information
Oluwagbenga Joloko committed May 14, 2019
2 parents aa3f730 + be34b4b commit 7c907da
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ class ArticlePaginator(PageNumberPagination):
"""

def get_paginated_response(self, data):
return Response(OrderedDict([
return OrderedDict([
("pageCount", len(self.page.object_list)),
('articlesCount', self.page.paginator.count),
('next', self.get_next_link()),
('previous', self.get_previous_link()),
('results', data)
]))
])


class FavoritesSerializer(serializers.ModelSerializer):
Expand Down
10 changes: 10 additions & 0 deletions authors/apps/articles/tests/basetests.py
Original file line number Diff line number Diff line change
Expand Up @@ -869,3 +869,13 @@ def filter_by_tag_and_title(self):
format='json'
)
return response

def filter_by_author_and_title(self):
"""
Filter by tag
"""
response = self.client.get((self.articles_url)
+ '?author=' + 'philip' + '?title=' + 'ted',
format='json'
)
return response
14 changes: 12 additions & 2 deletions authors/apps/articles/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def test_filter_non_author(self):
self.create_an_article()
response = self.filter_by_non_author()
self.assertEqual(response.status_code,
status.HTTP_404_NOT_FOUND
status.HTTP_200_OK
)
message = response.data.get("error")
message = response.data.get("message")
self.assertEqual(message, 'We couldn’t find any articles')

def test_filter_by_one_tag(self):
Expand Down Expand Up @@ -69,3 +69,13 @@ def test_filter_by_title_and_tags(self):
self.assertEqual(response.status_code,
status.HTTP_200_OK
)

def test_filter_by_author_and_title(self):
"""
tests filter for articles by author and title
"""
self.create_an_article()
response = self.filter_by_author_and_title()
self.assertEqual(response.status_code,
status.HTTP_200_OK
)
8 changes: 3 additions & 5 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,9 @@ def get(self, request):
response = paginator.get_paginated_response({
"articles": serializer.data
})
if not serializer.data:
response = Response(data={
"error": "We couldn’t find any articles"
}, status=status.HTTP_404_NOT_FOUND)
return response
if response.get("articlesCount") == 0:
response["message"] = "We couldn’t find any articles"
return Response(response)


class RetrieveUpdateArticleAPIView(GenericAPIView):
Expand Down

0 comments on commit 7c907da

Please sign in to comment.