Skip to content

Commit 7775ae9

Browse files
committed
Testing our new view
modified: mysite/polls/tests.py https://docs.djangoproject.com/en/1.8/intro/tutorial05/#testing-our-new-view
1 parent 5e70d58 commit 7775ae9

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

mysite/polls/tests.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22

3+
from django.core.urlresolvers import reverse
34
from django.utils import timezone
45
from django.test import TestCase
56

@@ -34,3 +35,73 @@ def test_was_published_recently_with_recent_question(self):
3435
time = timezone.now() - datetime.timedelta(hours=1)
3536
recent_question = Question(pub_date=time)
3637
self.assertEqual(recent_question.was_published_recently(), True)
38+
39+
40+
def create_question(question_text, days):
41+
"""
42+
Creates a question with the given `question_text` published the given
43+
number of `days` offset to now (negative for questions published
44+
in the past, positive for questions that have yet to be published).
45+
"""
46+
time = timezone.now() + datetime.timedelta(days=days)
47+
return Question.objects.create(question_text=question_text,
48+
pub_date=time)
49+
50+
51+
class QuestionViewTests(TestCase):
52+
def test_index_view_with_no_questions(self):
53+
"""
54+
If no questions exist, an appropriate message should be displayed.
55+
"""
56+
response = self.client.get(reverse('polls:index'))
57+
self.assertEqual(response.status_code, 200)
58+
self.assertContains(response, "No polls are available.")
59+
self.assertQuerysetEqual(response.context['latest_question_list'], [])
60+
61+
def test_index_view_with_a_past_question(self):
62+
"""
63+
Questions with a pub_date in the past should be displayed on the
64+
index page.
65+
"""
66+
create_question(question_text="Past question.", days=-30)
67+
response = self.client.get(reverse('polls:index'))
68+
self.assertQuerysetEqual(
69+
response.context['latest_question_list'],
70+
['<Question: Past question.>']
71+
)
72+
73+
def test_index_view_with_a_future_question(self):
74+
"""
75+
Questions with a pub_date in the future should not be displayed on
76+
the index page.
77+
"""
78+
create_question(question_text="Future question.", days=30)
79+
response = self.client.get(reverse('polls:index'))
80+
self.assertContains(response, "No polls are available.",
81+
status_code=200)
82+
self.assertQuerysetEqual(response.context['latest_question_list'], [])
83+
84+
def test_index_view_with_future_question_and_past_question(self):
85+
"""
86+
Even if both past and future questions exist, only past questions
87+
should be displayed.
88+
"""
89+
create_question(question_text="Past question.", days=-30)
90+
create_question(question_text="Future question.", days=30)
91+
response = self.client.get(reverse('polls:index'))
92+
self.assertQuerysetEqual(
93+
response.context['latest_question_list'],
94+
['<Question: Past question.>']
95+
)
96+
97+
def test_index_view_with_two_past_questions(self):
98+
"""
99+
The questions index page may display multiple questions.
100+
"""
101+
create_question(question_text="Past question 1.", days=-30)
102+
create_question(question_text="Past question 2.", days=-5)
103+
response = self.client.get(reverse('polls:index'))
104+
self.assertQuerysetEqual(
105+
response.context['latest_question_list'],
106+
['<Question: Past question 2.>', '<Question: Past question 1.>']
107+
)

0 commit comments

Comments
 (0)