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.

173 lines
5.8 KiB

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
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 mixer.backend.django import mixer
  5. from ..models import Label, DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation
  6. from ..serializers import ClassificationDocumentSerializer, DocumentAnnotationSerializer
  7. from ..serializers import SequenceDocumentSerializer, SequenceAnnotationSerializer
  8. from ..serializers import Seq2seqDocumentSerializer, Seq2seqAnnotationSerializer
  9. class TestTextClassificationProject(TestCase):
  10. @classmethod
  11. def setUpTestData(cls):
  12. cls.project = mixer.blend('server.TextClassificationProject')
  13. def test_image(self):
  14. image_url = self.project.image
  15. self.assertTrue(image_url.endswith('.jpg'))
  16. def test_get_template_name(self):
  17. template = self.project.get_template_name()
  18. self.assertEqual(template, 'annotation/document_classification.html')
  19. def test_get_document_serializer(self):
  20. serializer = self.project.get_document_serializer()
  21. self.assertEqual(serializer, ClassificationDocumentSerializer)
  22. def test_get_annotation_serializer(self):
  23. serializer = self.project.get_annotation_serializer()
  24. self.assertEqual(serializer, DocumentAnnotationSerializer)
  25. def test_get_annotation_class(self):
  26. klass = self.project.get_annotation_class()
  27. self.assertEqual(klass, DocumentAnnotation)
  28. class TestSequenceLabelingProject(TestCase):
  29. @classmethod
  30. def setUpTestData(cls):
  31. cls.project = mixer.blend('server.SequenceLabelingProject')
  32. def test_image(self):
  33. image_url = self.project.image
  34. self.assertTrue(image_url.endswith('.jpg'))
  35. def test_get_template_name(self):
  36. template = self.project.get_template_name()
  37. self.assertEqual(template, 'annotation/sequence_labeling.html')
  38. def test_get_document_serializer(self):
  39. serializer = self.project.get_document_serializer()
  40. self.assertEqual(serializer, SequenceDocumentSerializer)
  41. def test_get_annotation_serializer(self):
  42. serializer = self.project.get_annotation_serializer()
  43. self.assertEqual(serializer, SequenceAnnotationSerializer)
  44. def test_get_annotation_class(self):
  45. klass = self.project.get_annotation_class()
  46. self.assertEqual(klass, SequenceAnnotation)
  47. class TestSeq2seqProject(TestCase):
  48. @classmethod
  49. def setUpTestData(cls):
  50. cls.project = mixer.blend('server.Seq2seqProject')
  51. def test_image(self):
  52. image_url = self.project.image
  53. self.assertTrue(image_url.endswith('.jpg'))
  54. def test_get_template_name(self):
  55. template = self.project.get_template_name()
  56. self.assertEqual(template, 'annotation/seq2seq.html')
  57. def test_get_document_serializer(self):
  58. serializer = self.project.get_document_serializer()
  59. self.assertEqual(serializer, Seq2seqDocumentSerializer)
  60. def test_get_annotation_serializer(self):
  61. serializer = self.project.get_annotation_serializer()
  62. self.assertEqual(serializer, Seq2seqAnnotationSerializer)
  63. def test_get_annotation_class(self):
  64. klass = self.project.get_annotation_class()
  65. self.assertEqual(klass, Seq2seqAnnotation)
  66. class TestLabel(TestCase):
  67. def test_shortcut_uniqueness(self):
  68. label = mixer.blend('server.Label', shortcut='a')
  69. mixer.blend('server.Label', shortcut=label.shortcut)
  70. with self.assertRaises(IntegrityError):
  71. Label(project=label.project, shortcut=label.shortcut).save()
  72. def test_create_none_shortcut(self):
  73. label = mixer.blend('server.Label', shortcut=None)
  74. self.assertEqual(label.shortcut, None)
  75. def test_text_uniqueness(self):
  76. label = mixer.blend('server.Label')
  77. mixer.blend('server.Label', text=label.text)
  78. with self.assertRaises(IntegrityError):
  79. Label(project=label.project, text=label.text).save()
  80. class TestTextClassificationDocument(TestCase):
  81. @classmethod
  82. def setUpTestData(cls):
  83. cls.doc = mixer.blend('server.TextClassificationDocument')
  84. def test_get_annotations(self):
  85. self.assertEqual(self.doc.get_annotations().count(), 0)
  86. class TestSequenceLabelingDocument(TestCase):
  87. @classmethod
  88. def setUpTestData(cls):
  89. cls.doc = mixer.blend('server.SequenceLabelingDocument')
  90. def test_get_annotations(self):
  91. self.assertEqual(self.doc.get_annotations().count(), 0)
  92. class TestSeq2seqDocument(TestCase):
  93. @classmethod
  94. def setUpTestData(cls):
  95. cls.doc = mixer.blend('server.Seq2seqDocument')
  96. def test_get_annotations(self):
  97. self.assertEqual(self.doc.get_annotations().count(), 0)
  98. class TestDocumentAnnotation(TestCase):
  99. def test_uniqueness(self):
  100. a = mixer.blend('server.DocumentAnnotation')
  101. with self.assertRaises(IntegrityError):
  102. DocumentAnnotation(document=a.document, user=a.user, label=a.label).save()
  103. class TestSequenceAnnotation(TestCase):
  104. def test_uniqueness(self):
  105. a = mixer.blend('server.SequenceAnnotation')
  106. with self.assertRaises(IntegrityError):
  107. SequenceAnnotation(document=a.document,
  108. user=a.user,
  109. label=a.label,
  110. start_offset=a.start_offset,
  111. end_offset=a.end_offset).save()
  112. def test_position_constraint(self):
  113. with self.assertRaises(ValidationError):
  114. mixer.blend('server.SequenceAnnotation',
  115. start_offset=1, end_offset=0).clean()
  116. class TestSeq2seqAnnotation(TestCase):
  117. def test_uniqueness(self):
  118. a = mixer.blend('server.Seq2seqAnnotation')
  119. with self.assertRaises(IntegrityError):
  120. Seq2seqAnnotation(document=a.document,
  121. user=a.user,
  122. text=a.text).save()