You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.3 KiB

  1. from django.urls import reverse
  2. from rest_framework import status
  3. from rest_framework.test import APITestCase
  4. from mixer.backend.django import mixer
  5. from ..models import *
  6. class TestAnnotationAPI(APITestCase):
  7. def setUp(self):
  8. self.username, self.password = 'user', 'pass'
  9. def create_user(self):
  10. user = User.objects.create_user(username=self.username, password=self.password)
  11. return user
  12. def create_superuser(self):
  13. user = User.objects.create_superuser(username=self.username,
  14. password=self.password,
  15. email='hoge@example.com')
  16. return user
  17. def create_project(self):
  18. project = mixer.blend('server.Project')
  19. return project
  20. def create_label(self):
  21. label = mixer.blend('server.Label')
  22. return label
  23. def create_doc(self):
  24. doc = mixer.blend('server.Document')
  25. return doc
  26. def create_annotation(self):
  27. annotation = mixer.blend('server.DocumentAnnotation')
  28. return annotation
  29. def test_get_own_annotation(self):
  30. """
  31. Ensure we can get own annotation objects.
  32. """
  33. user = self.create_user()
  34. project = self.create_project()
  35. project.project_type = Project.DOCUMENT_CLASSIFICATION
  36. annotation = self.create_annotation()
  37. annotation.user = user
  38. project.users.add(user)
  39. project.documents.add(annotation.document)
  40. project.save()
  41. annotation.save()
  42. url = reverse('annotations', args=[project.id, annotation.document.id])
  43. self.client.login(username=self.username, password=self.password)
  44. response = self.client.get(url, format='json')
  45. self.assertEqual(response.status_code, status.HTTP_200_OK)
  46. self.assertIsInstance(response.data, list)
  47. self.assertEqual(response.data[0]['id'], annotation.id)
  48. def test_get_others_annotation(self):
  49. """
  50. Ensure we cannot get others annotation objects.
  51. """
  52. user = self.create_user()
  53. project = self.create_project()
  54. project.project_type = Project.DOCUMENT_CLASSIFICATION
  55. annotation = self.create_annotation()
  56. project.users.add(annotation.user)
  57. project.documents.add(annotation.document)
  58. project.save()
  59. annotation.save()
  60. url = reverse('annotations', args=[project.id, annotation.document.id])
  61. self.client.login(username=self.username, password=self.password)
  62. response = self.client.get(url, format='json')
  63. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  64. def test_create_annotation(self):
  65. """
  66. Ensure we can create a new annotation object.
  67. """
  68. user = self.create_user()
  69. project = self.create_project()
  70. doc = self.create_doc()
  71. label = self.create_label()
  72. project.project_type = Project.DOCUMENT_CLASSIFICATION
  73. project.users.add(user)
  74. project.documents.add(doc)
  75. project.labels.add(label)
  76. data = {'label_id': label.id}
  77. url = reverse('annotations', args=[project.id, doc.id])
  78. self.client.login(username=self.username, password=self.password)
  79. response = self.client.post(url, data, format='json')
  80. self.assertEqual(response.status_code, status.HTTP_200_OK)
  81. self.assertEqual(DocumentAnnotation.objects.count(), 1)
  82. def test_delete_annotation(self):
  83. """
  84. Ensure we cannot create a new project object by user.
  85. """
  86. user = self.create_user()
  87. project = self.create_project()
  88. project.project_type = Project.DOCUMENT_CLASSIFICATION
  89. annotation = self.create_annotation()
  90. annotation.user = user
  91. project.users.add(annotation.user)
  92. project.documents.add(annotation.document)
  93. project.save()
  94. annotation.save()
  95. url = reverse('ann', args=[project.id, annotation.document.id, annotation.id])
  96. self.assertEqual(DocumentAnnotation.objects.count(), 1)
  97. self.client.login(username=self.username, password=self.password)
  98. response = self.client.delete(url, format='json')
  99. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  100. self.assertEqual(DocumentAnnotation.objects.count(), 0)