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

35 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.utils import timezone 1a

6 

7from ..models import Choice, Question 1a

8 

9 

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

19 

20 

21def create_choice(question, choice_text, votes): 1a

22 return Choice.objects.create( 1a

23 question=question, choice_text=choice_text, votes=votes 

24 ) 

25 

26 

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 ) 

33 

34 def test___str__(self): 1a

35 assert str(self.choice) == self.choice.choice_text 1a

36 

37 

38class QuestionTests(TestCase): 1a

39 def setUp(self): 1a

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

41 username="johndoe", 

42 email="johndoe@example.com", 

43 password="secret", 

44 ) 

45 self.question = create_question(question_text="Current question.", days=0) 1a

46 

47 """ 1a

48 self.question = Question.objects.create( 

49 question_text="How much wood can a woodchuck chuck?", 

50 pub_date=timezone.now(), 

51 ) 

52 """ 

53 

54 def test___str__(self): 1a

55 assert str(self.question) == self.question.question_text 1a

56 

57 def test_was_published_recently_with_old_question(self): 1a

58 """ 

59 was_published_recently() returns False for 

60 questions whose pub_date is older than 1 day. 

61 """ 

62 time = timezone.now() - datetime.timedelta(days=1, seconds=1) 1a

63 old_question = Question(pub_date=time) 1a

64 self.assertIs(old_question.was_published_recently(), False) 1a

65 

66 def test_was_published_recently_with_recent_question(self): 1a

67 """ 

68 was_published_recently() returns True for 

69 questions whose pub_date is within the last day. 

70 """ 

71 time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59) 1a

72 recent_question = Question(pub_date=time) 1a

73 self.assertIs(recent_question.was_published_recently(), True) 1a

74 

75 def test_was_published_recently_with_future_question(self): 1a

76 """ 

77 was_published_recently() returns False for 

78 questions whose pub_date is in the future. 

79 """ 

80 time = timezone.now() + datetime.timedelta(days=30) 1a

81 future_question = Question(pub_date=time) 1a

82 

83 self.assertIs(future_question.was_published_recently(), False) 1a