Passed
Pull Request — master (#41)
by Paolo
06:50
created

SuccessfulFixValidationAnimalTest.setUp()   A

Complexity

Conditions 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 28
Ratio 100 %

Importance

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