Skip to content

Commit 1172d90

Browse files
committed
Testing the DetailView
modified: mysite/polls/tests.py modified: mysite/polls/views.py https://docs.djangoproject.com/en/1.8/intro/tutorial05/#testing-the-detailview
1 parent 7775ae9 commit 1172d90

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

mysite/polls/tests.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,28 @@ def test_index_view_with_two_past_questions(self):
105105
response.context['latest_question_list'],
106106
['<Question: Past question 2.>', '<Question: Past question 1.>']
107107
)
108+
109+
110+
class QuestionIndexDetailTests(TestCase):
111+
def test_detail_view_with_a_future_question(self):
112+
"""
113+
The detail view of a question with a pub_date in the future should
114+
return a 404 not found.
115+
"""
116+
future_question = create_question(question_text='Future question.',
117+
days=5)
118+
response = self.client.get(reverse('polls:detail',
119+
args=(future_question.id,)))
120+
self.assertEqual(response.status_code, 404)
121+
122+
def test_detail_view_with_a_past_question(self):
123+
"""
124+
The detail view of a question with a pub_date in the past should
125+
display the question's text.
126+
"""
127+
past_question = create_question(question_text='Past Question.',
128+
days=-5)
129+
response = self.client.get(reverse('polls:detail',
130+
args=(past_question.id,)))
131+
self.assertContains(response, past_question.question_text,
132+
status_code=200)

mysite/polls/views.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ class DetailView(generic.DetailView):
2525
model = Question
2626
template_name = 'polls/detail.html'
2727

28+
def get_queryset(self):
29+
"""
30+
Excludes any questions that aren't published yet.
31+
"""
32+
return Question.objects.filter(pub_date__lte=timezone.now())
33+
2834

2935
class ResultsView(generic.DetailView):
3036
model = Question

0 commit comments

Comments
 (0)