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.

92 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. users = cls.project.users
  18. cls.user = users[0]
  19. cls.another_user = users[1]
  20. cls.text_label = TextLabel(
  21. example=cls.example,
  22. user=cls.user,
  23. text='foo'
  24. )
  25. def test_can_annotate_category_to_unannotated_data(self):
  26. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  27. self.assertTrue(can_annotate)
  28. def test_uniqueness(self):
  29. a = mommy.make('TextLabel')
  30. with self.assertRaises(IntegrityError):
  31. TextLabel(example=a.example,
  32. user=a.user,
  33. text=a.text).save()
  34. class TestNonCollaborativeTextLabeling(TestTextLabeling):
  35. collaborative = False
  36. def test_cannot_annotate_same_text_to_annotated_data(self):
  37. mommy.make(
  38. 'TextLabel',
  39. example=self.example,
  40. user=self.user,
  41. text=self.text_label.text
  42. )
  43. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  44. self.assertFalse(can_annotate)
  45. def test_can_annotate_different_text_to_annotated_data(self):
  46. mommy.make('TextLabel', example=self.example, user=self.user)
  47. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  48. self.assertTrue(can_annotate)
  49. def test_allow_another_user_to_annotate_same_text(self):
  50. mommy.make(
  51. 'TextLabel',
  52. example=self.example,
  53. user=self.another_user,
  54. text=self.text_label.text
  55. )
  56. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  57. self.assertTrue(can_annotate)
  58. class TestCollaborativeTextLabeling(TestTextLabeling):
  59. collaborative = True
  60. def test_deny_another_user_to_annotate_same_text(self):
  61. mommy.make(
  62. 'TextLabel',
  63. example=self.example,
  64. user=self.another_user,
  65. text=self.text_label.text
  66. )
  67. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  68. self.assertFalse(can_annotate)
  69. def test_allow_another_user_to_annotate_different_text(self):
  70. mommy.make(
  71. 'TextLabel',
  72. example=self.example,
  73. user=self.another_user
  74. )
  75. can_annotate = TextLabel.objects.can_annotate(self.text_label, self.project.item)
  76. self.assertTrue(can_annotate)