Coverage for polls/tests/test_models.py: 100.00%
37 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.utils import timezone
7from ..models import Choice, Question
10def create_question(question_text, days):
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)
18 return Question.objects.create(question_text=question_text, pub_date=time)
21def create_choice(question, choice_text, votes):
22 return Choice.objects.create(
23 question=question, choice_text=choice_text, votes=votes
24 )
27class ChoiceTests(TestCase):
28 def setUp(self):
29 self.question = create_question(question_text="Current question.", days=0)
30 self.choice = create_choice(
31 question=self.question, choice_text="Seven logs", votes=3
32 )
34 def test___str__(self):
35 assert self.choice.__str__() == self.choice.choice_text 1e
36 assert str(self.choice) == self.choice.choice_text 1e
39class QuestionTests(TestCase):
40 def setUp(self):
41 self.user = get_user_model().objects.create_user(
42 username="johndoe",
43 email="johndoe@example.com",
44 password="secret",
45 )
46 self.question = create_question(question_text="Current question.", days=0)
48 """
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):
56 assert self.question.__str__() == self.question.question_text 1f
57 assert str(self.question) == self.question.question_text 1f
59 def test_was_published_recently_with_old_question(self):
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) 1b
65 old_question = Question(pub_date=time) 1b
66 self.assertIs(old_question.was_published_recently(), False) 1b
68 def test_was_published_recently_with_recent_question(self):
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) 1c
74 recent_question = Question(pub_date=time) 1c
75 self.assertIs(recent_question.was_published_recently(), True) 1c
77 def test_was_published_recently_with_future_question(self):
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) 1d
83 future_question = Question(pub_date=time) 1d
85 self.assertIs(future_question.was_published_recently(), False) 1d