-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_views_with_request_factory.py
152 lines (120 loc) · 5.51 KB
/
test_views_with_request_factory.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
from django.http import Http404
from django.utils import timezone
from django.urls import reverse
from polls.models import Choice, Question
from polls.tests import base
from polls.views import DetailView, IndexView, ResultsView, vote
class QuestionViewTests(base.BaseTestCase):
def test_index_view_with_no_questions(self):
"""
If no questions exist, an appropriate message should be displayed.
"""
request = self.request_factory.get(reverse('polls:index'))
response = IndexView.as_view()(request)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "No polls are available")
def test_index_view_with_a_past_question(self):
"""
Questions with a pub_date in the past should be displayed on the
index page.
"""
question = self.create_question(question_text="Past question", days=-30)
request = self.request_factory.get(reverse('polls:index'))
response = IndexView.as_view()(request)
self.assertContains(response, question.question_text)
def test_index_view_with_a_future_question(self):
"""
Questions with a pub_date in the future should not be displayed on
the index page.
"""
question = self.create_question(question_text="Future question", days=30)
request = self.request_factory.get(reverse('polls:index'))
response = IndexView.as_view()(request)
self.assertNotContains(response, question.question_text)
def test_index_view_with_future_question_and_past_question(self):
"""
Even if both past and future questions exist, only past questions
should be displayed.
"""
past_question = self.create_question(question_text="Past question", days=-30)
future_question = self.create_question(question_text="Future question", days=30)
request = self.request_factory.get(reverse('polls:index'))
response = IndexView.as_view()(request)
self.assertContains(response, past_question.question_text)
self.assertNotContains(response, future_question.question_text)
def test_index_view_with_two_past_questions(self):
"""
The questions index page may display multiple questions.
"""
question1 = self.create_question(question_text="Past question", days=-30)
question2 = self.create_question(question_text="Past question", days=-5)
request = self.request_factory.get(reverse('polls:index'))
response = IndexView.as_view()(request)
self.assertContains(response, question1.question_text)
self.assertContains(response, question2.question_text)
class QuestionIndexDetailsTests(base.BaseTestCase):
def test_detail_view_with_a_future_question(self):
"""
The detail view of a question with a pub_date in the future should
return a 404 not found.
"""
future_question = self.create_question(question_text='Future question.', days=5)
request = self.request_factory.get(reverse('polls:detail', args=(future_question.id,)))
with self.assertRaises(Http404):
DetailView.as_view()(request, pk=future_question.id)
def test_detail_view_with_a_past_question(self):
"""
The detail view of a question with a pub_date in the past should
display the question's text.
"""
past_question = self.create_question(question_text='Past Question.', days=-5)
request = self.request_factory.get(reverse('polls:detail', args=(past_question.id,)))
response = DetailView.as_view()(request, pk=past_question.id)
self.assertContains(response, past_question.question_text)
class QuestionResultsTests(base.BaseTestCase):
def test_results_empty(self):
"""
The results view of a question should display choices and counts.
"""
question = self.create_question(question_text='Question.', days=-5)
choice1 = Choice.objects.create(
question=question,
choice_text='Choice 1',
votes=10,
)
choice2 = Choice.objects.create(
question=question,
choice_text='Choice 2',
votes=11,
)
request = self.request_factory.get(reverse('polls:results', args=(question.id,)))
response = ResultsView.as_view()(request, pk=question.id)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "<li>{} -- {} votes</li>".format(choice1.choice_text, choice1.votes))
self.assertContains(response, "<li>{} -- {} votes</li>".format(choice2.choice_text, choice2.votes))
class VoteTests(base.BaseTestCase):
def setUp(self):
super().setUp()
self.question = Question.objects.create(
question_text='Is this a test?',
pub_date=timezone.now()
)
self.question.save()
self.choice1 = Choice(
question=self.question,
choice_text='No',
votes=0,
)
self.choice1.save()
self.choice2 = Choice(
question=self.question,
choice_text='Yes',
votes=0,
)
self.choice2.save()
def test_view(self):
url = reverse('polls:vote', args=(self.question.id,))
request = self.request_factory.post(url, {'choice': self.choice2.id})
response = vote(request, self.question.id)
self.assertEqual(response.status_code, 302)
self.assertEqual(response.url, reverse('polls:results', args=(self.question.id,)))