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.

134 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
5 years ago
  1. from django.core.exceptions import ValidationError
  2. from django.db.utils import IntegrityError
  3. from django.test import TestCase
  4. from model_mommy import mommy
  5. from api.models import (IMAGE_CLASSIFICATION, SEQUENCE_LABELING, CategoryType,
  6. ExampleState, generate_random_hex_color)
  7. from .api.utils import prepare_project
  8. class TestLabel(TestCase):
  9. def test_deny_creating_same_text(self):
  10. label = mommy.make('CategoryType')
  11. with self.assertRaises(IntegrityError):
  12. mommy.make('CategoryType', project=label.project, text=label.text)
  13. def test_keys_uniqueness(self):
  14. label = mommy.make('CategoryType', prefix_key='ctrl', suffix_key='a')
  15. with self.assertRaises(ValidationError):
  16. CategoryType(project=label.project,
  17. text='example',
  18. prefix_key=label.prefix_key,
  19. suffix_key=label.suffix_key).full_clean()
  20. def test_suffix_key_uniqueness(self):
  21. label = mommy.make('CategoryType', prefix_key=None, suffix_key='a')
  22. with self.assertRaises(ValidationError):
  23. CategoryType(project=label.project,
  24. text='example',
  25. prefix_key=label.prefix_key,
  26. suffix_key=label.suffix_key).full_clean()
  27. def test_cannot_add_label_only_prefix_key(self):
  28. project = mommy.make('Project')
  29. label = CategoryType(project=project,
  30. text='example',
  31. prefix_key='ctrl')
  32. with self.assertRaises(ValidationError):
  33. label.clean()
  34. def test_can_add_label_only_suffix_key(self):
  35. project = mommy.make('Project')
  36. label = CategoryType(project=project, text='example', suffix_key='a')
  37. try:
  38. label.full_clean()
  39. except ValidationError:
  40. self.fail(msg=ValidationError)
  41. def test_can_add_label_suffix_key_with_prefix_key(self):
  42. project = mommy.make('Project')
  43. label = CategoryType(project=project,
  44. text='example',
  45. prefix_key='ctrl',
  46. suffix_key='a')
  47. try:
  48. label.full_clean()
  49. except ValidationError:
  50. self.fail(msg=ValidationError)
  51. class TestGeneratedColor(TestCase):
  52. def test_length(self):
  53. for i in range(100):
  54. color = generate_random_hex_color()
  55. self.assertEqual(len(color), 7)
  56. class TestExampleState(TestCase):
  57. def setUp(self):
  58. self.project = prepare_project(SEQUENCE_LABELING)
  59. self.example = mommy.make('Example', project=self.project.item)
  60. self.other = mommy.make('Example', project=self.project.item)
  61. self.examples = self.project.item.examples.all()
  62. def test_initial_done(self):
  63. done = ExampleState.objects.count_done(self.examples)
  64. self.assertEqual(done, 0)
  65. def test_done_confirmed_by_user(self):
  66. mommy.make('ExampleState', example=self.example, confirmed_by=self.project.users[0])
  67. done = ExampleState.objects.count_done(self.examples)
  68. self.assertEqual(done, 1)
  69. def test_done_confirmed_by_multiple_user(self):
  70. mommy.make('ExampleState', example=self.example, confirmed_by=self.project.users[0])
  71. mommy.make('ExampleState', example=self.example, confirmed_by=self.project.users[1])
  72. done = ExampleState.objects.count_done(self.examples)
  73. self.assertEqual(done, 1)
  74. def test_done_confirmed_by_different_example(self):
  75. mommy.make('ExampleState', example=self.example, confirmed_by=self.project.users[0])
  76. mommy.make('ExampleState', example=self.other, confirmed_by=self.project.users[1])
  77. done = ExampleState.objects.count_done(self.examples, self.project.users[0])
  78. self.assertEqual(done, 1)
  79. def test_initial_user(self):
  80. progress = ExampleState.objects.measure_member_progress(self.examples, self.project.users)
  81. expected_progress = [{'user': user.username, 'done': 0} for user in self.project.users]
  82. self.assertEqual(progress, {'total': 2, 'progress': expected_progress})
  83. def test_user_count_after_confirmation(self):
  84. mommy.make('ExampleState', example=self.example, confirmed_by=self.project.users[0])
  85. progress = ExampleState.objects.measure_member_progress(self.examples, self.project.users)
  86. expected_progress = [{'user': user.username, 'done': 0} for user in self.project.users]
  87. expected_progress[0]['done'] = 1
  88. self.assertEqual(progress, {'total': 2, 'progress': expected_progress})
  89. def test_user_count_after_multiple_user_confirmation(self):
  90. user1 = self.project.users[0]
  91. user2 = self.project.users[1]
  92. mommy.make('ExampleState', example=self.example, confirmed_by=user1)
  93. mommy.make('ExampleState', example=self.example, confirmed_by=user2)
  94. progress = ExampleState.objects.measure_member_progress(self.examples, self.project.users)
  95. expected_progress = [{'user': user.username, 'done': 0} for user in self.project.users]
  96. expected_progress[0]['done'] = 1
  97. expected_progress[1]['done'] = 1
  98. self.assertEqual(progress, {'total': 2, 'progress': expected_progress})
  99. class TestExample(TestCase):
  100. def test_text_project_returns_text_as_data_property(self):
  101. project = prepare_project(SEQUENCE_LABELING)
  102. example = mommy.make('Example', project=project.item)
  103. self.assertEqual(example.text, example.data)
  104. def test_image_project_returns_filename_as_data_property(self):
  105. project = prepare_project(IMAGE_CLASSIFICATION)
  106. example = mommy.make('Example', project=project.item)
  107. self.assertEqual(str(example.filename), example.data)