-
Notifications
You must be signed in to change notification settings - Fork 32
/
candidatorg_views_tests.py
330 lines (292 loc) · 13.4 KB
/
candidatorg_views_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# coding=utf-8
from elections.tests import VotaInteligenteTestCase as TestCase
from elections.models import Election, QuestionCategory, Candidate
from django.core.urlresolvers import reverse
from elections.views import SoulMateDetailView
from candidator.models import TakenPosition, Position
from elections.models import Topic
from popolo.models import Person
from candidator.comparer import InformationHolder
from django.contrib.staticfiles.templatetags.staticfiles import static
from django.contrib.sites.models import Site
class CandidateInElectionsViewsTestCase(TestCase):
def setUp(self):
super(CandidateInElectionsViewsTestCase, self).setUp()
self.tarapaca = Election.objects.get(id=1)
self.coquimbo = Election.objects.get(id=2)
def test_url_candidate(self):
url = reverse('candidate_detail_view', kwargs={
'election_slug': self.tarapaca.slug,
'slug': self.tarapaca.candidates.all()[0].id
})
self.assertTrue(url)
def test_url_duplicated(self):
candidate = self.coquimbo.candidates.get(id=1)
candidate.slug = self.tarapaca.candidates.all()[0].id
candidate.save()
url_2 = reverse('candidate_detail_view', kwargs={
'election_slug': self.coquimbo.slug,
'slug': candidate.id
})
response = self.client.get(url_2)
self.assertEquals(response.status_code, 200)
self.assertEqual(response.context['election'], self.coquimbo)
self.assertEqual(response.context['candidate'], candidate)
def test_candidate_get_absolute_url(self):
candidate = self.coquimbo.candidates.get(id=1)
candidate.slug = self.tarapaca.candidates.all()[0].id
candidate.save()
url_2 = reverse('candidate_detail_view', kwargs={
'election_slug': self.coquimbo.slug,
'slug': candidate.id
})
self.assertEquals(candidate.get_absolute_url(), url_2)
def test_url_is_reachable(self):
url = reverse('candidate_detail_view', kwargs={
'election_slug': self.tarapaca.slug,
'slug': self.tarapaca.candidates.all()[0].id
})
self.assertTrue(url)
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
self.assertIn('election', response.context)
self.assertEqual(response.context['election'], self.tarapaca)
self.assertEqual(response.context['candidate'], self.tarapaca.candidates.all()[0])
self.assertTemplateUsed(response, 'elections/candidate_detail.html')
self.assertTemplateUsed(response, 'base.html')
def test_candidates_ogp(self):
site = Site.objects.get_current()
candidate = self.coquimbo.candidates.get(id=1)
self.assertTrue(candidate.ogp_enabled)
self.assertIn(candidate.name, candidate.ogp_title())
self.assertEquals('website', candidate.ogp_type())
expected_url = "http://%s%s" % (site.domain,
candidate.get_absolute_url())
self.assertEquals(expected_url, candidate.ogp_url())
expected_url = "http://%s%s" % (site.domain,
static('img/logo_vi_og.jpg'))
self.assertEquals(expected_url, candidate.ogp_image())
class QuestionaryInElectionsViewTestCase(TestCase):
def setUp(self):
super(QuestionaryInElectionsViewTestCase, self).setUp()
self.tarapaca = Election.objects.get(id=1)
def test_url_question(self):
url = reverse('questionary_detail_view',
kwargs={'slug': self.tarapaca.slug})
self.assertTrue(url)
def test_url_is_reachable(self):
url = reverse('questionary_detail_view',
kwargs={'slug': self.tarapaca.slug})
self.assertTrue(url)
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
self.assertEquals(response.context["election"], self.tarapaca)
self.assertTemplateUsed(response, 'elections/election_questionary.html')
class FaceToFaceViewTestCase(TestCase):
def setUp(self):
super(FaceToFaceViewTestCase, self).setUp()
self.tarapaca = Election.objects.get(id=1)
def test_url_face_to_face_two_candidate(self):
url = reverse('face_to_face_two_candidates_detail_view',
kwargs={
'slug': self.tarapaca.slug,
'slug_candidate_one': self.tarapaca.candidates.all()[0].id,
'slug_candidate_two': self.tarapaca.candidates.all()[1].id,
})
self.assertTrue(url)
def test_url_face_to_face_one_candidate(self):
url = reverse('face_to_face_one_candidate_detail_view',
kwargs={
'slug': self.tarapaca.slug,
'slug_candidate_one': self.tarapaca.candidates.all()[0].id
})
self.assertTrue(url)
def test_url_face_to_face_no_candidate(self):
url = reverse('face_to_face_no_candidate_detail_view',
kwargs={
'slug': self.tarapaca.slug
})
self.assertTrue(url)
def test_url_is_reachable_for_two_candidates(self):
url = reverse('face_to_face_two_candidates_detail_view',
kwargs={
'slug': self.tarapaca.slug,
'slug_candidate_one': self.tarapaca.candidates.all()[0].id,
'slug_candidate_two': self.tarapaca.candidates.all()[1].id,
})
self.assertTrue(url)
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'elections/compare_candidates.html')
self.assertIn('first_candidate', response.context)
self.assertEqual(response.context['first_candidate'], self.tarapaca.candidates.all()[0])
self.assertIn('second_candidate', response.context)
self.assertEqual(response.context['second_candidate'], self.tarapaca.candidates.all()[1])
def test_url_is_reachable_for_one_candidates(self):
url = reverse('face_to_face_one_candidate_detail_view',
kwargs={
'slug': self.tarapaca.slug,
'slug_candidate_one': self.tarapaca.candidates.all()[1].id,
})
self.assertTrue(url)
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'elections/compare_candidates.html')
self.assertIn('first_candidate', response.context)
self.assertEqual(response.context['first_candidate'], self.tarapaca.candidates.all()[1])
def test_url_is_reachable_for_no_one_candidates(self):
url = reverse('face_to_face_no_candidate_detail_view',
kwargs={
'slug': self.tarapaca.slug,
})
self.assertTrue(url)
response = self.client.get(url)
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'elections/compare_candidates.html')
class SoulMateTestCase(TestCase):
def setUp(self):
super(SoulMateTestCase, self).setUp()
self.antofa = Election.objects.get(id=2)
def test_url_better_half(self):
url = reverse('soul_mate_detail_view',
kwargs={
'slug': self.antofa.slug
})
self.assertTrue(url)
def test_url_is_reachable_for_better_half(self):
url = reverse('soul_mate_detail_view',
kwargs={
'slug': self.antofa.slug,
})
self.assertTrue(url)
response = self.client.get(url)
self.assertIn("election", response.context)
self.assertEquals(response.context['layout'], "elections/election_base.html")
self.assertTrue(response.context['result_url'].endswith(url))
self.assertEquals(response.context["election"], self.antofa)
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'elections/soulmate_candidate.html')
def test_get_embeded_12_naranja(self):
url = reverse('embedded_soul_mate_detail_view',
kwargs={
'slug': self.antofa.slug
})
response = self.client.get(url)
self.assertIn("election", response.context)
self.assertEquals(response.context["election"], self.antofa)
self.assertEquals(response.context['layout'], "elections/embedded.html")
self.assertTrue(response.context['result_url'].endswith(url))
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed(response, 'elections/soulmate_candidate.html')
def test_view_determine_the_choices(self):
view = SoulMateDetailView()
data = {
"question-0": "8",
"question-1": "11",
"question-2": "14",
"question-id-0": "4",
"question-id-1": "5",
"question-id-2": "6"
}
taken_positions = view.determine_taken_positions(data)
self.assertEquals(len(taken_positions), 3)
self.assertIsInstance(taken_positions[0], TakenPosition)
self.assertIsInstance(taken_positions[1], TakenPosition)
self.assertIsInstance(taken_positions[2], TakenPosition)
fiera_topic = Topic.objects.get(id=4)
si_fiera = Position.objects.get(id=8)
self.assertEquals(taken_positions[0].topic, fiera_topic)
self.assertEquals(taken_positions[0].position, si_fiera)
with self.assertRaises(Person.DoesNotExist):
taken_positions[0].person
benito_topic = Topic.objects.get(id=5)
si_benito = Position.objects.get(id=11)
self.assertEquals(taken_positions[1].topic, benito_topic)
self.assertEquals(taken_positions[1].position, si_benito)
with self.assertRaises(Person.DoesNotExist):
taken_positions[1].person
freedom_topic = Topic.objects.get(id=6)
no_freedom = Position.objects.get(id=14)
self.assertEquals(taken_positions[2].topic, freedom_topic)
self.assertEquals(taken_positions[2].position, no_freedom)
with self.assertRaises(Person.DoesNotExist):
taken_positions[2].person
def test_topic_str(self):
freedom_topic = Topic.objects.get(id=6)
self.assertIn(str(freedom_topic.election), str(freedom_topic))
self.assertIn(freedom_topic.label, str(freedom_topic))
def test_get_information_holder(self):
view = SoulMateDetailView()
view.object = self.antofa
holder = view.get_information_holder()
self.assertIsInstance(holder, InformationHolder)
for candidate in holder.persons:
self.assertIsInstance(candidate, Candidate)
self.assertIn(int(candidate.id), [1, 2, 3])
for category in holder.categories:
self.assertIsInstance(category, QuestionCategory)
self.assertIn(int(category.id), [3, 4])
def test_get_information_holder_with_positions(self):
view = SoulMateDetailView()
view.object = self.antofa
data = {
"question-0": "8",
"question-1": "11",
"question-2": "14",
"question-id-0": "4",
"question-id-1": "5",
"question-id-2": "6"
}
holder = view.get_information_holder(data=data)
for candidate in holder.persons:
self.assertIsInstance(candidate, Candidate)
self.assertIn(int(candidate.id), [1, 2, 3])
for category in holder.categories:
self.assertIsInstance(category, QuestionCategory)
self.assertIn(int(category.id), [3, 4])
for position_id in holder.positions:
self.assertIsInstance(holder.positions[position_id], TakenPosition)
self.assertIn(int(holder.positions[position_id].topic.id), [4, 5, 6])
self.assertIn(int(holder.positions[position_id].position.id), [8, 11, 14])
with self.assertRaises(Person.DoesNotExist):
holder.positions[position_id].person
def test_post_with_data_2(self):
data = {
"question-0": "8",
"question-1": "11",
"question-2": "13",
"question-id-0": "4",
"question-id-1": "5",
"question-id-2": "6"
}
url = reverse('soul_mate_detail_view',
kwargs={
'slug': self.antofa.slug,
})
response = self.client.post(url, data=data)
self.assertEquals(response.status_code, 200)
self.assertTemplateUsed("elections/soulmate_response.html")
self.assertIn("election", response.context)
self.assertEquals(response.context["election"], self.antofa)
self.assertIn("winner", response.context)
self.assertIn("candidate", response.context["winner"])
self.assertIsInstance(response.context["winner"]["candidate"], Candidate)
self.assertIn("others", response.context)
candidatos_antofa = self.antofa.candidates.all()
self.assertIn(response.context["winner"]["candidate"], candidatos_antofa)
def test_if_no_taken_position_provided(self):
'''If there is no taken prosition provided'''
data = {
"question-0": "8",
"question-1": "0", # This data is missing
"question-2": "13",
"question-id-0": "4",
"question-id-1": "5",
"question-id-2": "6"
}
url = reverse('soul_mate_detail_view',
kwargs={
'slug': self.antofa.slug,
})
response = self.client.post(url, data=data)
self.assertEquals(response.status_code, 200)