From 18febb7e1a9a91b28e4c9f32fba089898aea75b9 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Mon, 7 Jun 2021 14:39:21 +0900 Subject: [PATCH] Switch filter_class from DocumentFilter to ExampleFilter in ExampleList --- backend/api/tests/api/test_document.py | 49 ++++++++++++++++++++++++-- backend/api/views/example.py | 4 +-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/backend/api/tests/api/test_document.py b/backend/api/tests/api/test_document.py index 3fd4242e..a0fd5b52 100644 --- a/backend/api/tests/api/test_document.py +++ b/backend/api/tests/api/test_document.py @@ -1,11 +1,13 @@ +from django.utils.http import urlencode from rest_framework import status from rest_framework.reverse import reverse from ...models import DOCUMENT_CLASSIFICATION -from .utils import CRUDMixin, make_doc, make_user, prepare_project +from .utils import (CRUDMixin, make_doc, make_example_state, make_user, + prepare_project) -class TestDocumentListAPI(CRUDMixin): +class TestExampleListAPI(CRUDMixin): def setUp(self): self.project = prepare_project(task=DOCUMENT_CLASSIFICATION) @@ -40,7 +42,48 @@ class TestDocumentListAPI(CRUDMixin): self.assert_create(expected=status.HTTP_403_FORBIDDEN) -class TestDocumentDetail(CRUDMixin): +class TestExampleListFilter(CRUDMixin): + + def setUp(self): + self.project = prepare_project(task=DOCUMENT_CLASSIFICATION) + self.example = make_doc(self.project.item) + make_example_state(self.example, self.project.users[0]) + + def reverse(self, query_kwargs=None): + base_url = reverse(viewname='example_list', args=[self.project.item.id]) + self.url = '{}?{}'.format(base_url, urlencode(query_kwargs)) + + def assert_filter(self, data, user, expected): + self.reverse(query_kwargs=data) + response = self.assert_fetch(user, status.HTTP_200_OK) + self.assertEqual(response.data['count'], expected) + + def test_returns_example_if_confirmed_is_true(self): + user = self.project.users[0] + self.assert_filter(data={'confirmed': 'True'}, user=user, expected=1) + + def test_does_not_return_example_if_confirmed_is_false(self): + user = self.project.users[0] + self.assert_filter(data={'confirmed': 'False'}, user=user, expected=0) + + def test_returns_example_if_confirmed_is_empty(self): + user = self.project.users[0] + self.assert_filter(data={'confirmed': ''}, user=user, expected=1) + + def test_does_not_return_example_if_user_is_different(self): + user = self.project.users[1] + self.assert_filter(data={'confirmed': 'True'}, user=user, expected=0) + + def test_returns_example_if_user_is_different(self): + user = self.project.users[1] + self.assert_filter(data={'confirmed': 'False'}, user=user, expected=1) + + def test_returns_example_if_user_is_different_and_confirmed_is_empty(self): + user = self.project.users[1] + self.assert_filter(data={'confirmed': ''}, user=user, expected=1) + + +class TestExampleDetail(CRUDMixin): def setUp(self): self.project = prepare_project(task=DOCUMENT_CLASSIFICATION) diff --git a/backend/api/views/example.py b/backend/api/views/example.py index ee10e423..75b45ffd 100644 --- a/backend/api/views/example.py +++ b/backend/api/views/example.py @@ -7,7 +7,7 @@ from rest_framework import filters, generics, status from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response -from ..filters import DocumentFilter +from ..filters import DocumentFilter, ExampleFilter from ..models import Example, Project from ..permissions import IsInProjectReadOnlyOrAdmin from ..serializers import ExampleSerializer @@ -20,7 +20,7 @@ class ExampleList(generics.ListCreateAPIView): ordering_fields = ('created_at', 'updated_at') search_fields = ('text', 'filename') model = Example - filter_class = DocumentFilter + filter_class = ExampleFilter @property def project(self):