|
| 1 | +"""Tests for projects.models (Project model and related logic).""" |
| 2 | +from django.test import TestCase |
| 3 | +from projects.tests.factories import ProjectFactory |
| 4 | +from tasks.models import Task |
| 5 | +from tasks.tests.factories import AnnotationFactory, TaskFactory |
| 6 | +from tests.utils import mock_feature_flag |
| 7 | + |
| 8 | + |
| 9 | +class TestRearrangeOverlapCohort(TestCase): |
| 10 | + """ |
| 11 | + Tests for Project._rearrange_overlap_cohort(). |
| 12 | +
|
| 13 | + Covers overlap cohort assignment when overlap_cohort_percentage < 100: |
| 14 | + correct cohort size, deterministic vs random tie-breaking (feature flag), |
| 15 | + and prioritization of tasks with more annotations (progress preservation). |
| 16 | + """ |
| 17 | + |
| 18 | + @mock_feature_flag('fflag_feat_utc_563_randomize_overlap_cohort', True, parent_module='projects.models') |
| 19 | + def test_randomize_when_flag_on(self): |
| 20 | + """ |
| 21 | + With fflag_feat_utc_563_randomize_overlap_cohort on, cohort selection |
| 22 | + varies across runs because tie-breaking within same annotation count |
| 23 | + is random. Expected: at least 2 distinct cohort ID sets over 10 runs, |
| 24 | + and cohort size always equals must_tasks. |
| 25 | + """ |
| 26 | + num_tasks = 20 |
| 27 | + overlap_cohort_pct = 25 |
| 28 | + expected_cohort_size = int(num_tasks * overlap_cohort_pct / 100 + 0.5) # 5 |
| 29 | + project = ProjectFactory( |
| 30 | + maximum_annotations=2, |
| 31 | + overlap_cohort_percentage=overlap_cohort_pct, |
| 32 | + ) |
| 33 | + TaskFactory.create_batch(num_tasks, project=project) |
| 34 | + |
| 35 | + cohorts_seen = set() |
| 36 | + for _ in range(10): |
| 37 | + project._rearrange_overlap_cohort() |
| 38 | + cohort_ids = frozenset(Task.objects.filter(project=project, overlap__gt=1).values_list('id', flat=True)) |
| 39 | + assert len(cohort_ids) == expected_cohort_size |
| 40 | + cohorts_seen.add(cohort_ids) |
| 41 | + assert len(cohorts_seen) >= 2, 'Random tie-breaking should produce at least 2 different cohorts over 10 runs' |
| 42 | + |
| 43 | + @mock_feature_flag('fflag_feat_utc_563_randomize_overlap_cohort', False, parent_module='projects.models') |
| 44 | + def test_deterministic_when_flag_off(self): |
| 45 | + """ |
| 46 | + With fflag_feat_utc_563_randomize_overlap_cohort off, cohort selection |
| 47 | + is deterministic. Expected: two consecutive runs yield the same cohort |
| 48 | + ID set and correct cohort size. |
| 49 | + """ |
| 50 | + num_tasks = 20 |
| 51 | + overlap_cohort_pct = 25 |
| 52 | + expected_cohort_size = int(num_tasks * overlap_cohort_pct / 100 + 0.5) |
| 53 | + project = ProjectFactory( |
| 54 | + maximum_annotations=2, |
| 55 | + overlap_cohort_percentage=overlap_cohort_pct, |
| 56 | + ) |
| 57 | + TaskFactory.create_batch(num_tasks, project=project) |
| 58 | + |
| 59 | + project._rearrange_overlap_cohort() |
| 60 | + cohort_first = frozenset(Task.objects.filter(project=project, overlap__gt=1).values_list('id', flat=True)) |
| 61 | + project._rearrange_overlap_cohort() |
| 62 | + cohort_second = frozenset(Task.objects.filter(project=project, overlap__gt=1).values_list('id', flat=True)) |
| 63 | + assert len(cohort_first) == expected_cohort_size |
| 64 | + assert cohort_first == cohort_second |
| 65 | + |
| 66 | + @mock_feature_flag('fflag_feat_utc_563_randomize_overlap_cohort', True, parent_module='projects.models') |
| 67 | + def test_preserves_progress_when_flag_on(self): |
| 68 | + """ |
| 69 | + Tasks with more finished annotations are prioritized into the cohort |
| 70 | + (progress preserved). With flag on, only tie-breaking is random. |
| 71 | + Expected: tasks that already have one annotation are in the cohort. |
| 72 | + """ |
| 73 | + num_tasks = 10 |
| 74 | + overlap_cohort_pct = 30 |
| 75 | + expected_cohort_size = int(num_tasks * overlap_cohort_pct / 100 + 0.5) # 3 |
| 76 | + project = ProjectFactory( |
| 77 | + maximum_annotations=2, |
| 78 | + overlap_cohort_percentage=overlap_cohort_pct, |
| 79 | + ) |
| 80 | + tasks = TaskFactory.create_batch(num_tasks, project=project) |
| 81 | + for t in tasks[:2]: |
| 82 | + AnnotationFactory( |
| 83 | + task=t, |
| 84 | + project=project, |
| 85 | + result=[ |
| 86 | + { |
| 87 | + 'value': {'choices': ['A']}, |
| 88 | + 'from_name': 'text_class', |
| 89 | + 'to_name': 'text', |
| 90 | + 'type': 'choices', |
| 91 | + } |
| 92 | + ], |
| 93 | + was_cancelled=False, |
| 94 | + ground_truth=False, |
| 95 | + ) |
| 96 | + |
| 97 | + project._rearrange_overlap_cohort() |
| 98 | + |
| 99 | + cohort_ids = set(Task.objects.filter(project=project, overlap__gt=1).values_list('id', flat=True)) |
| 100 | + assert len(cohort_ids) == expected_cohort_size |
| 101 | + assert tasks[0].id in cohort_ids |
| 102 | + assert tasks[1].id in cohort_ids |
0 commit comments