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

animals.tests.test_tasks   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 119
Duplicated Lines 81.51 %

Importance

Changes 0
Metric Value
wmc 4
eloc 63
dl 97
loc 119
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A BatchDeleteAnimalsTest.setUp() 16 16 1
A BatchDeleteAnimalsTest.test_delete_animal() 21 21 1
A BatchDeleteAnimalsTest.test_on_failure() 34 34 1
A BatchDeleteAnimalsTest.test_delete_animal_not_exists() 20 20 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 = [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
    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
99
    def test_delete_animal_not_exists(self):
100
        # calling task and delete a animal
101
        res = self.my_task.run(
102
            submission_id=self.submission_id,
103
            animal_ids=["meow"])
104
105
        self.assertEqual(res, "success")
106
107
        # all animals remain
108
        n_animals = Animal.objects.count()
109
        self.assertEqual(n_animals, 3)
110
111
        # updating validation messages
112
113
        # calling a WebSocketMixin method
114
        # no validation message since no data in validation table
115
        self.check_message(
116
            message=STATUSES.get_value_display(NEED_REVISION),
117
            notification_message=(
118
                "You've removed 0 animals. It wasn't possible to find records "
119
                "with these ids: meow. Rerun validation please!")
120
        )
121