From 1d2e0f078e1fb9a5ca18d4f488b8cbdf936eb7ad Mon Sep 17 00:00:00 2001 From: Hironsan Date: Wed, 1 Aug 2018 14:55:19 +0900 Subject: [PATCH] Add test case for annotation api --- app/server/tests/test_annotation_api.py | 125 ++++++++++++++++++++++++ app/server/urls.py | 4 +- 2 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 app/server/tests/test_annotation_api.py diff --git a/app/server/tests/test_annotation_api.py b/app/server/tests/test_annotation_api.py new file mode 100644 index 00000000..2e25b7c4 --- /dev/null +++ b/app/server/tests/test_annotation_api.py @@ -0,0 +1,125 @@ +from django.urls import reverse +from rest_framework import status +from rest_framework.test import APITestCase +from mixer.backend.django import mixer +from ..models import * + + +class TestAnnotationAPI(APITestCase): + def setUp(self): + self.username, self.password = 'user', 'pass' + + def create_user(self): + user = User.objects.create_user(username=self.username, password=self.password) + + return user + + def create_superuser(self): + user = User.objects.create_superuser(username=self.username, + password=self.password, + email='hoge@example.com') + return user + + def create_project(self): + project = mixer.blend('server.Project') + + return project + + def create_label(self): + label = mixer.blend('server.Label') + + return label + + def create_doc(self): + doc = mixer.blend('server.Document') + + return doc + + def create_annotation(self): + annotation = mixer.blend('server.DocumentAnnotation') + + return annotation + + def test_get_own_annotation(self): + """ + Ensure we can get own annotation objects. + """ + user = self.create_user() + project = self.create_project() + project.project_type = Project.DOCUMENT_CLASSIFICATION + annotation = self.create_annotation() + annotation.user = user + project.users.add(user) + project.documents.add(annotation.document) + project.save() + annotation.save() + url = reverse('annotations', args=[project.id, annotation.document.id]) + + self.client.login(username=self.username, password=self.password) + response = self.client.get(url, format='json') + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertIsInstance(response.data, list) + self.assertEqual(response.data[0]['id'], annotation.id) + + def test_get_others_annotation(self): + """ + Ensure we cannot get others annotation objects. + """ + user = self.create_user() + project = self.create_project() + project.project_type = Project.DOCUMENT_CLASSIFICATION + annotation = self.create_annotation() + project.users.add(annotation.user) + project.documents.add(annotation.document) + project.save() + annotation.save() + url = reverse('annotations', args=[project.id, annotation.document.id]) + + self.client.login(username=self.username, password=self.password) + response = self.client.get(url, format='json') + + self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + + def test_create_annotation(self): + """ + Ensure we can create a new annotation object. + """ + user = self.create_user() + project = self.create_project() + doc = self.create_doc() + label = self.create_label() + project.project_type = Project.DOCUMENT_CLASSIFICATION + project.users.add(user) + project.documents.add(doc) + project.labels.add(label) + + data = {'label_id': label.id} + url = reverse('annotations', args=[project.id, doc.id]) + self.client.login(username=self.username, password=self.password) + response = self.client.post(url, data, format='json') + + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual(DocumentAnnotation.objects.count(), 1) + + def test_delete_annotation(self): + """ + Ensure we cannot create a new project object by user. + """ + user = self.create_user() + project = self.create_project() + project.project_type = Project.DOCUMENT_CLASSIFICATION + annotation = self.create_annotation() + annotation.user = user + project.users.add(annotation.user) + project.documents.add(annotation.document) + project.save() + annotation.save() + + url = reverse('ann', args=[project.id, annotation.document.id, annotation.id]) + self.assertEqual(DocumentAnnotation.objects.count(), 1) + self.client.login(username=self.username, password=self.password) + response = self.client.delete(url, format='json') + + self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) + self.assertEqual(DocumentAnnotation.objects.count(), 0) diff --git a/app/server/urls.py b/app/server/urls.py index c7a7cbfe..451a92f5 100644 --- a/app/server/urls.py +++ b/app/server/urls.py @@ -17,8 +17,8 @@ urlpatterns = [ path('api/projects//labels/', ProjectLabelsAPI.as_view(), name='labels'), path('api/projects//labels/', ProjectLabelAPI.as_view(), name='label'), path('api/projects//docs/', ProjectDocsAPI.as_view(), name='docs'), - path('api/projects//docs//annotations/', AnnotationsAPI.as_view()), - path('api/projects//docs//annotations/', AnnotationAPI.as_view()), + path('api/projects//docs//annotations/', AnnotationsAPI.as_view(), name='annotations'), + path('api/projects//docs//annotations/', AnnotationAPI.as_view(), name='ann'), path('projects/', ProjectsView.as_view(), name='projects'), path('projects//admin', ProjectAdminView.as_view(), name='project-admin'), path('projects//download', DataDownloadAPI.as_view(), name='download'),