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.

161 lines
5.5 KiB

6 years ago
5 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. from django.test import TestCase
  2. from django.core.exceptions import ValidationError
  3. from django.db.utils import IntegrityError
  4. from model_mommy import mommy
  5. from ..models import Label, DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation
  6. from ..serializers import DocumentAnnotationSerializer
  7. from ..serializers import SequenceAnnotationSerializer
  8. from ..serializers import Seq2seqAnnotationSerializer
  9. class TestTextClassificationProject(TestCase):
  10. @classmethod
  11. def setUpTestData(cls):
  12. cls.project = mommy.make('server.TextClassificationProject')
  13. def test_image(self):
  14. image_url = self.project.image
  15. self.assertTrue(image_url.endswith('.jpg'))
  16. def test_get_bundle_name(self):
  17. template = self.project.get_bundle_name()
  18. self.assertEqual(template, 'document_classification')
  19. def test_get_annotation_serializer(self):
  20. serializer = self.project.get_annotation_serializer()
  21. self.assertEqual(serializer, DocumentAnnotationSerializer)
  22. def test_get_annotation_class(self):
  23. klass = self.project.get_annotation_class()
  24. self.assertEqual(klass, DocumentAnnotation)
  25. class TestSequenceLabelingProject(TestCase):
  26. @classmethod
  27. def setUpTestData(cls):
  28. cls.project = mommy.make('server.SequenceLabelingProject')
  29. def test_image(self):
  30. image_url = self.project.image
  31. self.assertTrue(image_url.endswith('.jpg'))
  32. def test_get_bundle_name(self):
  33. template = self.project.get_bundle_name()
  34. self.assertEqual(template, 'sequence_labeling')
  35. def test_get_annotation_serializer(self):
  36. serializer = self.project.get_annotation_serializer()
  37. self.assertEqual(serializer, SequenceAnnotationSerializer)
  38. def test_get_annotation_class(self):
  39. klass = self.project.get_annotation_class()
  40. self.assertEqual(klass, SequenceAnnotation)
  41. class TestSeq2seqProject(TestCase):
  42. @classmethod
  43. def setUpTestData(cls):
  44. cls.project = mommy.make('server.Seq2seqProject')
  45. def test_image(self):
  46. image_url = self.project.image
  47. self.assertTrue(image_url.endswith('.jpg'))
  48. def test_get_bundle_name(self):
  49. template = self.project.get_bundle_name()
  50. self.assertEqual(template, 'seq2seq')
  51. def test_get_annotation_serializer(self):
  52. serializer = self.project.get_annotation_serializer()
  53. self.assertEqual(serializer, Seq2seqAnnotationSerializer)
  54. def test_get_annotation_class(self):
  55. klass = self.project.get_annotation_class()
  56. self.assertEqual(klass, Seq2seqAnnotation)
  57. class TestLabel(TestCase):
  58. def test_text_uniqueness(self):
  59. label = mommy.make('server.Label')
  60. mommy.make('server.Label', text=label.text)
  61. with self.assertRaises(IntegrityError):
  62. Label(project=label.project, text=label.text).save()
  63. def test_keys_uniqueness(self):
  64. label = mommy.make('server.Label', prefix_key='ctrl', suffix_key='a')
  65. with self.assertRaises(IntegrityError):
  66. Label(project=label.project,
  67. text='example',
  68. prefix_key=label.prefix_key,
  69. suffix_key=label.suffix_key).save()
  70. def test_suffix_key_uniqueness(self):
  71. label = mommy.make('server.Label', prefix_key=None, suffix_key='a')
  72. with self.assertRaises(ValidationError):
  73. Label(project=label.project,
  74. text='example',
  75. prefix_key=label.prefix_key,
  76. suffix_key=label.suffix_key).full_clean()
  77. def test_cannot_add_label_only_prefix_key(self):
  78. project = mommy.make('server.Project')
  79. label = Label(project=project,
  80. text='example',
  81. prefix_key='ctrl')
  82. with self.assertRaises(ValidationError):
  83. label.clean()
  84. def test_can_add_label_only_suffix_key(self):
  85. project = mommy.make('server.Project')
  86. label = Label(project=project,
  87. text='example',
  88. suffix_key='a')
  89. label.full_clean()
  90. def test_can_add_label_suffix_key_with_prefix_key(self):
  91. project = mommy.make('server.Project')
  92. label = Label(project=project,
  93. text='example',
  94. prefix_key='ctrl',
  95. suffix_key='a')
  96. label.full_clean()
  97. class TestDocumentAnnotation(TestCase):
  98. def test_uniqueness(self):
  99. a = mommy.make('server.DocumentAnnotation')
  100. with self.assertRaises(IntegrityError):
  101. DocumentAnnotation(document=a.document, user=a.user, label=a.label).save()
  102. class TestSequenceAnnotation(TestCase):
  103. def test_uniqueness(self):
  104. a = mommy.make('server.SequenceAnnotation')
  105. with self.assertRaises(IntegrityError):
  106. SequenceAnnotation(document=a.document,
  107. user=a.user,
  108. label=a.label,
  109. start_offset=a.start_offset,
  110. end_offset=a.end_offset).save()
  111. def test_position_constraint(self):
  112. with self.assertRaises(ValidationError):
  113. mommy.make('server.SequenceAnnotation',
  114. start_offset=1, end_offset=0).clean()
  115. class TestSeq2seqAnnotation(TestCase):
  116. def test_uniqueness(self):
  117. a = mommy.make('server.Seq2seqAnnotation')
  118. with self.assertRaises(IntegrityError):
  119. Seq2seqAnnotation(document=a.document,
  120. user=a.user,
  121. text=a.text).save()