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

NoBatchUpdateTest.setUp()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 12
rs 9.95
c 0
b 0
f 0
cc 1
nop 1
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Wed Sep 18 12:07:40 2019
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
from unittest.mock import patch
10
11
from django.test import Client, TestCase
12
from django.urls import resolve, reverse
13
14
from common.constants import WAITING
15
from common.tests import GeneralMixinTestCase, OwnerMixinTestCase
16
from validation.models import ValidationSummary
17
18
from .common import SubmissionDataMixin, SubmissionStatusMixin
19
from ..views import SubmissionValidationSummaryFixErrorsView, FixValidation
20
21
22
class SubmissionValidationSummaryFixErrorsViewTest(
23
        SubmissionDataMixin, GeneralMixinTestCase, OwnerMixinTestCase,
24
        TestCase):
25
    """Test SubmissionValidationSummaryViewTest View"""
26
27
    def setUp(self):
28
        super().setUp()
29
30
        self.url = reverse(
31
            'submissions:validation_summary_fix_errors',
32
            kwargs={
33
                'pk': 1,
34
                'type': 'animal',
35
                'message_counter': 0})
36
37
        self.response = self.client.get(self.url)
38
39
    def test_url_resolves_view(self):
40
        view = resolve('/submissions/1/validation_summary/animal/0/')
41
        self.assertIsInstance(
42
            view.func.view_class(),
43
            SubmissionValidationSummaryFixErrorsView)
44
45
    def test_view_not_found_status_code(self):
46
        url = reverse(
47
            'submissions:validation_summary_fix_errors',
48
            kwargs={
49
                'pk': 99,
50
                'type': 'animal',
51
                'message_counter': 0})
52
        response = self.client.get(url)
53
        self.assertEqual(response.status_code, 404)
54
55
56
class SubmissionValidationSummaryFixErrorsViewColumnTest(
57
        SubmissionDataMixin, TestCase):
58
59
    def setUp(self):
60
        # calling minxin methods
61
        super().setUp()
62
63
        self.url_animal = reverse(
64
            'submissions:validation_summary_fix_errors',
65
            kwargs={
66
                'pk': 1,
67
                'type': 'animal',
68
                'message_counter': 0})
69
70
        self.url_sample = reverse(
71
            'submissions:validation_summary_fix_errors',
72
            kwargs={
73
                'pk': 1,
74
                'type': 'sample',
75
                'message_counter': 0})
76
77
    def test_animal_age_issue(self):
78
        """Testing animal age column"""
79
80
        # is an animal VS
81
        vs = ValidationSummary.objects.get(pk=2)
82
        vs.messages = [
83
            ("{'message': 'Error: One of minutes, ... need to be present for "
84
             "the field Animal age at collection (specimen from organism "
85
             "section)', 'count': 1, 'ids': [1], 'offending_column': 'Animal "
86
             "age at collection'}")
87
        ]
88
        vs.save()
89
90
        # get animal page
91
        response = self.client.get(self.url_sample)
92
        self.assertEqual(response.status_code, 200)
93
94
    def test_accuracy_issue(self):
95
        """Testing animal age column"""
96
97
        # is an animal VS
98
        vs = ValidationSummary.objects.get(pk=1)
99
        vs.messages = [
100
            ("{'message': 'Error: .* of field .* is not in the valid values "
101
             "list .*', 'count': 1, 'ids': [1], 'offending_column': 'Birth "
102
             "location accuracy'}")
103
        ]
104
        vs.save()
105
106
        # get animal page
107
        response = self.client.get(self.url_animal)
108
        self.assertEqual(response.status_code, 200)
109
110
    def test_storage_processing_issue(self):
111
        """Testing sample storage processing column"""
112
113
        # is an animal VS
114
        vs = ValidationSummary.objects.get(pk=2)
115
        vs.messages = [
116
            ("{'message': 'Error: .* of field .* is not in the valid values "
117
             "list .*', 'count': 1, 'ids': [1], 'offending_column': 'Sample "
118
             "storage processing'}")
119
        ]
120
        vs.save()
121
122
        # get animal page
123
        response = self.client.get(self.url_sample)
124
        self.assertEqual(response.status_code, 200)
125
126
    def test_storage_issue(self):
127
        """Testing sample storage processing column"""
128
129
        # is an animal VS
130
        vs = ValidationSummary.objects.get(pk=2)
131
        vs.messages = [
132
            ("{'message': 'Error: .* of field .* is not in the valid values "
133
             "list .*', 'count': 1, 'ids': [1], 'offending_column': 'Sample "
134
             "storage'}")
135
        ]
136
        vs.save()
137
138
        # get animal page
139
        response = self.client.get(self.url_sample)
