diff --git a/first_django_app.md b/first_django_app.md index 576fb42..98cf6b7 100644 --- a/first_django_app.md +++ b/first_django_app.md @@ -544,7 +544,7 @@ Django provides a test **Client** (a class) to simulate a user interacting with We can try it in interactive shell following the guide -#### Improving our view +#### Test and Improve our view Add following in `tests.py` @@ -665,3 +665,29 @@ Destroying test database for alias 'default'... Fix it by change `get_queryset()` method in Question. 让他它能通过将 Question 的 pub_data 属性与 timezone.now() 相比较来判断是否应该显示此 Question +#### Testing the DetailView + +Target: hide future questions even if user type correct URL + +Add test cases first + +```python +class QuestionDetailViewTests(TestCase): + def test_future_question(self): + """ + The detail view of a question with a pub_date in the future returns a 404 not found. + """ + future_question = create_question(question_text="Future question.", days=5) + url = reverse('poll:detail', args=(future_question.id, )) + response = self.client.get(url) + self.assertEqual(response.status_code, 404) + + def test_past_questioin(self): + """ + The detail view of a question with a pub_date in the past displays the question's text. + """ + past_question = create_question(question_text='Past Question.', days=-5) + url = reverse('polls:detail', args=(past_question.id,)) + response = self.client.get(url) + self.assertContains(response, past_question.question_text) +``` \ No newline at end of file diff --git a/src/polls/tests.py b/src/polls/tests.py index 7062709..5acacfb 100644 --- a/src/polls/tests.py +++ b/src/polls/tests.py @@ -1,4 +1,6 @@ import datetime +from django.conf.urls import url +from django.http import response from django.test import TestCase from django.utils import timezone @@ -100,4 +102,23 @@ class QuestionIndexViewTests(TestCase): self.assertQuerysetEqual( response.context['latest_question_list'], ['', ''] - ) \ No newline at end of file + ) + +class QuestionDetailViewTests(TestCase): + def test_future_question(self): + """ + The detail view of a question with a pub_date in the future returns a 404 not found. + """ + future_question = create_question(question_text="Future question.", days=5) + url = reverse('polls:detail', args=(future_question.id, )) + response = self.client.get(url) + self.assertEqual(response.status_code, 404) + + def test_past_questioin(self): + """ + The detail view of a question with a pub_date in the past displays the question's text. + """ + past_question = create_question(question_text='Past Question.', days=-5) + url = reverse('polls:detail', args=(past_question.id,)) + response = self.client.get(url) + self.assertContains(response, past_question.question_text) \ No newline at end of file diff --git a/src/polls/views.py b/src/polls/views.py index 7d6e42c..692a93d 100644 --- a/src/polls/views.py +++ b/src/polls/views.py @@ -24,6 +24,12 @@ class DetailView(generic.DetailView): model = Question template_name = 'polls/detail.html' + def get_queryset(self): + """ + Excludes any questions that aren't published yet + """ + return Question.objects.filter(pub_date__lte=timezone.now()) + class ResultsView(generic.DetailView): model = Question template_name = 'polls/results.html'