Passed
Pull Request — master (#48)
by Paolo
06:18
created

NoBatchDeleteSampleTest.setUp()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
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 16:01:38 2019
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
from unittest.mock import patch
10
11
from django.test import TestCase, Client
12
from django.urls import resolve, reverse
13
14
from common.constants import WAITING
15
from common.tests.mixins import GeneralMixinTestCase, OwnerMixinTestCase
16
17
from .common import SubmissionDataMixin, SubmissionStatusMixin
18
from ..views import (
19
    DeleteAnimalsView, DeleteSamplesView)
20
21
22 View Code Duplication
class DeleteAnimalsViewTest(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
23
        SubmissionDataMixin, GeneralMixinTestCase, OwnerMixinTestCase,
24
        TestCase):
25
26
    def setUp(self):
27
        # call base method
28
        super().setUp()
29
30
        # explict url (is not a submission:delete view)
31
        self.url = reverse('submissions:delete_animals', kwargs={'pk': 1})
32
33
        # get response (no post request)
34
        self.response = self.client.get(self.url)
35
36
    def test_url_resolves_view(self):
37
        view = resolve('/submissions/1/delete_animals/')
38
        self.assertIsInstance(view.func.view_class(), DeleteAnimalsView)
39
40
    def test_not_found_status_code(self):
41
        url = reverse('submissions:delete_animals', kwargs={'pk': 99})
42
        response = self.client.get(url)
43
        self.assertEqual(response.status_code, 404)
44
45
    def test_csrf(self):
46
        self.assertContains(self.response, 'csrfmiddlewaretoken')
47
48
    def test_contains_navigation_links(self):
49
        """Testing link to submission detail"""
50
51
        link = reverse('submissions:detail', kwargs={'pk': 1})
52
        self.assertContains(self.response, 'href="{0}"'.format(link))
53
54
55 View Code Duplication
class DeleteSamplesViewTest(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
        SubmissionDataMixin, GeneralMixinTestCase, OwnerMixinTestCase,
57
        TestCase):
58
59
    def setUp(self):
60
        # call base method
61
        super().setUp()
62
63
        # explict url (is not a submission:delete view)
64
        self.url = reverse('submissions:delete_samples', kwargs={'pk': 1})
65
66
        # get response (no post request)
67
        self.response = self.client.get(self.url)
68
69
    def test_url_resolves_view(self):
70
        view = resolve('/submissions/1/delete_samples/')
71
        self.assertIsInstance(view.func.view_class(), DeleteSamplesView)
72
73
    def test_not_found_status_code(self):
74
        url = reverse('submissions:delete_samples', kwargs={'pk': 99})
75
        response = self.client.get(url)
76
        self.assertEqual(response.status_code, 404)
77
78
    def test_csrf(self):
79
        self.assertContains(self.response, 'csrfmiddlewaretoken')
80
81
    def test_contains_navigation_links(self):
82
        """Testing link to submission detail"""
83
84
        link = reverse('submissions:detail', kwargs={'pk': 1})
85
        self.assertContains(self.response, 'href="{0}"'.format(link))
86
87
88
class NoBatchDeleteAnimalTest(
89
        SubmissionDataMixin, SubmissionStatusMixin, TestCase):
90
    """Test if I can batch delete relying on status"""
91
92
    def setUp(self):
93
        # call base method
94
        super().setUp()
95
96
        # explict url (is not a submission:delete view)
97
        self.url = reverse('submissions:delete_animals', kwargs={'pk': 1})
98
        self.redirect_url = reverse('submissions:detail', kwargs={'pk': 1})
99
100
101
class NoBatchDeleteSampleTest(
102
        SubmissionDataMixin, SubmissionStatusMixin, TestCase):
103
    """Test if I can batch delete relying on status"""
104
105
    def setUp(self):
106
        # call base method
107
        super().setUp()
108
109
        # explict url (is not a submission:delete view)
110
        self.url = reverse('submissions:delete_samples', kwargs={'pk': 1})
111
        self.redirect_url = reverse('submissions:detail', kwargs={'pk': 1})
112
113
114 View Code Duplication
class BatchDeleteMixin(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
115
        SubmissionDataMixin, GeneralMixinTestCase):
116
117
    def tearDown(self):
118
        self.batch_delete_patcher.stop()
119
120
        super().tearDown()
121
122
    def test_ownership(self):
123
        """Test a non-owner having a 404 response"""
124
125
        client = Client()
126
        client.login(username='test2', password='test2')
127
128
        response = client.post(
129
            self.url,
130
            self.data,
131
            follow=True
132
        )
133
134
        self.assertEqual(response.status_code, 404)
135
136
    def test_redirect(self):
137
        url = reverse('submissions:detail', kwargs={'pk': 1})
138
        self.assertRedirects(self.response, url)
139
140
    def test_delete(self):
141
        """Asserting task called"""
142
143
        self.assertTrue(self.batch_delete.called)
144
145
        self.submission.refresh_from_db()
146
147
        self.assertEqual(self.submission.status, WAITING)
148
        self.assertEqual(
149
            self.submission.message,
150
            "waiting for batch delete to complete")
151
152
    def test_message(self):
153
        """Assert message"""
154
155
        # check messages (defined in common.tests.MessageMixinTestCase
156
        self.check_messages(
157
            self.response,
158
            "warning",
159
            "waiting for batch delete to complete")
160
161
162 View Code Duplication
class SuccessfulDeleteAnimalsViewTest(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
163
        BatchDeleteMixin, TestCase):
164
165
    def setUp(self):
166
        """call base method"""
167
168
        # call base method
169
        super().setUp()
170
171
        # defining patcher
172
        self.batch_delete_patcher = patch(
173
            'animals.tasks.BatchDeleteAnimals.delay')
174
        self.batch_delete = self.batch_delete_patcher.start()
175
176
        # explict url (is not a submission:delete view)
177
        self.url = reverse('submissions:delete_animals', kwargs={'pk': 1})
178
179
        self.data = {'to_delete': 'ANIMAL:::ID:::132713'}
180
181
        # get a response
182
        self.response = self.client.post(
183
            self.url,
184
            self.data,
185
            follow=True
186
        )
187
188
189 View Code Duplication
class SuccessfulDeleteSamplesViewTest(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
190
        BatchDeleteMixin, TestCase):
191
192
    def setUp(self):
193
        """call base method"""
194
195
        # call base method
196
        super().setUp()
197
198
        # defining patcher
199
        self.batch_delete_patcher = patch(
200
            'samples.tasks.BatchDeleteSamples.delay')
201
        self.batch_delete = self.batch_delete_patcher.start()
202
203
        # explict url (is not a submission:delete view)
204
        self.url = reverse('submissions:delete_samples', kwargs={'pk': 1})
205
206
        self.data = {'to_delete': 'Siems_0722_393449'}
207
208
        # get a response
209
        self.response = self.client.post(
210
            self.url,
211
            self.data,
212
            follow=True
213
        )
214