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.

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