Code Duplication    Length = 78-80 lines in 2 locations

django-data/image/animals/tests/test_tasks.py 1 location

@@ 130-209 (lines=80) @@
127
        )
128
129
130
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

django-data/image/samples/tests/test_tasks.py 1 location

@@ 120-197 (lines=78) @@
117
        )
118
119
120
class BatchUpdateSamplesTest(
121
        SampleFeaturesMixin, WebSocketMixin, TestCase):
122
123
    def setUp(self):
124
        # calling base methods
125
        super().setUp()
126
127
        # get a submission object
128
        self.submission = Submission.objects.get(pk=1)
129
        self.submission_id = self.submission.id
130
131
        # setting samples to delete
132
        self.sample_ids = {
133
            1: "meow",
134
        }
135
136
        # the attribute to change
137
        self.attribute = "collection_place"
138
139
        # setting tasks
140
        self.my_task = BatchUpdateSamples()
141
142
    def test_on_failure(self):
143
        """Testing on failure methods"""
144
145
        exc = Exception("Test")
146
        task_id = "test_task_id"
147
        args = [self.submission_id, self.sample_ids, self.attribute]
148
        kwargs = {}
149
        einfo = ExceptionInfo
150
151
        # call on_failure method
152
        self.my_task.on_failure(exc, task_id, args, kwargs, einfo)
153
154
        # check submission status and message
155
        self.submission.refresh_from_db()
156
157
        # check submission.state changed
158
        self.assertEqual(self.submission.status, ERROR)
159
        self.assertEqual(
160
            self.submission.message,
161
            "Error in sample batch update: Test")
162
163
        # test email sent
164
        self.assertGreater(len(mail.outbox), 0)
165
166
        # read email
167
        email = mail.outbox[0]
168
169
        self.assertEqual(
170
            "Error in sample batch update for submission: 1",
171
            email.subject)
172
173
        self.check_message(
174
            message='Error',
175
            notification_message='Error in sample batch update: Test')
176
177
    def test_update_sample(self):
178
        # calling task and update a sample
179
        res = self.my_task.run(
180
            submission_id=self.submission_id,
181
            sample_ids=self.sample_ids,
182
            attribute=self.attribute)
183
184
        self.assertEqual(res, "success")
185
186
        # asserting updates
187
        for key, value in self.sample_ids.items():
188
            sample = Sample.objects.get(pk=key)
189
            if value == "None":
190
                value = None
191
            self.assertEqual(getattr(sample, self.attribute), value)
192
193
        # calling a WebSocketMixin method
194
        # no validation message since no data in validation table
195
        self.check_message(
196
            message=STATUSES.get_value_display(NEED_REVISION),
197
            notification_message="Data updated, try to rerun validation",
198
        )
199