1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Mon Sep 16 14:03:56 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, Animal |
15
|
|
|
from common.constants import NEED_REVISION, STATUSES, ERROR |
16
|
|
|
from common.tests import WebSocketMixin |
17
|
|
|
|
18
|
|
|
from .common import AnimalFeaturesMixin |
19
|
|
|
from ..tasks import BatchDeleteAnimals, BatchUpdateAnimals |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class BatchDeleteAnimalsTest( |
23
|
|
|
AnimalFeaturesMixin, 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 animals to delete |
34
|
|
|
self.animal_ids = [ |
35
|
|
|
'ANIMAL:::ID:::132713', |
36
|
|
|
'ANIMAL:::ID:::son', |
37
|
|
|
'ANIMAL:::ID:::mother'] |
38
|
|
|
|
39
|
|
|
# setting tasks |
40
|
|
|
self.my_task = BatchDeleteAnimals() |
41
|
|
|
|
42
|
|
|
def test_on_failure(self): |
43
|
|
|
"""Testing on failure methods""" |
44
|
|
|
|
45
|
|
|
exc = Exception("Test") |
46
|
|
|
task_id = "test_task_id" |
47
|
|
|
args = [self.submission_id, self.animal_ids] |
48
|
|
|
kwargs = {} |
49
|
|
|
einfo = ExceptionInfo |
50
|
|
|
|
51
|
|
|
# call on_failure method |
52
|
|
|
self.my_task.on_failure(exc, task_id, args, kwargs, einfo) |
53
|
|
|
|
54
|
|
|
# check submission status and message |
55
|
|
|
self.submission.refresh_from_db() |
56
|
|
|
|
57
|
|
|
# check submission.state changed |
58
|
|
|
self.assertEqual(self.submission.status, ERROR) |
59
|
|
|
self.assertEqual( |
60
|
|
|
self.submission.message, |
61
|
|
|
"Error in animal batch delete: Test") |
62
|
|
|
|
63
|
|
|
# test email sent |
64
|
|
|
self.assertGreater(len(mail.outbox), 0) |
65
|
|
|
|
66
|
|
|
# read email |
67
|
|
|
email = mail.outbox[0] |
68
|
|
|
|
69
|
|
|
self.assertEqual( |
70
|
|
|
"Error in animal batch delete for submission: 1", |
71
|
|
|
email.subject) |
72
|
|
|
|
73
|
|
|
self.check_message( |
74
|
|
|
message='Error', |
75
|
|
|
notification_message='Error in animal batch delete: Test') |
76
|
|
|
|
77
|
|
View Code Duplication |
def test_delete_animal(self): |
|
|
|
|
78
|
|
|
# calling task and delete a animal |
79
|
|
|
res = self.my_task.run( |
80
|
|
|
submission_id=self.submission_id, |
81
|
|
|
animal_ids=self.animal_ids) |
82
|
|
|
|
83
|
|
|
self.assertEqual(res, "success") |
84
|
|
|
|
85
|
|
|
# no animals |
86
|
|
|
n_animals = Animal.objects.count() |
87
|
|
|
self.assertEqual(n_animals, 0) |
88
|
|
|
|
89
|
|
|
# updating validation messages |
90
|
|
|
|
91
|
|
|
# calling a WebSocketMixin method |
92
|
|
|
# no validation message since no data in validation table |
93
|
|
|
self.check_message( |
94
|
|
|
message=STATUSES.get_value_display(NEED_REVISION), |
95
|
|
|
notification_message=( |
96
|
|
|
"You've removed %s " |
97
|
|
|
"animals. Rerun validation please!" % len(self.animal_ids)), |
98
|
|
|
validation_message={ |
99
|
|
|
'animals': 0, 'samples': 0, 'animal_unkn': 0, |
100
|
|
|
'sample_unkn': 0, 'animal_issues': 0, 'sample_issues': 0} |
101
|
|
|
) |
102
|
|
|
|
103
|
|
View Code Duplication |
def test_delete_animal_not_exists(self): |
|
|
|
|
104
|
|
|
# calling task and delete a animal |
105
|
|
|
res = self.my_task.run( |
106
|
|
|
submission_id=self.submission_id, |
107
|
|
|
animal_ids=["meow"]) |
108
|
|
|
|
109
|
|
|
self.assertEqual(res, "success") |
110
|
|
|
|
111
|
|
|
# all animals remain |
112
|
|
|
n_animals = Animal.objects.count() |
113
|
|
|
self.assertEqual(n_animals, 3) |
114
|
|
|
|
115
|
|
|
# updating validation messages |
116
|
|
|
|
117
|
|
|
# calling a WebSocketMixin method |
118
|
|
|
# no validation message since no data in validation table |
119
|
|
|
self.check_message( |
120
|
|
|
message=STATUSES.get_value_display(NEED_REVISION), |
121
|
|
|
notification_message=( |
122
|
|
|
"You've removed 0 animals. It wasn't possible to find records " |
123
|
|
|
"with these ids: meow. Rerun validation please!"), |
124
|
|
|
validation_message={ |
125
|
|
|
'animals': 3, 'samples': 1, 'animal_unkn': 3, |
126
|
|
|
'sample_unkn': 1, 'animal_issues': 0, 'sample_issues': 0} |
127
|
|
|
) |
128
|
|
|
|
129
|
|
|
|
130
|
|
View Code Duplication |
class BatchUpdateAnimalsTest( |
|
|
|
|
131
|
|
|
AnimalFeaturesMixin, WebSocketMixin, TestCase): |
132
|
|
|
|
133
|
|
|
def setUp(self): |
134
|
|
|
# calling base methods |
135
|
|
|
super().setUp() |
136
|
|
|
|
137
|
|
|
# get a submission object |
138
|
|
|
self.submission = Submission.objects.get(pk=1) |
139
|
|
|
self.submission_id = self.submission.id |
140
|
|
|
|
141
|
|
|
# setting animals to delete |
142
|
|
|
self.animal_ids = { |
143
|
|
|
1: "meow", |
144
|
|
|
2: "bark", |
145
|
|
|
3: "None" |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
# the attribute to change |
149
|
|
|
self.attribute = "birth_location" |
150
|
|
|
|
151
|
|
|
# setting tasks |
152
|
|
|
self.my_task = BatchUpdateAnimals() |
153
|
|
|
|
154
|
|
|
def test_on_failure(self): |
155
|
|
|
"""Testing on failure methods""" |
156
|
|
|
|
157
|
|
|
exc = Exception("Test") |
158
|
|
|
task_id = "test_task_id" |
159
|
|
|
args = [self.submission_id, self.animal_ids, self.attribute] |
160
|
|
|
kwargs = {} |
161
|
|
|
einfo = ExceptionInfo |
162
|
|
|
|
163
|
|
|
# call on_failure method |
164
|
|
|
self.my_task.on_failure(exc, task_id, args, kwargs, einfo) |
165
|
|
|
|
166
|
|
|
# check submission status and message |
167
|
|
|
self.submission.refresh_from_db() |
168
|
|
|
|
169
|
|
|
# check submission.state changed |
170
|
|
|
self.assertEqual(self.submission.status, ERROR) |
171
|
|
|
self.assertEqual( |
172
|
|
|
self.submission.message, |
173
|
|
|
"Error in animal batch update: Test") |
174
|
|
|
|
175
|
|
|
# test email sent |
176
|
|
|
self.assertGreater(len(mail.outbox), 0) |
177
|
|
|
|
178
|
|
|
# read email |
179
|
|
|
email = mail.outbox[0] |
180
|
|
|
|
181
|
|
|
self.assertEqual( |
182
|
|
|
"Error in animal batch update for submission: 1", |
183
|
|
|
email.subject) |
184
|
|
|
|
185
|
|
|
self.check_message( |
186
|
|
|
message='Error', |
187
|
|
|
notification_message='Error in animal batch update: Test') |
188
|
|
|
|
189
|
|
|
def test_update_animal(self): |
190
|
|
|
# calling task and update a animal |
191
|
|
|
res = self.my_task.run( |
192
|
|
|
submission_id=self.submission_id, |
193
|
|
|
animal_ids=self.animal_ids, |
194
|
|
|
attribute=self.attribute) |
195
|
|
|
|
196
|
|
|
self.assertEqual(res, "success") |
197
|
|
|
|
198
|
|
|
# asserting updates |
199
|
|
|
for key, value in self.animal_ids.items(): |
200
|
|
|
animal = Animal.objects.get(pk=key) |
201
|
|
|
if value == "None": |
202
|
|
|
value = None |
203
|
|
|
self.assertEqual(getattr(animal, self.attribute), value) |
204
|
|
|
|
205
|
|
|
# calling a WebSocketMixin method |
206
|
|
|
# no validation message since no data in validation table |
207
|
|
|
self.check_message( |
208
|
|
|
message=STATUSES.get_value_display(NEED_REVISION), |
209
|
|
|
notification_message="Data updated, try to rerun validation", |
210
|
|
|
) |
211
|
|
|
|