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.

189 lines
6.6 KiB

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