-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(filter-search-functionality): implement filter and search functi…
…onality of articles - ensure users can filter articles based on author's username, article title, tags - ensure users can search fo articles based on keywords [Starts #163383192]
- Loading branch information
Showing
5 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
authors/apps/articles/tests/test_filter_search_functionality.py
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,152 @@ | ||
from django.urls import reverse | ||
from rest_framework import status | ||
from ...authentication.tests.base_class import BaseTest | ||
|
||
|
||
class TestFilterSearchFunctionality(BaseTest): | ||
""" | ||
TestFilterSearchFunctionality handles testing of the search and filter | ||
functionality of articles | ||
""" | ||
def setUp(self): | ||
super().setUp() | ||
self.user = self.activated_user() | ||
self.client.force_authenticate(user=self.user) | ||
self.article = self.create_article(self.user) | ||
self.url = reverse('articles:articles') | ||
|
||
def add_tags_to_article(self): | ||
""" | ||
add_tags_to_article method adds a tag to an article | ||
""" | ||
self.article.tag_list = ["okay"] | ||
self.article.save() | ||
|
||
def test_user_can_filter_articles_by_author_username(self): | ||
""" | ||
Test user can filter articles based on author username | ||
""" | ||
username = self.user.username | ||
response = self.client.get(f"{self.url}?author={username}") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
response = self.client.get(f"{self.url}?author=vdhfvsf") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) | ||
|
||
def test_user_can_filter_by_articles_by_title(self): | ||
""" | ||
Test user can filter articles based on article title | ||
""" | ||
title = self.article.title | ||
response = self.client.get(f"{self.url}?title={title}") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
response = self.client.get(f"{self.url}?title=vdhfvsf") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) | ||
|
||
def test_user_can_filter_by_articles_by_tag(self): | ||
""" | ||
Test user can filter articles based on article tag | ||
""" | ||
self.add_tags_to_article() | ||
tag = self.article.tag_list[0] | ||
response = self.client.get(f"{self.url}?tag={tag}") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
response = self.client.get(f"{self.url}?tag=vdhfvsf") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) | ||
|
||
def test_user_can_filter_by_author_username_and_article_title(self): | ||
""" | ||
Test user can filer for articles based on author username and article | ||
title | ||
""" | ||
username = self.user.username | ||
title = self.article.title | ||
response = self.client.get( | ||
f"{self.url}?author={username}&title={title}" | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
response = self.client.get(f"{self.url}?title=vfvsf&author={username}") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) | ||
response = self.client.get(f"{self.url}?title={title}&author=skjfg") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) | ||
|
||
def test_user_can_filter_by_author_username_and_tag(self): | ||
""" | ||
Test user can filer for articles based on author username and tag | ||
""" | ||
self.add_tags_to_article() | ||
username = self.user.username | ||
tag = self.article.tag_list[0] | ||
response = self.client.get( | ||
f"{self.url}?author={username}&tag={tag}" | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
response = self.client.get(f"{self.url}?tag=vfvsf&author={username}") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) | ||
response = self.client.get(f"{self.url}?tag={tag}&author=skjfg") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) | ||
|
||
def test_user_can_filter_by_author_username_article_title_and_tag(self): | ||
""" | ||
Test user can filer for articles based on author username, article | ||
title and tag | ||
""" | ||
self.add_tags_to_article() | ||
username = self.user.username | ||
title = self.article.title | ||
tag = self.article.tag_list[0] | ||
response = self.client.get( | ||
f"{self.url}?author={username}&tag={tag}&title={title}" | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
response = self.client.get( | ||
f"{self.url}?tag=vfvsf&author={username}&title={title}" | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) | ||
response = self.client.get( | ||
f"{self.url}?tag={tag}&author=skjfg&title={title}" | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) | ||
response = self.client.get( | ||
f"{self.url}?tag={tag}&author={username}&title=hjsdgfhj" | ||
) | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) | ||
|
||
def test_user_can_search_for_articles_by_keywords(self): | ||
""" | ||
Test user can search for articles based on keywords | ||
""" | ||
username = self.user.username | ||
title = self.article.title | ||
description = self.article.description | ||
body = self.article.body | ||
response = self.client.get(f"{self.url}?search={username}") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
response = self.client.get(f"{self.url}?search={title}") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
response = self.client.get(f"{self.url}?search={description}") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
response = self.client.get(f"{self.url}?search={body}") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 1) | ||
response = self.client.get(f"{self.url}?search=vdhfvsf") | ||
self.assertEqual(response.status_code, status.HTTP_200_OK) | ||
self.assertEqual(len(response.data), 0) |
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,19 @@ | ||
from django_filters import FilterSet, rest_framework | ||
from ..models import Article | ||
|
||
|
||
class ArticleFilter(FilterSet): | ||
""" | ||
ArtcleFilter is a custom filter class which makes all my filter fields | ||
case insensitive | ||
""" | ||
title = rest_framework.CharFilter('title', | ||
lookup_expr='icontains') | ||
author = rest_framework.CharFilter('author__username', | ||
lookup_expr='icontains') | ||
tag = rest_framework.CharFilter('tag_list', | ||
lookup_expr='icontains') | ||
|
||
class Meta: | ||
model = Article | ||
fields = ("title", "author", "tag", ) |
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
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