Coverage for polls/tests/test_models.py: 100.00%
37 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 10:02 -0700
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 10:02 -0700
1import datetime 1a
3from django.contrib.auth import get_user_model 1a
4from django.test import TestCase 1a
5from django.utils import timezone 1a
7from ..models import Choice, Question 1a
10def create_question(question_text, days): 1a
11 """
12 Create a question with the given 'question_text' and published
13 the given number of 'days' offset to now (negative for questions
14 published in the past, positive for questions that have yet to
15 be published.
16 """
17 time = timezone.now() + datetime.timedelta(days=days) 1a
18 return Question.objects.create(question_text=question_text, pub_date=time) 1a
21def create_choice(question, choice_text, votes): 1a
22 return Choice.objects.create( 1a
23 question=question, choice_text=choice_text, votes=votes
24 )
27class ChoiceTests(TestCase): 1a
28 def setUp(self): 1a
29 self.question = create_question(question_text="Current question.", days=0) 1a
30 self.choice = create_choice( 1a
31 question=self.question, choice_text="Seven logs", votes=3
32 )
34 def test___str__(self): 1a
35 assert self.choice.__str__() == self.choice.choice_text 1a
36 assert str(self.choice) == self.choice.choice_text 1a
39class QuestionTests(TestCase): 1a
40 def setUp(self): 1a
41 self.user = get_user_model().objects.create_user( 1a
42 username="johndoe",
43 email="johndoe@example.com",
44 password="secret",
45 )
46 self.question = create_question(question_text="Current question.", days=0) 1a
48 """ 1a
49 self.question = Question.objects.create(
50 question_text="How much wood can a woodchuck chuck?",
51 pub_date=timezone.now(),
52 )
53 """
55 def test___str__(self): 1a
56 assert self.question.__str__() == self.question.question_text 1a
57 assert str(self.question) == self.question.question_text 1a
59 def test_was_published_recently_with_old_question(self): 1a
60 """
61 was_published_recently() returns False for
62 questions whose pub_date is older than 1 day.
63 """
64 time = timezone.now() - datetime.timedelta(days=1, seconds=1) 1a
65 old_question = Question(pub_date=time) 1a
66 self.assertIs(old_question.was_published_recently(), False) 1a
68 def test_was_published_recently_with_recent_question(self): 1a
69 """
70 was_published_recently() returns True for
71 questions whose pub_date is within the last day.
72 """
73 time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59) 1a
74 recent_question = Question(pub_date=time) 1a
75 self.assertIs(recent_question.was_published_recently(), True) 1a
77 def test_was_published_recently_with_future_question(self): 1a
78 """
79 was_published_recently() returns False for
80 questions whose pub_date is in the future.
81 """
82 time = timezone.now() + datetime.timedelta(days=30) 1a
83 future_question = Question(pub_date=time) 1a
85 self.assertIs(future_question.was_published_recently(), False) 1a