-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from .base_test import BaseTestCase | ||
|
||
|
||
class TestSearchArticles(BaseTestCase): | ||
"class to test searching and filtering of articles" | ||
|
||
def test_search_by_title(self): | ||
"""class to test searching by title""" | ||
|
||
self.user_signup() | ||
token = self.user_login() | ||
response = self.create_article() | ||
response = self.client.get(reverse("articles:filter"), data={'title': 'Hello'}, format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
def test_search_by_author(self): | ||
"""class to test searching by author""" | ||
|
||
self.user_signup() | ||
token = self.user_login() | ||
response = self.create_article() | ||
response = self.client.get(reverse("articles:filter"), data={'author': 'JohnDoe'}, format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
|
||
def test_search_by_tag(self): | ||
"""class to test searching by author""" | ||
|
||
self.user_signup() | ||
token = self.user_login() | ||
response = self.create_article() | ||
response = self.client.get(reverse("articles:filter"), data={'tag': 'dummydata'}, format='json', HTTP_AUTHORIZATION='Token ' + token) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from django_filters import rest_framework as filters | ||
from rest_framework.generics import ListAPIView | ||
from django_filters.rest_framework import DjangoFilterBackend | ||
from rest_framework.filters import SearchFilter, OrderingFilter | ||
|
||
from authors.apps.articles.serializers import ArticleAuthorSerializer | ||
from authors.apps.articles.models import Article | ||
from rest_framework.response import Response | ||
|
||
|
||
class ArticlesFilter(filters.FilterSet): | ||
author = filters.CharFilter(field_name='author__username', lookup_expr='icontains') | ||
title = filters.CharFilter(field_name='title', lookup_expr='icontains') | ||
tag = filters.CharFilter(field_name='tags__name', lookup_expr='icontains') | ||
|
||
|
||
class Meta: | ||
model = Article | ||
fields = ['title', 'author', 'tag'] | ||
|
||
class FilterSearchListAPIView(ListAPIView): | ||
"""class for filtering and searching articles""" | ||
queryset = Article.objects.all() | ||
serializer_class = ArticleAuthorSerializer | ||
filter_backends = (SearchFilter, DjangoFilterBackend) | ||
filterset_class = ArticlesFilter | ||
search_fields = ('title', 'author__username', 'tags__name') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,5 @@ urllib3==1.24.1 | |
whitenoise==4.1.2 | ||
wrapt==1.10.11 | ||
psycopg2==2.7.6.1 | ||
django-filter==2.0.0 | ||
|