chenzhaoyang
2025-12-17 d3e5a4b7658ece4f845bbc0c4f95acf3fbdf8a61
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
"""This file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
"""
import json
 
import pytest
from django.apps import apps
from tasks.models import Annotation, Prediction, Task
from tasks.serializers import AnnotationSerializer
 
 
@pytest.mark.skip(reason='HTX-868')
@pytest.mark.parametrize(
    'annotation_items, aggregated_class',
    [
        (
            [
                {
                    'id': 1,
                    'review_result': None,
                    'ground_truth': False,
                    'result': [
                        {
                            'id': '123',
                            'type': 'choices',
                            'value': {'choices': ['class_AA']},
                            'to_name': 'text',
                            'from_name': 'text_class',
                        }
                    ],
                    'created_at': 'test',
                    'updated_at': 'test',
                    'lead_time': None,
                    'completed_by': 2,
                }
            ],
            'class_AA',
        ),
        (
            [
                {
                    'id': 2,
                    'review_result': None,
                    'ground_truth': False,
                    'result': [
                        {
                            'id': '123',
                            'type': 'choices',
                            'value': {'choices': ['class_AA']},
                            'to_name': 'text',
                            'from_name': 'text_class',
                        }
                    ],
                    'created_at': '',
                    'updated_at': '',
                    'lead_time': None,
                    'completed_by': 4,
                },
                {
                    'id': 3,
                    'review_result': None,
                    'ground_truth': False,
                    'result': [
                        {
                            'id': '456',
                            'type': 'choices',
                            'value': {'choices': ['class_AA']},
                            'to_name': 'text',
                            'from_name': 'text_class',
                        }
                    ],
                    'created_at': '',
                    'updated_at': '',
                    'lead_time': None,
                    'completed_by': 4,
                },
            ],
            'class_AA',
        ),
        (
            [
                {
                    'id': 4,
                    'review_result': None,
                    'ground_truth': False,
                    'result': [
                        {
                            'id': '123',
                            'type': 'choices',
                            'value': {'choices': ['class_AA']},
                            'to_name': 'text',
                            'from_name': 'text_class',
                        }
                    ],
                    'created_at': '',
                    'updated_at': '',
                    'lead_time': None,
                    'completed_by': 6,
                },
                {
                    'id': 5,
                    'review_result': None,
                    'ground_truth': False,
                    'result': [
                        {
                            'id': '456',
                            'type': 'choices',
                            'value': {'choices': ['class_BB']},
                            'to_name': 'text',
                            'from_name': 'text_class',
                        }
                    ],
                    'created_at': '',
                    'updated_at': '',
                    'lead_time': None,
                    'completed_by': 6,
                },
                {
                    'id': 6,
                    'review_result': None,
                    'ground_truth': False,
                    'result': [
                        {
                            'id': '789',
                            'type': 'choices',
                            'value': {'choices': ['class_BB']},
                            'to_name': 'text',
                            'from_name': 'text_class',
                        }
                    ],
                    'created_at': '',
                    'updated_at': '',
                    'lead_time': None,
                    'completed_by': 6,
                },
            ],
            'class_BB',
        ),
        (
            [
                {
                    'id': 7,
                    'review_result': None,
                    'ground_truth': False,
                    'result': [
                        {
                            'id': '123',
                            'type': 'choices',
                            'value': {'choices': ['class_AA']},
                            'to_name': 'text',
                            'from_name': 'text_class',
                        }
                    ],
                    'created_at': '',
                    'updated_at': '',
                    'lead_time': None,
                    'completed_by': 10,
                }
            ],
            'class_AA',
        ),
    ],
)
@pytest.mark.parametrize(
    'finished, aggregator_type, return_task, num_task_in_result',
    [
        ('0', 'no_aggregation', '0', 2),
        ('1', 'no_aggregation', '0', 1),
        ('0', 'majority_vote', '0', 2),
        ('0', 'majority_vote', '1', 2),
        ('1', 'majority_vote', '1', 1),
    ],
)
@pytest.mark.django_db
def test_export(
    business_client,
    configured_project,
    finished,
    aggregator_type,
    return_task,
    num_task_in_result,
    annotation_items,
    aggregated_class,
):
    if aggregator_type == 'majority_vote' and not apps.is_installed('businesses'):
        pytest.skip('Not supported aggregation for open-source version')
 
    task_query = Task.objects.filter(project=configured_project.id)
    task = task_query.first()
 
    expected_annotations_for_task = set()
    for annotation in annotation_items:
        db_annotation = Annotation.objects.create(
            task=task, result=annotation['result'], completed_by=business_client.admin
        )
        db_annotation = AnnotationSerializer(db_annotation).data
        annotation['id'] = db_annotation['id']
        annotation['created_at'] = db_annotation['created_at']
        annotation['updated_at'] = db_annotation['updated_at']
        annotation['completed_by'] = business_client.admin.id
        expected_annotations_for_task.add(json.dumps(annotation))
 
    r = business_client.get(
        f'/api/projects/{configured_project.id}/results/',
        data={'finished': finished, 'aggregator_type': aggregator_type, 'return_task': return_task},
    )
    assert r.status_code == 200
    exports = r.json()
 
    # test expected number of objects returned
    assert len(exports) == num_task_in_result
 
    # test whether "id" or full task included in results
    if return_task == '0':
        task_with_annotation = next((t for t in exports if t['id'] == task.id))
        assert task_with_annotation['id'] == task.id
    elif return_task == '1':
        task_with_annotation = next((t for t in exports if t['id'] == task.id))
        assert task_with_annotation['data'] == task.data
    else:
        raise Exception('Incorrect return_task param in test: ' + str(return_task))
 
    # test how aggregation affects annotations
    if aggregator_type == 'no_aggregation':
        exported_annotations = set()
        for annotation in task_with_annotation['annotations']:
            exported_annotations.add(json.dumps(annotation))
        assert exported_annotations == expected_annotations_for_task
        if finished != '1':
            # we expect to see all tasks in exports...
            assert len(exports) == task_query.count()
            # ...as well as task without annotations (with empty results)
            assert all(len(t['annotations']) == 0 for t in exports if t['id'] != task.id)
    else:
        assert task_with_annotation['annotations'][0]['result'][0]['value']['choices'][0] == aggregated_class
 
 
