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.

122 lines
4.0 KiB

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