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.

123 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. users = cls.project.users
  21. cls.user = users[0]
  22. cls.another_user = users[1]
  23. cls.category = Category(
  24. example=cls.example,
  25. label=cls.label_type,
  26. user=cls.user
  27. )
  28. def test_can_annotate_category_to_unannotated_data(self):
  29. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  30. self.assertTrue(can_annotate)
  31. class NonCollaborativeMixin:
  32. def test_cannot_annotate_same_category_to_annotated_data(self):
  33. mommy.make('Category', example=self.example, label=self.label_type, user=self.user)
  34. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  35. self.assertFalse(can_annotate)
  36. def test_allow_another_user_to_annotate_same_category(self):
  37. mommy.make(
  38. 'Category',
  39. example=self.example,
  40. label=self.label_type,
  41. user=self.another_user
  42. )
  43. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  44. self.assertTrue(can_annotate)
  45. class TestExclusiveCategoryLabeling(TestCategoryLabeling, NonCollaborativeMixin):
  46. exclusive = True
  47. collaborative = False
  48. def test_cannot_annotate_different_category_to_annotated_data(self):
  49. mommy.make('Category', example=self.example, user=self.user)
  50. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  51. self.assertFalse(can_annotate)
  52. class TestNonExclusiveCategoryLabeling(TestCategoryLabeling, NonCollaborativeMixin):
  53. exclusive = False
  54. collaborative = False
  55. def test_can_annotate_different_category_to_annotated_data(self):
  56. mommy.make('Category', example=self.example, user=self.user)
  57. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  58. self.assertTrue(can_annotate)
  59. class CollaborativeMixin:
  60. def test_deny_another_user_to_annotate_same_category(self):
  61. mommy.make(
  62. 'Category',
  63. example=self.example,
  64. label=self.label_type,
  65. user=self.another_user
  66. )
  67. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  68. self.assertFalse(can_annotate)
  69. class TestCollaborativeExclusiveCategoryLabeling(TestCategoryLabeling, CollaborativeMixin):
  70. exclusive = True
  71. collaborative = True
  72. def test_deny_another_user_to_annotate_different_category(self):
  73. mommy.make(
  74. 'Category',
  75. example=self.example,
  76. user=self.another_user
  77. )
  78. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  79. self.assertFalse(can_annotate)
  80. class TestCollaborativeNonExclusiveCategoryLabeling(TestCategoryLabeling, CollaborativeMixin):
  81. exclusive = False
  82. collaborative = True
  83. def test_allow_another_user_to_annotate_different_category(self):
  84. mommy.make(
  85. 'Category',
  86. example=self.example,
  87. user=self.another_user
  88. )
  89. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  90. self.assertTrue(can_annotate)
  91. class TestCategory(TestCase):
  92. def test_uniqueness(self):
  93. a = mommy.make('Category')
  94. with self.assertRaises(IntegrityError):
  95. Category(example=a.example, user=a.user, label=a.label).save()