1
|
|
|
#!/usr/bin/env python3 |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
""" |
4
|
|
|
Created on Mon Oct 14 12:56:11 2019 |
5
|
|
|
|
6
|
|
|
@author: Paolo Cozzi <[email protected]> |
7
|
|
|
""" |
8
|
|
|
|
9
|
|
|
from django.test import TestCase |
10
|
|
|
from django.urls import resolve, reverse |
11
|
|
|
|
12
|
|
|
from common.tests import ( |
13
|
|
|
FormMixinTestCase, OwnerMixinTestCase, InvalidFormMixinTestCase) |
14
|
|
|
from uid.models import Submission |
15
|
|
|
|
16
|
|
|
from ..forms import UpdateSubmissionForm |
17
|
|
|
from ..views import UpdateSubmissionView |
18
|
|
|
|
19
|
|
|
from .common import SubmissionDataMixin |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
class UpdateSubmissionViewTest( |
23
|
|
|
FormMixinTestCase, OwnerMixinTestCase, SubmissionDataMixin, TestCase): |
24
|
|
|
|
25
|
|
|
form_class = UpdateSubmissionForm |
26
|
|
|
|
27
|
|
|
def setUp(self): |
28
|
|
|
# call base method |
29
|
|
|
super().setUp() |
30
|
|
|
|
31
|
|
|
self.url = reverse('submissions:update', kwargs={'pk': 1}) |
32
|
|
|
self.response = self.client.get(self.url) |
33
|
|
|
|
34
|
|
|
def test_url_resolves_view(self): |
35
|
|
|
view = resolve('/submissions/1/update/') |
36
|
|
|
self.assertIsInstance(view.func.view_class(), UpdateSubmissionView) |
37
|
|
|
|
38
|
|
|
def test_form_inputs(self): |
39
|
|
|
self.assertContains(self.response, '<input', 5) |
40
|
|
|
self.assertContains(self.response, '<select', 3) |
41
|
|
|
|
42
|
|
|
def test_reload_not_found_status_code(self): |
43
|
|
|
url = reverse('submissions:update', kwargs={'pk': 99}) |
44
|
|
|
response = self.client.get(url) |
45
|
|
|
self.assertEqual(response.status_code, 404) |
46
|
|
|
|
47
|
|
|
def test_contains_navigation_links(self): |
48
|
|
|
"""Contain links to DetailSubmissionView""" |
49
|
|
|
|
50
|
|
|
link = reverse('submissions:detail', kwargs={'pk': 1}) |
51
|
|
|
self.assertContains(self.response, 'href="{0}"'.format(link)) |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
class SuccessfulUpdateSubmissionViewTest(SubmissionDataMixin, TestCase): |
55
|
|
|
def setUp(self): |
56
|
|
|
# call base method |
57
|
|
|
super().setUp() |
58
|
|
|
|
59
|
|
|
self.data = { |
60
|
|
|
'title': 'test-edited', |
61
|
|
|
'description': 'edited-submission', |
62
|
|
|
'gene_bank_name': 'Cryoweb-edited', |
63
|
|
|
'gene_bank_country': 1, |
64
|
|
|
'organization': 1, |
65
|
|
|
"datasource_type": 0, |
66
|
|
|
"datasource_version": "test", |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
self.url = reverse('submissions:update', kwargs={'pk': 1}) |
70
|
|
|
self.response = self.client.post(self.url, self.data, follow=True) |
71
|
|
|
|
72
|
|
|
def test_redirect(self): |
73
|
|
|
url = reverse('submissions:detail', kwargs={'pk': 1}) |
74
|
|
|
self.assertRedirects(self.response, url) |
75
|
|
|
|
76
|
|
|
def test_model_updated(self): |
77
|
|
|
self.submission.refresh_from_db() |
78
|
|
|
self.assertEqual(self.submission.title, self.data["title"]) |
79
|
|
|
self.assertEqual(self.submission.description, self.data["description"]) |
80
|
|
|
|
81
|
|
|
def test_same_submission(self): |
82
|
|
|
"""Updating a submission with the same parameters of another one""" |
83
|
|
|
|
84
|
|
|
# ok create a new submission object |
85
|
|
|
data = { |
86
|
|
|
"title": "test", |
87
|
|
|
"project": "IMAGE", |
88
|
|
|
"description": "image test data", |
89
|
|
|
"gene_bank_name": "Cryoweb", |
90
|
|
|
"gene_bank_country_id": 1, |
91
|
|
|
"datasource_type": 0, |
92
|
|
|
"datasource_version": "test", |
93
|
|
|
"organization_id": 1, |
94
|
|
|
"uploaded_file": "data_source/cryoweb_test_data_only.sql", |
95
|
|
|
"created_at": "2018-04-11T13:44:31.988Z", |
96
|
|
|
"updated_at": "2018-04-11T13:44:31.988Z", |
97
|
|
|
"status": 0, |
98
|
|
|
"message": "waiting for data loading", |
99
|
|
|
"owner_id": 1 |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
submission = Submission.objects.create(**data) |
103
|
|
|
|
104
|
|
|
# define a new url |
105
|
|
|
url = reverse('submissions:update', kwargs={'pk': submission.id}) |
106
|
|
|
|
107
|
|
|
response = self.client.post( |
108
|
|
|
url, |
109
|
|
|
self.data, |
110
|
|
|
follow=True) |
111
|
|
|
|
112
|
|
|
# assert status code (no redirect) |
113
|
|
|
self.assertEqual(response.status_code, 200) |
114
|
|
|
|
115
|
|
|
# check errors |
116
|
|
|
form = response.context.get('form') |
117
|
|
|
self.assertGreater(len(form.errors), 0) |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
class InvalidUpdateSubmissionViewTest( |
121
|
|
|
InvalidFormMixinTestCase, SubmissionDataMixin, TestCase): |
122
|
|
|
def setUp(self): |
123
|
|
|
# call base method |
124
|
|
|
super().setUp() |
125
|
|
|
|
126
|
|
|
self.url = reverse('submissions:update', kwargs={'pk': 1}) |
127
|
|
|
self.response = self.client.post(self.url, {}) |
128
|
|
|
|