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.

91 lines
2.9 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 SEQ2SEQ
  6. from labels.models import TextLabel
  7. from api.tests.api.utils import prepare_project
  8. class TestTextLabeling(abc.ABC, TestCase):
  9. collaborative = False
  10. @classmethod
  11. def setUpTestData(cls):
  12. cls.project = prepare_project(
  13. SEQ2SEQ,
  14. collaborative_annotation=cls.collaborative
  15. )
  16. cls.example = mommy.make('Example', project=cls.project.item)
  17. cls.user = cls.project.admin
  18. cls.another_user = cls.project.approver
  19. cls.text_label = TextLabel(
  20. example=cls.example,
  21. user=cls.user,
  22. text='foo'
  23. )
  24. def test_can_annotate_category_to_unannotated_data(self):
  25. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  26. self.assertTrue(can_annotate)
  27. def test_uniqueness(self):
  28. a = mommy.make('TextLabel')
  29. with self.assertRaises(IntegrityError):
  30. TextLabel(example=a.example,
  31. user=a.user,
  32. text=a.text).save()
  33. class TestNonCollaborativeTextLabeling(TestTextLabeling):
  34. collaborative = False
  35. def test_cannot_annotate_same_text_to_annotated_data(self):
  36. mommy.make(
  37. 'TextLabel',
  38. example=self.example,
  39. user=self.user,
  40. text=self.text_label.text
  41. )
  42. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  43. self.assertFalse(can_annotate)
  44. def test_can_annotate_different_text_to_annotated_data(self):
  45. mommy.make('TextLabel', example=self.example, user=self.user)
  46. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  47. self.assertTrue(can_annotate)
  48. def test_allow_another_user_to_annotate_same_text(self):
  49. mommy.make(
  50. 'TextLabel',
  51. example=self.example,
  52. user=self.another_user,
  53. text=self.text_label.text
  54. )
  55. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  56. self.assertTrue(can_annotate)
  57. class TestCollaborativeTextLabeling(TestTextLabeling):
  58. collaborative = True
  59. def test_deny_another_user_to_annotate_same_text(self):
  60. mommy.make(
  61. 'TextLabel',
  62. example=self.example,
  63. user=self.another_user,
  64. text=self.text_label.text
  65. )
  66. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  67. self.assertFalse(can_annotate)
  68. def test_allow_another_user_to_annotate_different_text(self):
  69. mommy.make(
  70. 'TextLabel',
  71. example=self.example,
  72. user=self.another_user
  73. )
  74. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  75. self.assertTrue(can_annotate)