1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Tue Oct 9 14:51:13 2018 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from pytest import raises |
10
|
|
|
from collections import Counter |
11
|
|
|
from unittest.mock import patch, Mock |
12
|
|
|
from datetime import timedelta |
13
|
|
|
from pyUSIrest.exceptions import USIConnectionError |
14
|
|
|
|
15
|
|
|
from celery.exceptions import Retry |
16
|
|
|
|
17
|
|
|
from django.test import TestCase |
18
|
|
|
from django.core import mail |
19
|
|
|
from django.utils import timezone |
20
|
|
|
|
21
|
|
|
from common.constants import ( |
22
|
|
|
LOADED, ERROR, READY, NEED_REVISION, SUBMITTED, COMPLETED, STATUSES) |
23
|
|
|
from common.tests import WebSocketMixin |
24
|
|
|
from uid.models import Submission, Animal, Sample |
25
|
|
|
|
26
|
|
|
from ..tasks.retrieval import ( |
27
|
|
|
FetchStatusTask, FetchStatusHelper, RetrievalCompleteTask) |
28
|
|
|
from ..models import ManagedTeam, Submission as USISubmission |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
class FetchMixin(): |
32
|
|
|
"""Mixin for fetching status""" |
33
|
|
|
|
34
|
|
|
fixtures = [ |
35
|
|
|
'biosample/account', |
36
|
|
|
'biosample/managedteam', |
37
|
|
|
'biosample/submission', |
38
|
|
|
'biosample/submissiondata', |
39
|
|
|
'uid/animal', |
40
|
|
|
'uid/dictbreed', |
41
|
|
|
'uid/dictcountry', |
42
|
|
|
'uid/dictrole', |
43
|
|
|
'uid/dictsex', |
44
|
|
|
'uid/dictspecie', |
45
|
|
|
'uid/dictstage', |
46
|
|
|
'uid/dictuberon', |
47
|
|
|
'uid/organization', |
48
|
|
|
'uid/publication', |
49
|
|
|
'uid/sample', |
50
|
|
|
'uid/submission', |
51
|
|
|
'uid/user' |
52
|
|
|
] |
53
|
|
|
|
54
|
|
|
@classmethod |
55
|
|
|
def setUpClass(cls): |
56
|
|
|
# calling my base class setup |
57
|
|
|
super().setUpClass() |
58
|
|
|
|
59
|
|
|
unmanaged = ManagedTeam.objects.get(pk=2) |
60
|
|
|
unmanaged.delete() |
61
|
|
|
|
62
|
|
|
# starting mocked objects |
63
|
|
|
cls.mock_root_patcher = patch('pyUSIrest.usi.Root') |
64
|
|
|
cls.mock_root = cls.mock_root_patcher.start() |
65
|
|
|
|
66
|
|
|
cls.mock_auth_patcher = patch('pyUSIrest.auth.Auth') |
67
|
|
|
cls.mock_auth = cls.mock_auth_patcher.start() |
68
|
|
|
|
69
|
|
|
@classmethod |
70
|
|
|
def tearDownClass(cls): |
71
|
|
|
cls.mock_root_patcher.stop() |
72
|
|
|
cls.mock_auth_patcher.stop() |
73
|
|
|
|
74
|
|
|
# calling base method |
75
|
|
|
super().tearDownClass() |
76
|
|
|
|
77
|
|
|
def setUp(self): |
78
|
|
|
# calling my base setup |
79
|
|
|
super().setUp() |
80
|
|
|
|
81
|
|
|
# get a submission object |
82
|
|
|
self.submission_obj = Submission.objects.get(pk=1) |
83
|
|
|
|
84
|
|
|
# set a status which I can fetch_status |
85
|
|
|
self.submission_obj.status = SUBMITTED |
86
|
|
|
self.submission_obj.message = "Waiting for biosample validation" |
87
|
|
|
self.submission_obj.save() |
88
|
|
|
|
89
|
|
|
# set status for objects, like submittask does. |
90
|
|
|
self.animal_qs = Animal.objects.all() |
91
|
|
|
self.animal_qs.update(status=SUBMITTED) |
92
|
|
|
|
93
|
|
|
self.sample_qs = Sample.objects.all() |
94
|
|
|
self.sample_qs.update(status=SUBMITTED) |
95
|
|
|
|
96
|
|
|
# count number of names in UID for such submission (exclude |
97
|
|
|
# unknown animals) |
98
|
|
|
self.n_to_submit = self.animal_qs.count() + self.sample_qs.count() |
99
|
|
|
|
100
|
|
|
# track submission ID |
101
|
|
|
self.submission_obj_id = self.submission_obj.id |
102
|
|
|
|
103
|
|
|
# start root object |
104
|
|
|
self.my_root = self.mock_root.return_value |
105
|
|
|
|
106
|
|
|
def count_by_status(self, status): |
107
|
|
|
"""Return the number of sample and animal by status""" |
108
|
|
|
|
109
|
|
|
return ( |
110
|
|
|
Animal.objects.filter(status=status).count() + |
111
|
|
|
Sample.objects.filter(status=status).count() |
112
|
|
|
) |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
class FetchStatusHelperMixin(FetchMixin): |
116
|
|
|
"""Test class for FetchStatusHelper""" |
117
|
|
|
|
118
|
|
|
def setUp(self): |
119
|
|
|
# calling my base setup |
120
|
|
|
super().setUp() |
121
|
|
|
|
122
|
|
|
# define a biosample submission object |
123
|
|
|
self.my_submission = Mock() |
124
|
|
|
self.my_submission.name = "test-fetch" |
125
|
|
|
|
126
|
|
|
# passing submission to Mocked Root |
127
|
|
|
self.my_root.get_submission_by_name.return_value = self.my_submission |
128
|
|
|
|
129
|
|
|
# get a biosample.model.Submission and update object |
130
|
|
|
self.usi_submission = USISubmission.objects.get(pk=1) |
131
|
|
|
self.usi_submission.usi_submission_name = self.my_submission.name |
132
|
|
|
self.usi_submission.status = SUBMITTED |
133
|
|
|
self.usi_submission.save() |
134
|
|
|
|
135
|
|
|
# ok setup the object |
136
|
|
|
self.status_helper = FetchStatusHelper(self.usi_submission) |
137
|
|
|
|
138
|
|
|
# track names |
139
|
|
|
self.animal_name = Animal.objects.get(pk=1) |
140
|
|
|
self.sample_name = Sample.objects.get(pk=1) |
141
|
|
|
|
142
|
|
|
def common_tests(self): |
143
|
|
|
"""Assert stuff for each test""" |
144
|
|
|
|
145
|
|
|
# call stuff |
146
|
|
|
self.status_helper.check_submission_status() |
147
|
|
|
|
148
|
|
|
# UID submission status remain the same |
149
|
|
|
self.submission_obj.refresh_from_db() |
150
|
|
|
self.assertEqual(self.submission_obj.status, SUBMITTED) |
151
|
|
|
|
152
|
|
|
self.assertTrue(self.mock_auth.called) |
153
|
|
|
self.assertTrue(self.mock_root.called) |
154
|
|
|
self.assertTrue(self.my_root.get_submission_by_name.called) |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
class FetchIgnoreTestCase(FetchStatusHelperMixin, TestCase): |
158
|
|
|
"""a submission that could be ignored""" |
159
|
|
|
|
160
|
|
|
def setUp(self): |
161
|
|
|
# calling my base setup |
162
|
|
|
super().setUp() |
163
|
|
|
|
164
|
|
|
# an unmanaged status |
165
|
|
|
self.my_submission.status = 'Unmanaged' |
166
|
|
|
|
167
|
|
|
def common_tests(self, status): |
168
|
|
|
"""Override default common tests. Status is the status the |
169
|
|
|
submission is supposed to remain""" |
170
|
|
|
|
171
|
|
|
# assert auth, root and get_submission by name called |
172
|
|
|
super().common_tests() |
173
|
|
|
|
174
|
|
|
# USI submission status did't changed |
175
|
|
|
self.usi_submission.refresh_from_db() |
176
|
|
|
self.assertEqual(self.usi_submission.status, status) |
177
|
|
|
|
178
|
|
|
# check animal/sample status didn't changed |
179
|
|
|
n_to_submit = self.count_by_status(SUBMITTED) |
180
|
|
|
self.assertEqual(n_to_submit, self.n_to_submit) |
181
|
|
|
|
182
|
|
|
def test_fetch_unmanaged_submission_status(self): |
183
|
|
|
"""Test fetch status for an unmanaged submission""" |
184
|
|
|
|
185
|
|
|
# assert my common tests |
186
|
|
|
self.common_tests(status=SUBMITTED) |
187
|
|
|
|
188
|
|
|
def test_fetch_not_submitted(self): |
189
|
|
|
"""Ignore a submission with a status different from SUBMITTED""" |
190
|
|
|
|
191
|
|
|
self.usi_submission.status = COMPLETED |
192
|
|
|
self.usi_submission.save() |
193
|
|
|
|
194
|
|
|
self.common_tests(status=COMPLETED) |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
class FetchCompletedTestCase(FetchStatusHelperMixin, TestCase): |
198
|
|
|
"""a completed submission with two samples""" |
199
|
|
|
|
200
|
|
|
def setUp(self): |
201
|
|
|
# calling my base setup |
202
|
|
|
super().setUp() |
203
|
|
|
|
204
|
|
|
# a completed submission with two samples |
205
|
|
|
self.my_submission.status = 'Completed' |
206
|
|
|
|
207
|
|
|
def test_fetch_status(self): |
208
|
|
|
"""Test fetch status for a complete submission""" |
209
|
|
|
|
210
|
|
|
# Add samples |
211
|
|
|
my_sample1 = Mock() |
212
|
|
|
my_sample1.name = "test-animal" |
213
|
|
|
my_sample1.alias = "IMAGEA000000001" |
214
|
|
|
my_sample1.accession = "SAMEA0000001" |
215
|
|
|
my_sample2 = Mock() |
216
|
|
|
my_sample2.name = "test-sample" |
217
|
|
|
my_sample2.alias = "IMAGES000000001" |
218
|
|
|
my_sample2.accession = "SAMEA0000002" |
219
|
|
|
self.my_submission.get_samples.return_value = [my_sample1, my_sample2] |
220
|
|
|
|
221
|
|
|
# assert auth, root and get_submission by name called |
222
|
|
|
self.common_tests() |
223
|
|
|
|
224
|
|
|
# USI submission status changed |
225
|
|
|
self.usi_submission.refresh_from_db() |
226
|
|
|
self.assertEqual(self.usi_submission.status, COMPLETED) |
227
|
|
|
|
228
|
|
|
# check name status changed |
229
|
|
|
n_completed = self.count_by_status(COMPLETED) |
230
|
|
|
self.assertEqual(n_completed, 2) |
231
|
|
|
|
232
|
|
|
# fetch two name objects |
233
|
|
|
self.animal_name.refresh_from_db() |
234
|
|
|
self.assertEqual(self.animal_name.biosample_id, "SAMEA0000001") |
235
|
|
|
|
236
|
|
|
self.sample_name.refresh_from_db() |
237
|
|
|
self.assertEqual(self.sample_name.biosample_id, "SAMEA0000002") |
238
|
|
|
|
239
|
|
|
def test_fetch_status_no_accession(self): |
240
|
|
|
"""Test fetch status for a submission which doens't send accession |
241
|
|
|
no updates in such case""" |
242
|
|
|
|
243
|
|
|
# Add samples |
244
|
|
|
my_sample1 = Mock() |
245
|
|
|
my_sample1.name = "test-animal" |
246
|
|
|
my_sample1.alias = "IMAGEA000000001" |
247
|
|
|
my_sample1.accession = None |
248
|
|
|
my_sample2 = Mock() |
249
|
|
|
my_sample2.name = "test-sample" |
250
|
|
|
my_sample2.alias = "IMAGES000000001" |
251
|
|
|
my_sample2.accession = None |
252
|
|
|
self.my_submission.get_samples.return_value = [my_sample1, my_sample2] |
253
|
|
|
|
254
|
|
|
# assert auth, root and get_submission by name called |
255
|
|
|
self.common_tests() |
256
|
|
|
|
257
|
|
|
# USI submission status didn't change |
258
|
|
|
self.usi_submission.refresh_from_db() |
259
|
|
|
self.assertEqual(self.usi_submission.status, SUBMITTED) |
260
|
|
|
|
261
|
|
|
# check name status didn't changed |
262
|
|
|
n_to_submit = self.count_by_status(SUBMITTED) |
263
|
|
|
self.assertEqual(n_to_submit, self.n_to_submit) |
264
|
|
|
|
265
|
|
|
|
266
|
|
|
class FetchWithErrorsTestCase(FetchStatusHelperMixin, TestCase): |
267
|
|
|
"""Test a submission with errors for biosample""" |
268
|
|
|
|
269
|
|
|
def setUp(self): |
270
|
|
|
# calling my base setup |
271
|
|
|
super().setUp() |
272
|
|
|
|
273
|
|
|
# a draft submission with errors |
274
|
|
|
self.my_submission.status = 'Draft' |
275
|
|
|
self.my_submission.has_errors.return_value = Counter( |
276
|
|
|
{True: 1, False: 1}) |
277
|
|
|
self.my_submission.get_status.return_value = Counter({'Complete': 2}) |
278
|
|
|
|
279
|
|
|
# Add samples. Suppose that first failed, second is ok |
280
|
|
|
my_validation_result1 = Mock() |
281
|
|
|
my_validation_result1.errorMessages = { |
282
|
|
|
'Ena': [ |
283
|
|
|
'a sample message', |
284
|
|
|
] |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
my_sample1 = Mock() |
288
|
|
|
my_sample1.name = "test-animal" |
289
|
|
|
my_sample1.alias = "IMAGEA000000001" |
290
|
|
|
my_sample1.has_errors.return_value = True |
291
|
|
|
my_sample1.get_validation_result.return_value = my_validation_result1 |
292
|
|
|
|
293
|
|
|
# sample2 is ok |
294
|
|
|
my_validation_result2 = Mock() |
295
|
|
|
my_validation_result2.errorMessages = None |
296
|
|
|
|
297
|
|
|
my_sample2 = Mock() |
298
|
|
|
my_sample2.name = "test-sample" |
299
|
|
|
my_sample2.alias = "IMAGES000000001" |
300
|
|
|
my_sample2.has_errors.return_value = False |
301
|
|
|
my_sample2.get_validation_result.return_value = my_validation_result2 |
302
|
|
|
|
303
|
|
|
# simulate that IMAGEA000000001 has errors |
304
|
|
|
self.my_submission.get_samples.return_value = [my_sample1, my_sample2] |
305
|
|
|
|
306
|
|
|
# track other objects |
307
|
|
|
self.my_sample1 = my_sample1 |
308
|
|
|
self.my_sample2 = my_sample2 |
309
|
|
|
|
310
|
|
|
def common_tests(self): |
311
|
|
|
# assert auth, root and get_submission by name called |
312
|
|
|
super().common_tests() |
313
|
|
|
|
314
|
|
|
# assert custom mock attributes called |
315
|
|
|
self.assertTrue(self.my_sample1.has_errors.called) |
316
|
|
|
self.assertTrue(self.my_sample1.get_validation_result.called) |
317
|
|
|
|
318
|
|
|
# if sample has no errors, no all methods will be called |
319
|
|
|
self.assertTrue(self.my_sample2.has_errors.called) |
320
|
|
|
self.assertFalse(self.my_sample2.get_validation_result.called) |
321
|
|
|
|
322
|
|
|
def test_fetch_status(self): |
323
|
|
|
# assert tmock methods called |
324
|
|
|
self.common_tests() |
325
|
|
|
|
326
|
|
|
# USI submission changed |
327
|
|
|
self.usi_submission.refresh_from_db() |
328
|
|
|
self.assertEqual(self.usi_submission.status, NEED_REVISION) |
329
|
|
|
|
330
|
|
|
# check name status changed only for animal (not sample) |
331
|
|
|
self.animal_name.refresh_from_db() |
332
|
|
|
self.assertEqual(self.animal_name.status, NEED_REVISION) |
333
|
|
|
|
334
|
|
|
self.sample_name.refresh_from_db() |
335
|
|
|
self.assertEqual(self.sample_name.status, SUBMITTED) |
336
|
|
|
|
337
|
|
|
|
338
|
|
|
class FetchDraftTestCase(FetchStatusHelperMixin, TestCase): |
339
|
|
|
"""a draft submission without errors""" |
340
|
|
|
|
341
|
|
|
def common_tests(self): |
342
|
|
|
# assert auth, root and get_submission by name called |
343
|
|
|
super().common_tests() |
344
|
|
|
|
345
|
|
|
# USI submission status didn't change |
346
|
|
|
self.usi_submission.refresh_from_db() |
347
|
|
|
self.assertEqual(self.usi_submission.status, SUBMITTED) |
348
|
|
|
|
349
|
|
|
def test_fetch_status(self): |
350
|
|
|
# a draft submission without errors |
351
|
|
|
self.my_submission.status = 'Draft' |
352
|
|
|
self.my_submission.has_errors.return_value = Counter({False: 2}) |
353
|
|
|
self.my_submission.get_status.return_value = Counter({'Complete': 2}) |
354
|
|
|
|
355
|
|
|
# assert mock methods called |
356
|
|
|
self.common_tests() |
357
|
|
|
|
358
|
|
|
# testing a finalized biosample condition |
359
|
|
|
self.assertTrue(self.my_submission.finalize.called) |
360
|
|
|
|
361
|
|
|
def test_fetch_status_pending(self): |
362
|
|
|
"""Testing status with pending validation""" |
363
|
|
|
|
364
|
|
|
# a draft submission without errors |
365
|
|
|
self.my_submission.status = 'Draft' |
366
|
|
|
self.my_submission.has_errors.return_value = Counter({False: 2}) |
367
|
|
|
self.my_submission.get_status.return_value = Counter({'Pending': 2}) |
368
|
|
|
|
369
|
|
|
# assert mock methods called |
370
|
|
|
self.common_tests() |
371
|
|
|
|
372
|
|
|
# testing a not finalized biosample condition |
373
|
|
|
self.assertFalse(self.my_submission.finalize.called) |
374
|
|
|
|
375
|
|
|
def test_fetch_status_submitted(self): |
376
|
|
|
"""Testing status during biosample submission""" |
377
|
|
|
|
378
|
|
|
# a draft submission without errors |
379
|
|
|
self.my_submission.status = 'Submitted' |
380
|
|
|
self.my_submission.has_errors.return_value = Counter({False: 2}) |
381
|
|
|
self.my_submission.get_status.return_value = Counter({'Complete': 2}) |
382
|
|
|
|
383
|
|
|
# assert mock methods called |
384
|
|
|
self.common_tests() |
385
|
|
|
|
386
|
|
|
# testing a not finalized biosample condition |
387
|
|
|
self.assertFalse(self.my_submission.finalize.called) |
388
|
|
|
|
389
|
|
|
def test_fetch_status_processing(self): |
390
|
|
|
"""This is a status I see during validation""" |
391
|
|
|
|
392
|
|
|
# an example submission in processing stage. If everything is ok |
393
|
|
|
# it has no errors and is complete |
394
|
|
|
self.my_submission.status = 'Processing' |
395
|
|
|
self.my_submission.has_errors.return_value = Counter({False: 2}) |
396
|
|
|
self.my_submission.get_status.return_value = Counter({'Complete': 2}) |
397
|
|
|
|
398
|
|
|
# assert mock methods called |
399
|
|
|
self.common_tests() |
400
|
|
|
|
401
|
|
|
# testing a not finalized biosample condition |
402
|
|
|
self.assertFalse(self.my_submission.finalize.called) |
403
|
|
|
|
404
|
|
|
|
405
|
|
|
class FetchLongStatusTestCase(FetchStatusHelperMixin, TestCase): |
406
|
|
|
"""A submission wich remain in the same status for a long time""" |
407
|
|
|
|
408
|
|
|
def setUp(self): |
409
|
|
|
# calling my base setup |
410
|
|
|
super().setUp() |
411
|
|
|
|
412
|
|
|
# make "now" 2 months ago |
413
|
|
|
testtime = timezone.now() - timedelta(days=60) |
414
|
|
|
|
415
|
|
|
# https://devblog.kogan.com/blog/testing-auto-now-datetime-fields-in-django |
416
|
|
|
with patch('django.utils.timezone.now') as mock_now: |
417
|
|
|
mock_now.return_value = testtime |
418
|
|
|
|
419
|
|
|
# update submission updated time with an older date than now |
420
|
|
|
self.usi_submission.updated_at = testtime |
421
|
|
|
self.usi_submission.save() |
422
|
|
|
|
423
|
|
|
def common_tests(self): |
424
|
|
|
# assert auth, root and get_submission by name called |
425
|
|
|
super().common_tests() |
426
|
|
|
|
427
|
|
|
# biosample.models.Submission status changed |
428
|
|
|
self.assertEqual(self.usi_submission.status, ERROR) |
429
|
|
|
self.assertIn( |
430
|
|
|
"Biosample submission '{}' remained with the same status".format( |
431
|
|
|
self.my_submission.name), |
432
|
|
|
self.usi_submission.message |
433
|
|
|
) |
434
|
|
|
|
435
|
|
|
def test_error_in_submitted_status(self): |
436
|
|
|
# a still running submission |
437
|
|
|
self.my_submission.status = 'Submitted' |
438
|
|
|
|
439
|
|
|
# assert mock methods called |
440
|
|
|
self.common_tests() |
441
|
|
|
|
442
|
|
|
def test_error_in_draft_status(self): |
443
|
|
|
# a still running submission |
444
|
|
|
self.my_submission.status = 'Draft' |
445
|
|
|
|
446
|
|
|
# assert mock methods called |
447
|
|
|
self.common_tests() |
448
|
|
|
|
449
|
|
|
def test_error_in_processing_status(self): |
450
|
|
|
# a still running submission |
451
|
|
|
self.my_submission.status = 'Processing' |
452
|
|
|
|
453
|
|
|
# assert mock methods called |
454
|
|
|
self.common_tests() |
455
|
|
|
|
456
|
|
|
|
457
|
|
|
class FetchUnsupportedStatusTestCase(FetchMixin, TestCase): |
458
|
|
|
"""A submission object with a status I can ignore. Task will exit |
459
|
|
|
immediatey""" |
460
|
|
|
|
461
|
|
|
def setUp(self): |
462
|
|
|
# calling my base setup |
463
|
|
|
super().setUp() |
464
|
|
|
|
465
|
|
|
# define my task |
466
|
|
|
self.my_task = FetchStatusTask() |
467
|
|
|
|
468
|
|
|
# change lock_id (useful when running test during cron) |
469
|
|
|
self.my_task.lock_id = "test-FetchStatusTask" |
470
|
|
|
|
471
|
|
|
def update_status(self, status): |
472
|
|
|
# change status |
473
|
|
|
self.submission_obj.status = status |
474
|
|
|
self.submission_obj.save() |
475
|
|
|
|
476
|
|
|
# override FetchMixing methods |
477
|
|
|
def common_tests(self, status): |
478
|
|
|
# update submission status |
479
|
|
|
self.update_status(status) |
480
|
|
|
|
481
|
|
|
# NOTE that I'm calling the function directly, without delay |
482
|
|
|
# (AsyncResult). I've patched the time consuming task |
483
|
|
|
res = self.my_task.run() |
484
|
|
|
|
485
|
|
|
# assert a success with data uploading |
486
|
|
|
self.assertEqual(res, "success") |
487
|
|
|
|
488
|
|
|
self.assertFalse(self.mock_auth.called) |
489
|
|
|
self.assertFalse(self.mock_root.called) |
490
|
|
|
self.assertFalse(self.my_root.get_submission_by_name.called) |
491
|
|
|
|
492
|
|
|
# assert status for submissions |
493
|
|
|
self.submission_obj.refresh_from_db() |
494
|
|
|
self.assertEqual(self.submission_obj.status, status) |
495
|
|
|
|
496
|
|
|
def test_loaded(self): |
497
|
|
|
"""Test fecth_status with a loaded submission""" |
498
|
|
|
|
499
|
|
|
# assert task and mock methods called |
500
|
|
|
self.common_tests(LOADED) |
501
|
|
|
|
502
|
|
|
def test_need_revision(self): |
503
|
|
|
"""Test fecth_status with a need_revision submission""" |
504
|
|
|
|
505
|
|
|
# assert task and mock methods called |
506
|
|
|
self.common_tests(NEED_REVISION) |
507
|
|
|
|
508
|
|
|
def test_ready(self): |
509
|
|
|
"""Test fecth_status with a ready submission""" |
510
|
|
|
|
511
|
|
|
# assert task and mock methods called |
512
|
|
|
self.common_tests(READY) |
513
|
|
|
|
514
|
|
|
def test_completed(self): |
515
|
|
|
"""Test fecth_status with a completed submission""" |
516
|
|
|
|
517
|
|
|
# assert task and mock methods called |
518
|
|
|
self.common_tests(COMPLETED) |
519
|
|
|
|
520
|
|
|
|
521
|
|
|
class FetchStatusTaskTestCase(FetchMixin, TestCase): |
522
|
|
|
def setUp(self): |
523
|
|
|
# calling my base setup |
524
|
|
|
super().setUp() |
525
|
|
|
|
526
|
|
|
# set proper status to biosample.models.Submission |
527
|
|
|
USISubmission.objects.update(status=SUBMITTED) |
528
|
|
|
|
529
|
|
|
# starting mocked objects |
530
|
|
|
self.mock_helper_patcher = patch( |
531
|
|
|
'biosample.tasks.retrieval.FetchStatusHelper') |
532
|
|
|
self.mock_helper = self.mock_helper_patcher.start() |
533
|
|
|
|
534
|
|
|
self.mock_complete_patcher = patch( |
535
|
|
|
'biosample.tasks.retrieval.RetrievalCompleteTask') |
536
|
|
|
self.mock_complete = self.mock_complete_patcher.start() |
537
|
|
|
|
538
|
|
|
# define my task |
539
|
|
|
self.my_task = FetchStatusTask() |
540
|
|
|
|
541
|
|
|
# change lock_id (useful when running test during cron) |
542
|
|
|
self.my_task.lock_id = "test-FetchStatusTask" |
543
|
|
|
|
544
|
|
|
def tearDown(self): |
545
|
|
|
self.mock_helper_patcher.stop() |
546
|
|
|
self.mock_complete_patcher.stop() |
547
|
|
|
|
548
|
|
|
# calling base method |
549
|
|
|
super().tearDown() |
550
|
|
|
|
551
|
|
|
def test_fetch_status(self): |
552
|
|
|
"""Test fetch status task""" |
553
|
|
|
|
554
|
|
|
# NOTE that I'm calling the function directly, without delay |
555
|
|
|
# (AsyncResult). I've patched the time consuming task |
556
|
|
|
res = self.my_task.run() |
557
|
|
|
|
558
|
|
|
# assert a success with data uploading |
559
|
|
|
self.assertEqual(res, "success") |
560
|
|
|
|
561
|
|
|
# assert my objects called |
562
|
|
|
self.assertTrue(self.mock_helper.called) |
563
|
|
|
self.assertTrue(self.mock_complete.called) |
564
|
|
|
|
565
|
|
|
# those objects are proper of FetchStatusHelper class, no one |
566
|
|
|
# call them in this task itself |
567
|
|
|
self.assertFalse(self.mock_auth.called) |
568
|
|
|
self.assertFalse(self.mock_root.called) |
569
|
|
|
|
570
|
|
|
def test_fetch_status_all_completed(self): |
571
|
|
|
"""Test fetch status task with completed biosample.models.Submission""" |
572
|
|
|
|
573
|
|
|
# simulate completed case (no more requests to biosample) |
574
|
|
|
USISubmission.objects.update(status=COMPLETED) |
575
|
|
|
|
576
|
|
|
# NOTE that I'm calling the function directly, without delay |
577
|
|
|
# (AsyncResult). I've patched the time consuming task |
578
|
|
|
res = self.my_task.run() |
579
|
|
|
|
580
|
|
|
# assert a success with data uploading |
581
|
|
|
self.assertEqual(res, "success") |
582
|
|
|
|
583
|
|
|
# assert no helper called for this submission |
584
|
|
|
self.assertFalse(self.mock_helper.called) |
585
|
|
|
|
586
|
|
|
# this is called if every submission is completed |
587
|
|
|
self.assertTrue(self.mock_complete.called) |
588
|
|
|
|
589
|
|
|
# those objects are proper of FetchStatusHelper class, no one |
590
|
|
|
# call them in this task itself |
591
|
|
|
self.assertFalse(self.mock_auth.called) |
592
|
|
|
self.assertFalse(self.mock_root.called) |
593
|
|
|
|
594
|
|
|
# http://docs.celeryproject.org/en/latest/userguide/testing.html#tasks-and-unit-tests |
595
|
|
|
@patch("biosample.tasks.FetchStatusTask.retry") |
596
|
|
|
@patch("biosample.tasks.FetchStatusTask.fetch_queryset") |
597
|
|
|
def test_fetch_status_retry(self, my_fetch, my_retry): |
598
|
|
|
"""Test fetch status with retry""" |
599
|
|
|
|
600
|
|
|
# Set a side effect on the patched methods |
601
|
|
|
# so that they raise the errors we want. |
602
|
|
|
my_retry.side_effect = Retry() |
603
|
|
|
my_fetch.side_effect = USIConnectionError() |
604
|
|
|
|
605
|
|
|
with raises(Retry): |
606
|
|
|
self.my_task.run() |
607
|
|
|
|
608
|
|
|
self.assertTrue(my_fetch.called) |
609
|
|
|
self.assertTrue(my_retry.called) |
610
|
|
|
|
611
|
|
|
# assert no helper called for this submission |
612
|
|
|
self.assertFalse(self.mock_helper.called) |
613
|
|
|
self.assertFalse(self.mock_complete.called) |
614
|
|
|
|
615
|
|
|
# Test a non blocking instance |
616
|
|
|
@patch("biosample.tasks.FetchStatusTask.fetch_queryset") |
617
|
|
|
@patch("redis.lock.Lock.acquire", return_value=False) |
618
|
|
|
def test_fetch_status_nb(self, my_lock, my_fetch): |
619
|
|
|
"""Test FetchSTatus while a lock is present""" |
620
|
|
|
|
621
|
|
|
res = self.my_task.run() |
622
|
|
|
|
623
|
|
|
# assert database is locked |
624
|
|
|
self.assertEqual(res, "%s already running!" % (self.my_task.name)) |
625
|
|
|
self.assertFalse(my_fetch.called) |
626
|
|
|
|
627
|
|
|
# assert no helper called for this submission |
628
|
|
|
self.assertFalse(self.mock_helper.called) |
629
|
|
|
self.assertFalse(self.mock_complete.called) |
630
|
|
|
|
631
|
|
|
|
632
|
|
|
class RetrievalCompleteTaskTestCase(FetchMixin, WebSocketMixin, TestCase): |
633
|
|
|
"""testing update status after a fetch status""" |
634
|
|
|
|
635
|
|
|
def setUp(self): |
636
|
|
|
# calling my base setup |
637
|
|
|
super().setUp() |
638
|
|
|
|
639
|
|
|
# set proper status to biosample.models.Submission |
640
|
|
|
USISubmission.objects.update(status=SUBMITTED) |
641
|
|
|
|
642
|
|
|
# define my task |
643
|
|
|
self.my_task = RetrievalCompleteTask() |
644
|
|
|
|
645
|
|
|
def updated_check(self, status, message): |
646
|
|
|
"""Common check for tests""" |
647
|
|
|
|
648
|
|
|
# set proper status to biosample.models.Submission |
649
|
|
|
USISubmission.objects.update(status=status, message=message) |
650
|
|
|
|
651
|
|
|
# calling task |
652
|
|
|
result = self.my_task.run( |
653
|
|
|
uid_submission_id=self.submission_obj_id) |
654
|
|
|
|
655
|
|
|
# assert a success with data uploading |
656
|
|
|
self.assertEqual(result, "success") |
657
|
|
|
|
658
|
|
|
# check status and messages |
659
|
|
|
self.submission_obj.refresh_from_db() |
660
|
|
|
self.assertEqual(self.submission_obj.status, status) |
661
|
|
|
self.assertEqual(self.submission_obj.message, message) |
662
|
|
|
|
663
|
|
|
# calling a WebSocketMixin method |
664
|
|
|
self.check_message( |
665
|
|
|
STATUSES.get_value_display(status), |
666
|
|
|
message) |
667
|
|
|
|
668
|
|
|
def not_updated_check(self, status, message): |
669
|
|
|
"""Test a submission not updated""" |
670
|
|
|
|
671
|
|
|
# set proper status to biosample.models.Submission |
672
|
|
|
USISubmission.objects.filter(pk=1).update( |
673
|
|
|
status=status, message=message) |
674
|
|
|
|
675
|
|
|
# calling task |
676
|
|
|
result = self.my_task.run( |
677
|
|
|
uid_submission_id=self.submission_obj_id) |
678
|
|
|
|
679
|
|
|
# assert a success with data uploading |
680
|
|
|
self.assertEqual(result, "success") |
681
|
|
|
|
682
|
|
|
# check status and messages |
683
|
|
|
self.submission_obj.refresh_from_db() |
684
|
|
|
self.assertEqual(self.submission_obj.status, SUBMITTED) |
685
|
|
|
self.assertEqual( |
686
|
|
|
self.submission_obj.message, |
687
|
|
|
"Waiting for biosample validation") |
688
|
|
|
|
689
|
|
|
# defined in websoketmixin |
690
|
|
|
self.check_message_not_called() |
691
|
|
|
|
692
|
|
|
def test_submitted(self): |
693
|
|
|
"""Test submitted status""" |
694
|
|
|
|
695
|
|
|
status = SUBMITTED |
696
|
|
|
message = "Waiting for biosample validation" |
697
|
|
|
|
698
|
|
|
self.not_updated_check( |
699
|
|
|
status, |
700
|
|
|
message) |
701
|
|
|
|
702
|
|
|
def test_error(self): |
703
|
|
|
"""test an error in a submission""" |
704
|
|
|
|
705
|
|
|
status = ERROR |
706
|
|
|
message = "error messages" |
707
|
|
|
|
708
|
|
|
self.updated_check( |
709
|
|
|
status, |
710
|
|
|
message) |
711
|
|
|
|
712
|
|
|
# test email sent |
713
|
|
|
self.assertEqual(len(mail.outbox), 1) |
714
|
|
|
|
715
|
|
|
# read email |
716
|
|
|
email = mail.outbox[0] |
717
|
|
|
|
718
|
|
|
self.assertEqual( |
719
|
|
|
"Error in biosample submission 1", |
720
|
|
|
email.subject) |
721
|
|
|
|
722
|
|
|
def test_need_revision(self): |
723
|
|
|
"""test an issue in a submission""" |
724
|
|
|
|
725
|
|
|
status = NEED_REVISION |
726
|
|
|
message = "error messages" |
727
|
|
|
|
728
|
|
|
self.updated_check( |
729
|
|
|
status, |
730
|
|
|
message) |
731
|
|
|
|
732
|
|
|
# test email sent |
733
|
|
|
self.assertEqual(len(mail.outbox), 1) |
734
|
|
|
|
735
|
|
|
# read email |
736
|
|
|
email = mail.outbox[0] |
737
|
|
|
|
738
|
|
|
self.assertEqual( |
739
|
|
|
"Error in biosample submission 1", |
740
|
|
|
email.subject) |
741
|
|
|
|
742
|
|
|
def test_completed(self): |
743
|
|
|
"""test a submission completed""" |
744
|
|
|
|
745
|
|
|
status = COMPLETED |
746
|
|
|
message = "completed messages" |
747
|
|
|
|
748
|
|
|
self.updated_check( |
749
|
|
|
status, |
750
|
|
|
message) |
751
|
|
|
|
752
|
|
|
def test_partial_submitted(self): |
753
|
|
|
"""Having submitted in statuses will not complete the submission""" |
754
|
|
|
|
755
|
|
|
status = ERROR |
756
|
|
|
message = "error messages" |
757
|
|
|
|
758
|
|
|
self.not_updated_check( |
759
|
|
|
status, |
760
|
|
|
message) |
761
|
|
|
|
762
|
|
|
status = NEED_REVISION |
763
|
|
|
message = "error messages" |
764
|
|
|
|
765
|
|
|
self.not_updated_check( |
766
|
|
|
status, |
767
|
|
|
message) |
768
|
|
|
|
769
|
|
|
status = COMPLETED |
770
|
|
|
message = "completed messages" |
771
|
|
|
|
772
|
|
|
self.not_updated_check( |
773
|
|
|
status, |
774
|
|
|
message) |
775
|
|
|
|