1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Mon Sep 9 17:24:54 2019 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from billiard.einfo import ExceptionInfo |
10
|
|
|
|
11
|
|
|
from django.core import mail |
12
|
|
|
from django.test import TestCase |
13
|
|
|
|
14
|
|
|
from image_app.models import Submission, Sample |
15
|
|
|
from common.constants import NEED_REVISION, STATUSES, ERROR |
16
|
|
|
from common.tests import WebSocketMixin |
17
|
|
|
|
18
|
|
|
from .common import SampleFeaturesMixin |
19
|
|
|
from ..tasks import BatchDeleteSamples |
20
|
|
|
|
21
|
|
|
|
22
|
|
View Code Duplication |
class BatchDeleteSamplesTest( |
|
|
|
|
23
|
|
|
SampleFeaturesMixin, WebSocketMixin, TestCase): |
24
|
|
|
|
25
|
|
|
def setUp(self): |
26
|
|
|
# calling base methods |
27
|
|
|
super().setUp() |
28
|
|
|
|
29
|
|
|
# get a submission object |
30
|
|
|
self.submission = Submission.objects.get(pk=1) |
31
|
|
|
self.submission_id = self.submission.id |
32
|
|
|
|
33
|
|
|
# setting samples to delete |
34
|
|
|
self.sample_ids = ['Siems_0722_393449'] |
35
|
|
|
|
36
|
|
|
# setting tasks |
37
|
|
|
self.my_task = BatchDeleteSamples() |
38
|
|
|
|
39
|
|
|
def test_on_failure(self): |
40
|
|
|
"""Testing on failure methods""" |
41
|
|
|
|
42
|
|
|
exc = Exception("Test") |
43
|
|
|
task_id = "test_task_id" |
44
|
|
|
args = [] |
45
|
|
|
kwargs = { |
46
|
|
|
'submission_id': self.submission_id, |
47
|
|
|
'animal_ids': self.sample_ids} |
48
|
|
|
einfo = ExceptionInfo |
49
|
|
|
|
50
|
|
|
# call on_failure method |
51
|
|
|
self.my_task.on_failure(exc, task_id, args, kwargs, einfo) |
52
|
|
|
|
53
|
|
|
# check submission status and message |
54
|
|
|
self.submission.refresh_from_db() |
55
|
|
|
|
56
|
|
|
# check submission.state changed |
57
|
|
|
self.assertEqual(self.submission.status, ERROR) |
58
|
|
|
self.assertEqual( |
59
|
|
|
self.submission.message, |
60
|
|
|
"Error in sample batch delete: Test") |
61
|
|
|
|
62
|
|
|
# test email sent |
63
|
|
|
self.assertGreater(len(mail.outbox), 0) |
64
|
|
|
|
65
|
|
|
# read email |
66
|
|
|
email = mail.outbox[0] |
67
|
|
|
|
68
|
|
|
self.assertEqual( |
69
|
|
|
"Error in sample batch delete for submission: 1", |
70
|
|
|
email.subject) |
71
|
|
|
|
72
|
|
|
self.check_message( |
73
|
|
|
message='Error', |
74
|
|
|
notification_message='Error in sample batch delete: Test') |
75
|
|
|
|
76
|
|
|
def test_delete_sample(self): |
77
|
|
|
# calling task and delete a sample |
78
|
|
|
res = self.my_task.run( |
79
|
|
|
submission_id=self.submission.id, |
80
|
|
|
sample_ids=self.sample_ids) |
81
|
|
|
|
82
|
|
|
self.assertEqual(res, "success") |
83
|
|
|
|
84
|
|
|
# no samples |
85
|
|
|
n_samples = Sample.objects.count() |
86
|
|
|
self.assertEqual(n_samples, 0) |
87
|
|
|
|
88
|
|
|
# updating validation messages |
89
|
|
|
|
90
|
|
|
# calling a WebSocketMixin method |
91
|
|
|
# no validation message since no data in validation table |
92
|
|
|
self.check_message( |
93
|
|
|
message=STATUSES.get_value_display(NEED_REVISION), |
94
|
|
|
notification_message=( |
95
|
|
|
"You've removed %s " |
96
|
|
|
"samples. Rerun validation please!" % len(self.sample_ids))) |
97
|
|
|
|
98
|
|
|
def test_delete_samples_not_exists(self): |
99
|
|
|
# calling task and delete a animal |
100
|
|
|
res = self.my_task.run( |
101
|
|
|
submission_id=self.submission_id, |
102
|
|
|
sample_ids=["meow"]) |
103
|
|
|
|
104
|
|
|
self.assertEqual(res, "success") |
105
|
|
|
|
106
|
|
|
# all samples remain |
107
|
|
|
n_samples = Sample.objects.count() |
108
|
|
|
self.assertEqual(n_samples, 1) |
109
|
|
|
|
110
|
|
|
# updating validation messages |
111
|
|
|
|
112
|
|
|
# calling a WebSocketMixin method |
113
|
|
|
# no validation message since no data in validation table |
114
|
|
|
self.check_message( |
115
|
|
|
message=STATUSES.get_value_display(NEED_REVISION), |
116
|
|
|
notification_message=( |
117
|
|
|
"You've removed 0 samples. It wasn't possible to find records " |
118
|
|
|
"with these ids: meow. Rerun validation please!") |
119
|
|
|
) |
120
|
|
|
|