|
1 | 1 | import datetime
|
2 | 2 |
|
| 3 | +from django.core.urlresolvers import reverse |
3 | 4 | from django.utils import timezone
|
4 | 5 | from django.test import TestCase
|
5 | 6 |
|
@@ -34,3 +35,73 @@ def test_was_published_recently_with_recent_question(self):
|
34 | 35 | time = timezone.now() - datetime.timedelta(hours=1)
|
35 | 36 | recent_question = Question(pub_date=time)
|
36 | 37 | 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