Coverage for polls/tests/test_views.py: 100.00%
88 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-08 13:12 -0700
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-08 13:12 -0700
1import datetime
3from django.contrib.auth import get_user_model
4from django.test import TestCase
5from django.urls import reverse
6from django.utils import timezone
8from ..models import Question
11def create_question(question_text, days):
12 """
13 Create a question with the given 'question_text' and published
14 the given number of 'days' offset to now (negative for questions
15 published in the past, positive for questions that have yet to
16 be published.
17 """
18 time = timezone.now() + datetime.timedelta(days=days) 1bchdiefg
19 return Question.objects.create(question_text=question_text, pub_date=time) 1bchdiefg
22class QuestionIndexViewTests(TestCase):
23 def setUp(self):
24 self.user = get_user_model().objects.create_user(
25 username="pollreader",
26 email="pollreader@example.com",
27 password="testpass123",
28 )
30 def test_question_index_view_for_logged_out_user(self):
31 response = self.client.get(reverse("poll_list")) 1j
32 self.assertEqual(response.status_code, 302) 1j
33 self.assertRedirects(response, "%s?next=/polls/" % (reverse("account_login"))) 1j
34 response = self.client.get("%s?next=/polls/" % (reverse("account_login"))) 1j
35 self.assertContains(response, "Log In") 1j
37 def test_no_questions(self):
38 """
39 If no questions exist, an appropriate message is displayed.
40 """
41 self.client.login(email="pollreader@example.com", password="testpass123") 1k
42 response = self.client.get(reverse("poll_list")) 1k
43 self.assertEqual(response.status_code, 200) 1k
44 self.assertContains(response, "No polls are currently available.") 1k
45 self.assertQuerySetEqual(response.context["latest_question_list"], []) 1k
47 def test_past_question(self):
48 """
49 Questions with a pub_date in the past are displayed on the polls page.
50 """
51 self.client.login(email="pollreader@example.com", password="testpass123") 1i
52 question = create_question(question_text="Past question.", days=-30) 1i
53 response = self.client.get(reverse("poll_list")) 1i
54 self.assertQuerySetEqual( 1i
55 response.context["latest_question_list"],
56 [question],
57 )
59 def test_future_question(self):
60 """
61 Questions with a pub_date in the future aren't
62 displayed on the polls page.
63 """
64 self.client.login(email="pollreader@example.com", password="testpass123") 1h
65 create_question(question_text="Future question.", days=30) 1h
66 response = self.client.get(reverse("poll_list")) 1h
67 self.assertQuerySetEqual(response.context["latest_question_list"], []) 1h
69 def test_future_question_and_past_question(self):
70 """
71 Even if both past and future questions exist, only past questions
72 are displayed.
73 """
74 self.client.login(email="pollreader@example.com", password="testpass123") 1d
75 question = create_question(question_text="Past question.", days=-30) 1d
76 create_question(question_text="Future question.", days=30) 1d
77 response = self.client.get(reverse("poll_list")) 1d
78 self.assertQuerySetEqual( 1d
79 response.context["latest_question_list"],
80 [question],
81 )
83 def test_two_past_questions(self):
84 """
85 The questions polls page may display multiple questions.
86 """
87 self.client.login(email="pollreader@example.com", password="testpass123") 1e
88 question1 = create_question(question_text="Past question 1.", days=-30) 1e
89 question2 = create_question(question_text="Past question 2.", days=-5) 1e
90 response = self.client.get(reverse("poll_list")) 1e
91 self.assertQuerySetEqual( 1e
92 response.context["latest_question_list"],
93 [question2, question1],
94 )
97class QuestionDetailViewTests(TestCase):
98 def setUp(self):
99 self.user = get_user_model().objects.create_user(
100 username="pollreader",
101 email="pollreader@example.com",
102 password="testpass123",
103 )
105 def test_question_detail_view_for_logged_out_user(self):
106 response = self.client.get(reverse("poll_list")) 1l
107 self.assertEqual(response.status_code, 302) 1l
108 self.assertRedirects(response, "%s?next=/polls/" % (reverse("account_login"))) 1l
109 response = self.client.get("%s?next=/polls/" % (reverse("account_login"))) 1l
110 self.assertContains(response, "Log In") 1l
112 def test_future_question(self):
113 """
114 The detail view of a question with a pub_date in the
115 future returns a 404 not found.
116 """
117 self.client.login(email="pollreader@example.com", password="testpass123") 1b
118 future_question = create_question(question_text="Future question.", days=5) 1b
119 url = reverse("poll_detail", args=(future_question.id,)) 1b
120 response = self.client.get(url) 1b
121 self.assertEqual(response.status_code, 404) 1b
123 def test_past_question(self):
124 """
125 The detail view of a question with a pub_date in the past
126 displays the question's text.
127 """
128 self.client.login(email="pollreader@example.com", password="testpass123") 1c
129 past_question = create_question(question_text="Past question.", days=-5) 1c
130 url = reverse("poll_detail", args=(past_question.id,)) 1c
131 response = self.client.get(url) 1c
132 self.assertContains(response, past_question.question_text) 1c
135class QuestionResultsViewTests(TestCase):
136 def setUp(self):
137 self.user = get_user_model().objects.create_user(
138 username="pollreader",
139 email="pollreader@example.com",
140 password="testpass123",
141 )
143 def test_question_results_view_for_logged_out_user(self):
144 response = self.client.get(reverse("poll_list")) 1m
145 self.assertEqual(response.status_code, 302) 1m
146 self.assertRedirects(response, "%s?next=/polls/" % (reverse("account_login"))) 1m
147 response = self.client.get("%s?next=/polls/" % (reverse("account_login"))) 1m
148 self.assertContains(response, "Log In") 1m
150 def test_future_question(self):
151 """
152 The results view of a question with a pub_date in the
153 future returns a 404 not found.
154 """
155 self.client.login(email="pollreader@example.com", password="testpass123") 1f
156 future_question = create_question(question_text="Future question.", days=5) 1f
157 url = reverse("poll_results", args=(future_question.id,)) 1f
158 response = self.client.get(url) 1f
159 self.assertEqual(response.status_code, 404) 1f
161 def test_past_question(self):
162 """
163 The results view of a question with a pub_date in the past
164 displays the question's results.
165 """
166 self.client.login(email="pollreader@example.com", password="testpass123") 1g
167 past_question = create_question(question_text="Past question.", days=-5) 1g
168 url = reverse("poll_results", args=(past_question.id,)) 1g
169 response = self.client.get(url) 1g
170 self.assertContains(response, past_question.question_text) 1g