Skip to content

Commit 76603b5

Browse files
committed
test: Identify bug and test views
resolved #24 - 모델 날짜 버그 (날짜가 미래로 설정되어 있는데 '최근'으로 표시되는 현상) 테스트 - 모델 버그 수정 및 기능 추가 후 테스트 - 장고 테스트 클라이언트 사용 - 설문조사 목록에 아직 게시되지 않은 설문조사가 표시되는 버그 테스트 - 뷰 수정 및 디테일 뷰 제약조건 추가 후 테스트
1 parent 586b1fd commit 76603b5

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

mysite/polls/tests.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from django.test import TestCase
44
from django.utils import timezone
5+
from django.urls import reverse
56

67
from .models import Question
78

@@ -35,3 +36,93 @@ def test_was_published_recently_with_recent_question(self):
3536
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
3637
recent_question = Question(pub_date=time)
3738
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

Comments
 (0)