@pytest.mark.skip(reason='HTX-868')
@pytest.mark.parametrize('finished', ('0', '1'))
@pytest.mark.parametrize('return_task', ('0', '1'))
@pytest.mark.parametrize('aggregator_type', ('no_aggregation', 'majority_vote'))
@pytest.mark.parametrize(
    'annotation_results, predictions',
    [
        (
            [
                [
                    {
                        'id': '123',
                        'from_name': 'text_class',
                        'to_name': 'text',
                        'type': 'choices',
                        'value': {'choices': ['class_A']},
                    }
                ]
            ],
            {
                'result': [
                    {
                        'id': '123',
                        'from_name': 'text_class',
                        'to_name': 'text',
                        'type': 'choices',
                        'value': {'choices': ['class_A']},
                    }
                ],
                'score': 0.5,
            },
        ),
        (
            [
                [
                    {
                        'id': '123',
                        'from_name': 'text_class',
                        'to_name': 'text',
                        'type': 'choices',
                        'value': {'choices': ['class_A']},
                    }
                ]
            ],
            None,
        ),
    ],
)
@pytest.mark.django_db
def test_export_with_predictions(
    business_client, configured_project, finished, return_task, aggregator_type, annotation_results, predictions
):
    if aggregator_type == 'majority_vote' and not apps.is_installed('businesses'):
        pytest.skip('Not supported aggregation for open-source version')
 
    tasks = Task.objects.filter(project=configured_project.id)
    task = tasks.first()
    for result in annotation_results:
        for r in result:
            r['completed_by'] = [business_client.admin.id]
        Annotation.objects.create(task=task, result=result, completed_by=business_client.admin)
    if predictions:
        for task in tasks:
            Prediction.objects.create(
                task=task, project=task.project, result=predictions['result'], score=predictions['score']
            )
 
    r = business_client.get(
        f'/api/projects/{configured_project.id}/results/',
        data={
            'finished': finished,
            'aggregator_type': aggregator_type,
            'return_task': return_task,
            'return_predictions': '1',
        },
    )
    assert r.status_code == 200
    exports = r.json()
    for task in exports:
        if predictions:
            assert task['predictions'][0]['result'] == predictions['result']
            assert task['predictions'][0]['score'] == predictions['score']
        else:
            assert task['predictions'] == []