Completed
Pull Request — master (#45)
by Paolo
06:21
created

BatchDeleteAnimalsTest.test_on_failure()   A

Complexity

Conditions 1

Size

Total Lines 36
Code Lines 22

Duplication

Lines 36
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 22
dl 36
loc 36
rs 9.352
c 0
b 0
f 0
cc 1
nop 1
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
20
21
22 View Code Duplication
class BatchDeleteAnimalsTest(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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 = []
48
        kwargs = {
49
            'submission_id': self.submission_id,
50
            'animal_ids': self.animal_ids}
51
        einfo = ExceptionInfo
52
53
        # call on_failure method
54
        self.my_task.on_failure(exc, task_id, args, kwargs, einfo)
55
56
        # check submission status and message
57
        self.submission.refresh_from_db()
58
59
        # check submission.state changed
60
        self.assertEqual(self.submission.status, ERROR)
61
        self.assertEqual(
62
            self.submission.message,
63
            "Error in animal batch delete: Test")
64
65
        # test email sent
66
        self.assertGreater(len(mail.outbox), 0)
67
68
        # read email
69
        email = mail.outbox[0]
70
71
        self.assertEqual(
72
            "Error in animal batch delete for submission: 1",
73
            email.subject)
74
75
        self.check_message(
76
            message='Error',
77
            notification_message='Error in animal batch delete: Test')
78
79
    def test_delete_animal(self):
80
        # calling task and delete a animal
81
        res = self.my_task.run(
82
            submission_id=self.submission_id,
83
            animal_ids=self.animal_ids)
84
85
        self.assertEqual(res, "success")
86
87
        # no animals
88
        n_animals = Animal.objects.count()
89
        self.assertEqual(n_animals, 0)
90
91
        # updating validation messages
92
93
        # calling a WebSocketMixin method
94
        # no validation message since no data in validation table
95
        self.check_message(
96
            message=STATUSES.get_value_display(NEED_REVISION),
97
            notification_message=(
98
                "You've removed %s "
99
                "animals. Rerun validation please!" % len(self.animal_ids)))
100
101
    def test_delete_animal_not_exists(self):
102
        # calling task and delete a animal
103
        res = self.my_task.run(
104
            submission_id=self.submission_id,
105
            animal_ids=["meow"])
106
107
        self.assertEqual(res, "success")
108
109
        # all animals remain
110
        n_animals = Animal.objects.count()
111
        self.assertEqual(n_animals, 3)
112
113
        # updating validation messages
114
115
        # calling a WebSocketMixin method
116
        # no validation message since no data in validation table
117
        self.check_message(
118
            message=STATUSES.get_value_display(NEED_REVISION),
119
            notification_message=(
120
                "You've removed 0 animals. It wasn't possible to find records "
121
                "with these ids: meow. Rerun validation please!")
122
        )
123