diff --git a/backend/examples/tests/test_document.py b/backend/examples/tests/test_example.py similarity index 59% rename from backend/examples/tests/test_document.py rename to backend/examples/tests/test_example.py index 639cb686..b39eed4e 100644 --- a/backend/examples/tests/test_document.py +++ b/backend/examples/tests/test_example.py @@ -1,12 +1,11 @@ -from django.conf import settings from django.utils.http import urlencode from rest_framework import status from rest_framework.reverse import reverse -from .utils import make_doc, make_example_state +from .utils import make_assignment, make_doc, make_example_state from api.tests.utils import CRUDMixin from projects.models import ProjectType -from projects.tests.utils import assign_user_to_role, prepare_project +from projects.tests.utils import prepare_project from users.tests.utils import make_user @@ -15,10 +14,12 @@ class TestExampleListAPI(CRUDMixin): self.project = prepare_project(task=ProjectType.DOCUMENT_CLASSIFICATION) self.non_member = make_user() self.example = make_doc(self.project.item) + for member in self.project.members: + make_assignment(self.project.item, self.example, member) self.data = {"text": "example"} self.url = reverse(viewname="example_list", args=[self.project.item.id]) - def test_allows_project_member_to_list_docs(self): + def test_allows_project_member_to_list_examples(self): for member in self.project.members: response = self.assert_fetch(member, status.HTTP_200_OK) self.assertEqual(response.data["count"], 1) @@ -26,33 +27,24 @@ class TestExampleListAPI(CRUDMixin): for item in response.data["results"]: self.assertIn("text", item) - def test_denies_non_project_member_to_list_docs(self): + def test_denies_non_project_member_to_list_examples(self): self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN) - def test_denies_unauthenticated_user_to_list_docs(self): + def test_denies_unauthenticated_user_to_list_examples(self): self.assert_fetch(expected=status.HTTP_403_FORBIDDEN) - def test_allows_project_admin_to_create_doc(self): + def test_allows_project_admin_to_create_example(self): response = self.assert_create(self.project.admin, status.HTTP_201_CREATED) self.assertEqual(response.data["text"], self.data["text"]) - def test_denies_project_staff_to_create_doc(self): + def test_denies_non_admin_to_create_example(self): for member in self.project.staffs: self.assert_create(member, status.HTTP_403_FORBIDDEN) - def test_denies_unauthenticated_user_to_create_doc(self): + def test_denies_unauthenticated_user_to_create_example(self): self.assert_create(expected=status.HTTP_403_FORBIDDEN) - def test_is_confirmed(self): - make_example_state(self.example, self.project.admin) - response = self.assert_fetch(self.project.admin, status.HTTP_200_OK) - self.assertTrue(response.data["results"][0]["is_confirmed"]) - - def test_is_not_confirmed(self): - response = self.assert_fetch(self.project.admin, status.HTTP_200_OK) - self.assertFalse(response.data["results"][0]["is_confirmed"]) - - def test_does_not_share_another_user_confirmed(self): + def test_example_is_not_approved_if_another_user_approve_it(self): make_example_state(self.example, self.project.admin) response = self.assert_fetch(self.project.annotator, status.HTTP_200_OK) self.assertFalse(response.data["results"][0]["is_confirmed"]) @@ -60,23 +52,13 @@ class TestExampleListAPI(CRUDMixin): class TestExampleListCollaborative(CRUDMixin): def setUp(self): - self.project = prepare_project(task=ProjectType.DOCUMENT_CLASSIFICATION) + self.project = prepare_project(task=ProjectType.DOCUMENT_CLASSIFICATION, collaborative_annotation=True) self.example = make_doc(self.project.item) + for member in self.project.members: + make_assignment(self.project.item, self.example, member) self.url = reverse(viewname="example_list", args=[self.project.item.id]) - def test_shares_confirmed_in_same_role(self): - annotator1 = make_user() - assign_user_to_role(annotator1, self.project.item, settings.ROLE_ANNOTATOR) - annotator2 = make_user() - assign_user_to_role(annotator2, self.project.item, settings.ROLE_ANNOTATOR) - - make_example_state(self.example, annotator1) - response = self.assert_fetch(annotator1, status.HTTP_200_OK) - self.assertTrue(response.data["results"][0]["is_confirmed"]) - response = self.assert_fetch(annotator2, status.HTTP_200_OK) - self.assertTrue(response.data["results"][0]["is_confirmed"]) - - def test_does_not_share_confirmed_in_other_role(self): + def test_example_is_approved_if_someone_approve_it(self): admin = self.project.admin approver = self.project.approver @@ -84,14 +66,20 @@ class TestExampleListCollaborative(CRUDMixin): response = self.assert_fetch(admin, status.HTTP_200_OK) self.assertTrue(response.data["results"][0]["is_confirmed"]) response = self.assert_fetch(approver, status.HTTP_200_OK) - self.assertFalse(response.data["results"][0]["is_confirmed"]) + self.assertTrue(response.data["results"][0]["is_confirmed"]) class TestExampleListFilter(CRUDMixin): def setUp(self): self.project = prepare_project(task=ProjectType.DOCUMENT_CLASSIFICATION) - self.example = make_doc(self.project.item) - make_example_state(self.example, self.project.admin) + example1 = make_doc(self.project.item) + example2 = make_doc(self.project.item) + example3 = make_doc(self.project.item) + for member in self.project.members: + make_assignment(self.project.item, example1, member) + make_assignment(self.project.item, example2, member) + make_assignment(self.project.item, example3, member) + make_example_state(example1, self.project.admin) def reverse(self, query_kwargs=None): base_url = reverse(viewname="example_list", args=[self.project.item.id]) @@ -102,67 +90,59 @@ class TestExampleListFilter(CRUDMixin): response = self.assert_fetch(user, status.HTTP_200_OK) self.assertEqual(response.data["count"], expected) - def test_returns_example_if_confirmed_is_true(self): + def test_returns_only_approved_examples(self): user = self.project.admin self.assert_filter(data={"confirmed": "True"}, user=user, expected=1) - def test_does_not_return_example_if_confirmed_is_false(self): + def test_returns_only_non_approved_examples(self): user = self.project.admin - self.assert_filter(data={"confirmed": "False"}, user=user, expected=0) + self.assert_filter(data={"confirmed": "False"}, user=user, expected=2) - def test_returns_example_if_confirmed_is_empty(self): + def test_returns_all_examples(self): user = self.project.admin - self.assert_filter(data={"confirmed": ""}, user=user, expected=1) + self.assert_filter(data={"confirmed": ""}, user=user, expected=3) - def test_does_not_return_example_if_user_is_different(self): + def test_does_not_return_approved_example_to_another_user(self): user = self.project.approver self.assert_filter(data={"confirmed": "True"}, user=user, expected=0) - def test_returns_example_if_user_is_different(self): - user = self.project.approver - 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.approver - self.assert_filter(data={"confirmed": ""}, user=user, expected=1) - class TestExampleDetail(CRUDMixin): def setUp(self): self.project = prepare_project(task=ProjectType.DOCUMENT_CLASSIFICATION) self.non_member = make_user() - doc = make_doc(self.project.item) + example = make_doc(self.project.item) self.data = {"text": "example"} - self.url = reverse(viewname="example_detail", args=[self.project.item.id, doc.id]) + self.url = reverse(viewname="example_detail", args=[self.project.item.id, example.id]) - def test_allows_project_member_to_get_doc(self): + def test_allows_project_member_to_get_example(self): for member in self.project.members: response = self.assert_fetch(member, status.HTTP_200_OK) self.assertIn("text", response.data) - def test_denies_non_project_member_to_get_doc(self): + def test_denies_non_project_member_to_get_example(self): self.assert_fetch(self.non_member, status.HTTP_403_FORBIDDEN) - def test_denies_unauthenticated_user_to_get_doc(self): + def test_denies_unauthenticated_user_to_get_example(self): self.assert_fetch(expected=status.HTTP_403_FORBIDDEN) - def test_allows_project_admin_to_update_doc(self): + def test_allows_project_admin_to_update_example(self): response = self.assert_update(self.project.admin, status.HTTP_200_OK) self.assertEqual(response.data["text"], self.data["text"]) - def test_denies_project_staff_to_update_doc(self): + def test_denies_non_admin_to_update_example(self): for member in self.project.staffs: self.assert_update(member, status.HTTP_403_FORBIDDEN) - def test_denies_non_project_member_to_update_doc(self): + def test_denies_non_project_member_to_update_example(self): self.assert_update(self.non_member, status.HTTP_403_FORBIDDEN) - def test_allows_project_admin_to_delete_doc(self): + def test_allows_project_admin_to_delete_example(self): self.assert_delete(self.project.admin, status.HTTP_204_NO_CONTENT) - def test_denies_project_staff_to_delete_doc(self): + def test_denies_non_admin_to_delete_example(self): for member in self.project.staffs: self.assert_delete(member, status.HTTP_403_FORBIDDEN) - def test_denies_non_project_member_to_delete_doc(self): + def test_denies_non_project_member_to_delete_example(self): self.assert_delete(self.non_member, status.HTTP_403_FORBIDDEN) diff --git a/backend/examples/views/example.py b/backend/examples/views/example.py index c045cac5..43b0963a 100644 --- a/backend/examples/views/example.py +++ b/backend/examples/views/example.py @@ -1,6 +1,3 @@ -import random - -from django.db.models import F from django.shortcuts import get_object_or_404 from django_filters.rest_framework import DjangoFilterBackend from rest_framework import filters, generics, status @@ -28,14 +25,7 @@ class ExampleList(generics.ListCreateAPIView): return get_object_or_404(Project, pk=self.kwargs["project_id"]) def get_queryset(self): - queryset = self.model.objects.filter(project=self.project) - if self.project.random_order: - # Todo: fix the algorithm. - random.seed(self.request.user.id) - value = random.randrange(2, 20) - queryset = queryset.annotate(sort_id=F("id") % value).order_by("sort_id", "id") - else: - queryset = queryset.order_by("created_at") + queryset = self.model.objects.filter(project=self.project, assignments__assignee=self.request.user) return queryset def perform_create(self, serializer):