Coverage for polls/tests/test_views.py: 100.00%

88 statements  

« prev     ^ index     » next       coverage.py v7.17.5, created at 2026-11-25 06:29 -0800

1import datetime 1a

2 

3from django.contrib.auth import get_user_model 1a

4from django.test import TestCase 1a

5from django.urls import reverse 1a

6from django.utils import timezone 1a

7 

8from ..models import Question 1a

9 

10 

11def create_question(question_text, days): 1a

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) 1a

19 return Question.objects.create(question_text=question_text, pub_date=time) 1a

20 

21 

22class QuestionIndexViewTests(TestCase): 1a

23 def setUp(self): 1a

24 self.user = get_user_model().objects.create_user( 1a

25 username="pollreader", 

26 email="pollreader@example.com", 

27 password="testpass123", 

28 ) 

29 

30 def test_question_index_view_for_logged_out_user(self): 1a

31 response = self.client.get(reverse("poll_list")) 1a

32 self.assertEqual(response.status_code, 302) 1a

33 self.assertRedirects( 1a

34 response, "{}?next=/polls/".format(reverse("account_login")) 

35 ) 

36 response = self.client.get("{}?next=/polls/".format(reverse("account_login"))) 1a

37 self.assertContains(response, "Log In") 1a

38 

39 def test_no_questions(self): 1a

40 """ 

41 If no questions exist, an appropriate message is displayed. 

42 """ 

43 self.client.login(email="pollreader@example.com", password="testpass123") 1a

44 response = self.client.get(reverse("poll_list")) 1a

45 self.assertEqual(response.status_code, 200) 1a

46 self.assertContains(response, "No polls are currently available.") 1a

47 self.assertQuerySetEqual(response.context["latest_question_list"], []) 1a

48 

49 def test_past_question(self): 1a

50 """ 

51 Questions with a pub_date in the past are displayed on the polls page. 

52 """ 

53 self.client.login(email="pollreader@example.com", password="testpass123") 1a

54 question = create_question(question_text="Past question.", days=-30) 1a

55 response = self.client.get(reverse("poll_list")) 1a

56 self.assertQuerySetEqual( 1a

57 response.context["latest_question_list"], 

58 [question], 

59 ) 

60 

61 def test_future_question(self): 1a

62 """ 

63 Questions with a pub_date in the future aren't 

64 displayed on the polls page. 

65 """ 

66 self.client.login(email="pollreader@example.com", password="testpass123") 1a

67 create_question(question_text="Future question.", days=30) 1a

68 response = self.client.get(reverse("poll_list")) 1a

69 self.assertQuerySetEqual(response.context["latest_question_list"], []) 1a

70 

71 def test_future_question_and_past_question(self): 1a

72 """ 

73 Even if both past and future questions exist, only past questions 

74 are displayed. 

75 """ 

76 self.client.login(email="pollreader@example.com", password="testpass123") 1a

77 question = create_question(question_text="Past question.", days=-30) 1a

78 create_question(question_text="Future question.", days=30) 1a

79 response = self.client.get(reverse("poll_list")) 1a

80 self.assertQuerySetEqual( 1a

81 response.context["latest_question_list"], 

82 [question], 

83 ) 

84 

85 def test_two_past_questions(self): 1a

86 """ 

87 The questions polls page may display multiple questions. 

88 """ 

89 self.client.login(email="pollreader@example.com", password="testpass123") 1a

90 question1 = create_question(question_text="Past question 1.", days=-30) 1a

91 question2 = create_question(question_text="Past question 2.", days=-5) 1a

92 response = self.client.get(reverse("poll_list")) 1a

93 self.assertQuerySetEqual( 1a

94 response.context["latest_question_list"], 

95 [question2, question1], 

96 ) 

97 

98 

99class QuestionDetailViewTests(TestCase): 1a

100 def setUp(self): 1a

101 self.user = get_user_model().objects.create_user( 1a

102 username="pollreader", 

103 email="pollreader@example.com", 

104 password="testpass123", 

105 ) 

106 

107 def test_question_detail_view_for_logged_out_user(self): 1a

108 response = self.client.get(reverse("poll_list")) 1a

109 self.assertEqual(response.status_code, 302) 1a

110 self.assertRedirects( 1a

111 response, "{}?next=/polls/".format(reverse("account_login")) 

112 ) 

113 response = self.client.get("{}?next=/polls/".format(reverse("account_login"))) 1a

114 self.assertContains(response, "Log In") 1a

115 

116 def test_future_question(self): 1a

117 """ 

118 The detail view of a question with a pub_date in the 

119 future returns a 404 not found. 

120 """ 

121 self.client.login(email="pollreader@example.com", password="testpass123") 1a

122 future_question = create_question(question_text="Future question.", days=5) 1a

123 url = reverse("poll_detail", args=(future_question.id,)) 1a

124 response = self.client.get(url) 1a

125 self.assertEqual(response.status_code, 404) 1a

126 

127 def test_past_question(self): 1a

128 """ 

129 The detail view of a question with a pub_date in the past 

130 displays the question's text. 

131 """ 

132 self.client.login(email="pollreader@example.com", password="testpass123") 1a

133 past_question = create_question(question_text="Past question.", days=-5) 1a

134 url = reverse("poll_detail", args=(past_question.id,)) 1a

135 response = self.client.get(url) 1a

136 self.assertContains(response, past_question.question_text) 1a

137 

138 

139class QuestionResultsViewTests(TestCase): 1a

140 def setUp(self): 1a

141 self.user = get_user_model().objects.create_user( 1a

142 username="pollreader", 

143 email="pollreader@example.com", 

144 password="testpass123", 

145 ) 

146 

147 def test_question_results_view_for_logged_out_user(self): 1a

148 response = self.client.get(reverse("poll_list")) 1a

149 self.assertEqual(response.status_code, 302) 1a

150 self.assertRedirects( 1a

151 response, "{}?next=/polls/".format(reverse("account_login")) 

152 ) 

153 response = self.client.get("{}?next=/polls/".format(reverse("account_login"))) 1a

154 self.assertContains(response, "Log In") 1a

155 

156 def test_future_question(self): 1a

157 """ 

158 The results view of a question with a pub_date in the 

159 future returns a 404 not found. 

160 """ 

161 self.client.login(email="pollreader@example.com", password="testpass123") 1a

162 future_question = create_question(question_text="Future question.", days=5) 1a

163 url = reverse("poll_results", args=(future_question.id,)) 1a

164 response = self.client.get(url) 1a

165 self.assertEqual(response.status_code, 404) 1a

166 

167 def test_past_question(self): 1a

168 """ 

169 The results view of a question with a pub_date in the past 

170 displays the question's results. 

171 """ 

172 self.client.login(email="pollreader@example.com", password="testpass123") 1a

173 past_question = create_question(question_text="Past question.", days=-5) 1a

174 url = reverse("poll_results", args=(past_question.id,)) 1a

175 response = self.client.get(url) 1a

176 self.assertContains(response, past_question.question_text) 1a