140
        self.assertEqual(response.status_code, 200)
141
142
143
class NoBatchUpdateTest(
144
        SubmissionDataMixin, SubmissionStatusMixin, TestCase):
145
    """Test if I can batch delete relying on status"""
146
147
    def setUp(self):
148
        # call base method
149
        super().setUp()
150
151
        # explict url (is not a submission:delete view)
152
        self.url = reverse(
153
            'submissions:validation_summary_fix_errors',
154
            kwargs={
155
                'pk': 1,
156
                'type': 'animal',
157
                'message_counter': 0})
158
        self.redirect_url = reverse('submissions:detail', kwargs={'pk': 1})
159
160
161 View Code Duplication
class FixValidationMixin(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
162
        SubmissionDataMixin, GeneralMixinTestCase):
163
164
    def tearDown(self):
165
        self.batch_update_patcher.stop()
166
167
        super().tearDown()
168
169
    def test_ownership(self):
170
        """Test a non-owner having a 404 response"""
171
172
        client = Client()
173
        client.login(username='test2', password='test2')
174
175
        response = client.post(
176
            self.url,
177
            self.data,
178
            follow=True
179
        )
180
181
        self.assertEqual(response.status_code, 404)
182
183
    def test_redirect(self):
184
        url = reverse('submissions:detail', kwargs={'pk': 1})
185
        self.assertRedirects(self.response, url)
186
187
    def test_update(self):
188
        """Asserting task called"""
189
190
        self.assertTrue(self.batch_update.called)
191
192
        self.submission.refresh_from_db()
193
194
        self.assertEqual(self.submission.status, WAITING)
195
        self.assertEqual(
196
            self.submission.message,
197
            "waiting for data updating")
198
199
    def test_message(self):
200
        """Assert message"""
201
202
        # check messages (defined in common.tests.MessageMixinTestCase
203
        self.check_messages(
204
            self.response,
205
            "warning",
206
            "waiting for data updating")
207
208
209 View Code Duplication
class SuccessfulFixValidationAnimalTest(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
210
        FixValidationMixin, TestCase):
211
212
    def setUp(self):
213
        """call base method"""
214
215
        # call base method
216
        super().setUp()
217
218
        # defining patcher
219
        self.batch_update_patcher = patch(
220
            'animals.tasks.BatchUpdateAnimals.delay')
221
        self.batch_update = self.batch_update_patcher.start()
222
223
        # setting attribute
224
        self.attribute_to_edit = 'birth_location'
225
226
        self.url = reverse(
227
            'submissions:fix_validation',
228
            kwargs={
229
                'pk': 1,
230
                'record_type': 'animal',
231
                'attribute_to_edit': self.attribute_to_edit})
232
233
        self.data = {'to_edit1': 'Meow'}
234
235
        # get a response
236
        self.response = self.client.post(
237
            self.url,
238
            self.data,
239
            follow=True
240
        )
241
242
    def test_url_resolves_view(self):
243
        view = resolve('/submissions/1/fix_validation/animal/birth_location/')
244
        self.assertIsInstance(view.func.view_class(), FixValidation)
245
246
    def test_called_args(self):
247
        """Testing used arguments"""
248
        self.batch_update.assert_called_with(
249
            str(self.submission.id),
250
            {1: "Meow"},
251
            self.attribute_to_edit)
252
253
254 View Code Duplication
class SuccessfulFixValidationSampleTest(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
255
        FixValidationMixin, TestCase):
256
257
    def setUp(self):
258
        """call base method"""
259
260
        # call base method
261
        super().setUp()
262
263
        # defining patcher
264
        self.batch_update_patcher = patch(
265
            'samples.tasks.BatchUpdateSamples.delay')
266
        self.batch_update = self.batch_update_patcher.start()
267
268
        # setting attribute
269
        self.attribute_to_edit = 'collection_place'
270
271
        self.url = reverse(
272
            'submissions:fix_validation',
273
            kwargs={
274
                'pk': 1,
275
                'record_type': 'sample',
276
                'attribute_to_edit': self.attribute_to_edit})
277
278
        self.data = {'to_edit1': 'Meow'}
279
280
        # get a response
281
        self.response = self.client.post(
282
            self.url,
283
            self.data,
284
            follow=True
285
        )
286
287
    def test_url_resolves_view(self):
288
        view = resolve(
289
            '/submissions/1/fix_validation/sample/collection_place/')
290
        self.assertIsInstance(view.func.view_class(), FixValidation)
291
292
    def test_called_args(self):
293
        """Testing used arguments"""
294
        self.batch_update.assert_called_with(
295
            str(self.submission.id),
296
            {1: "Meow"},
297
            self.attribute_to_edit)
298