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.

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