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.

97 lines
3.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. import abc
  2. from django.db import IntegrityError
  3. from django.test import TestCase
  4. from model_mommy import mommy
  5. from labels.models import Category
  6. from projects.models import DOCUMENT_CLASSIFICATION
  7. from projects.tests.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(example=cls.example, label=cls.label_type, user=cls.user)
  23. def test_can_annotate_category_to_unannotated_data(self):
  24. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  25. self.assertTrue(can_annotate)
  26. class NonCollaborativeMixin:
  27. def test_cannot_annotate_same_category_to_annotated_data(self):
  28. mommy.make("Category", example=self.example, label=self.label_type, user=self.user)
  29. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  30. self.assertFalse(can_annotate)
  31. def test_allow_another_user_to_annotate_same_category(self):
  32. mommy.make("Category", example=self.example, label=self.label_type, user=self.another_user)
  33. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  34. self.assertTrue(can_annotate)
  35. class TestExclusiveCategoryLabeling(TestCategoryLabeling, NonCollaborativeMixin):
  36. exclusive = True
  37. collaborative = False
  38. def test_cannot_annotate_different_category_to_annotated_data(self):
  39. mommy.make("Category", example=self.example, user=self.user)
  40. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  41. self.assertFalse(can_annotate)
  42. class TestNonExclusiveCategoryLabeling(TestCategoryLabeling, NonCollaborativeMixin):
  43. exclusive = False
  44. collaborative = False
  45. def test_can_annotate_different_category_to_annotated_data(self):
  46. mommy.make("Category", example=self.example, user=self.user)
  47. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  48. self.assertTrue(can_annotate)
  49. class CollaborativeMixin:
  50. def test_deny_another_user_to_annotate_same_category(self):
  51. mommy.make("Category", example=self.example, label=self.label_type, user=self.another_user)
  52. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  53. self.assertFalse(can_annotate)
  54. class TestCollaborativeExclusiveCategoryLabeling(TestCategoryLabeling, CollaborativeMixin):
  55. exclusive = True
  56. collaborative = True
  57. def test_deny_another_user_to_annotate_different_category(self):
  58. mommy.make("Category", example=self.example, user=self.another_user)
  59. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  60. self.assertFalse(can_annotate)
  61. class TestCollaborativeNonExclusiveCategoryLabeling(TestCategoryLabeling, CollaborativeMixin):
  62. exclusive = False
  63. collaborative = True
  64. def test_allow_another_user_to_annotate_different_category(self):
  65. mommy.make("Category", example=self.example, user=self.another_user)
  66. can_annotate = Category.objects.can_annotate(self.category, self.project.item)
  67. self.assertTrue(can_annotate)
  68. class TestCategory(TestCase):
  69. def test_uniqueness(self):
  70. a = mommy.make("Category")
  71. with self.assertRaises(IntegrityError):
  72. Category(example=a.example, user=a.user, label=a.label).save()