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.

114 lines
3.8 KiB

  1. import abc
  2. from django.test import TestCase
  3. from model_mommy import mommy
  4. from api.models import DOCUMENT_CLASSIFICATION, Category
  5. from .api.utils import prepare_project
  6. class TestCategoryAnnotation(abc.ABC, TestCase):
  7. exclusive = True
  8. collaborative = False
  9. @classmethod
  10. def setUpTestData(cls):
  11. cls.project = prepare_project(
  12. DOCUMENT_CLASSIFICATION,
  13. single_class_classification=cls.exclusive,
  14. collaborative_annotation=cls.collaborative
  15. )
  16. cls.example = mommy.make('Example', project=cls.project.item)
  17. cls.label_type = mommy.make('CategoryType', project=cls.project.item)
  18. users = cls.project.users
  19. cls.user = users[0]
  20. cls.another_user = users[1]
  21. cls.category = Category(
  22. example=cls.example,
  23. label=cls.label_type,
  24. user=cls.user
  25. )
  26. def test_can_annotate_category_to_unannotated_data(self):
  27. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  28. self.assertTrue(can_annotate)
  29. class NonCollaborativeMixin:
  30. def test_cannot_annotate_same_category_to_annotated_data(self):
  31. mommy.make('Category', example=self.example, label=self.label_type, user=self.user)
  32. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  33. self.assertFalse(can_annotate)
  34. def test_allow_another_user_to_annotate_same_category(self):
  35. mommy.make(
  36. 'Category',
  37. example=self.example,
  38. label=self.label_type,
  39. user=self.another_user
  40. )
  41. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  42. self.assertTrue(can_annotate)
  43. class TestExclusiveCategoryAnnotation(TestCategoryAnnotation, NonCollaborativeMixin):
  44. exclusive = True
  45. collaborative = False
  46. def test_cannot_annotate_different_category_to_annotated_data(self):
  47. mommy.make('Category', example=self.example, user=self.user)
  48. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  49. self.assertFalse(can_annotate)
  50. class TestNonExclusiveCategoryAnnotation(TestCategoryAnnotation, NonCollaborativeMixin):
  51. exclusive = False
  52. collaborative = False
  53. def test_can_annotate_different_category_to_annotated_data(self):
  54. mommy.make('Category', example=self.example, user=self.user)
  55. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  56. self.assertTrue(can_annotate)
  57. class CollaborativeMixin:
  58. def test_deny_another_user_to_annotate_same_category(self):
  59. mommy.make(
  60. 'Category',
  61. example=self.example,
  62. label=self.label_type,
  63. user=self.another_user
  64. )
  65. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  66. self.assertFalse(can_annotate)
  67. class TestCollaborativeExclusiveCategoryAnnotation(TestCategoryAnnotation, CollaborativeMixin):
  68. exclusive = True
  69. collaborative = True
  70. def test_deny_another_user_to_annotate_different_category(self):
  71. mommy.make(
  72. 'Category',
  73. example=self.example,
  74. user=self.another_user
  75. )
  76. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  77. self.assertFalse(can_annotate)
  78. class TestCollaborativeNonExclusiveCategoryAnnotation(TestCategoryAnnotation, CollaborativeMixin):
  79. exclusive = False
  80. collaborative = True
  81. def test_allow_another_user_to_annotate_different_category(self):
  82. mommy.make(
  83. 'Category',
  84. example=self.example,
  85. user=self.another_user
  86. )
  87. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  88. self.assertTrue(can_annotate)