|
2 | 2 |
|
3 | 3 | from django.test import TestCase
|
4 | 4 | from django.utils import timezone
|
| 5 | +from django.urls import reverse |
5 | 6 |
|
6 | 7 | from .models import Question
|
7 | 8 |
|
@@ -35,3 +36,93 @@ def test_was_published_recently_with_recent_question(self):
|
35 | 36 | time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
|
36 | 37 | recent_question = Question(pub_date=time)
|
37 | 38 | self.assertIs(recent_question.was_published_recently(), True)
|
| 39 | + |
| 40 | + |
| 41 | +def create_question(question_text, days): |
| 42 | + """ |
| 43 | + Create a question with the given `question_text` and published the |
| 44 | + given number of `days` offset to now (negative for questions published |
| 45 | + in the past, positive for questions that have yet to be published). |
| 46 | + """ |
| 47 | + time = timezone.now() + datetime.timedelta(days=days) |
| 48 | + return Question.objects.create(question_text=question_text, pub_date=time) |
| 49 | + |
| 50 | + |
| 51 | +class QuestionIndexViewTests(TestCase): |
| 52 | + def test_no_questions(self): |
| 53 | + """ |
| 54 | + If no questions exist, an appropriate message is 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_past_question(self): |
| 62 | + """ |
| 63 | + Questions with a pub_date in the past are displayed on the |
| 64 | + index page. |
| 65 | + """ |
| 66 | + question = 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], |
| 71 | + ) |
| 72 | + |
| 73 | + def test_future_question(self): |
| 74 | + """ |
| 75 | + Questions with a pub_date in the future aren't 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 | + self.assertQuerysetEqual(response.context['latest_question_list'], []) |
| 82 | + |
| 83 | + def test_future_question_and_past_question(self): |
| 84 | + """ |
| 85 | + Even if both past and future questions exist, only past questions |
| 86 | + are displayed. |
| 87 | + """ |
| 88 | + question = create_question(question_text="Past question.", days=-30) |
| 89 | + create_question(question_text="Future question.", days=30) |
| 90 | + response = self.client.get(reverse('polls:index')) |
| 91 | + self.assertQuerysetEqual( |
| 92 | + response.context['latest_question_list'], |
| 93 | + [question], |
| 94 | + ) |
| 95 | + |
| 96 | + def test_two_past_questions(self): |
| 97 | + """ |
| 98 | + The questions index page may display multiple questions. |
| 99 | + """ |
| 100 | + question1 = create_question(question_text="Past question 1.", days=-30) |
| 101 | + question2 = create_question(question_text="Past question 2.", days=-5) |
| 102 | + response = self.client.get(reverse('polls:index')) |
| 103 | + self.assertQuerysetEqual( |
| 104 | + response.context['latest_question_list'], |
| 105 | + [question2, question1], |
| 106 | + ) |
| 107 | + |
| 108 | + |
| 109 | +class QuestionDetailViewTests(TestCase): |
| 110 | + def test_future_question(self): |
| 111 | + """ |
| 112 | + The detail view of a question with a pub_date in the future |
| 113 | + returns a 404 not found. |
| 114 | + """ |
| 115 | + future_question = create_question(question_text='Future question.', days=5) |
| 116 | + url = reverse('polls:detail', args=(future_question.id,)) |
| 117 | + response = self.client.get(url) |
| 118 | + self.assertEqual(response.status_code, 404) |
| 119 | + |
| 120 | + def test_past_question(self): |
| 121 | + """ |
| 122 | + The detail view of a question with a pub_date in the past |
| 123 | + displays the question's text. |
| 124 | + """ |
| 125 | + past_question = create_question(question_text='Past Question.', days=-5) |
| 126 | + url = reverse('polls:detail', args=(past_question.id,)) |
| 127 | + response = self.client.get(url) |
| 128 | + self.assertContains(response, past_question.question_text) |
0 commit comments