From 7e2d0e2d46bf5dd4852a66deb28bbc869fdefd32 Mon Sep 17 00:00:00 2001 From: JasonHomeWorkstationUbuntu Date: Tue, 20 Oct 2020 21:19:51 +1100 Subject: [PATCH] 5.1 Comprehensive Tests --- first_django_app.md | 23 +++++++++++++++++++++++ src/polls/tests.py | 20 +++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/first_django_app.md b/first_django_app.md index ac8d309..74d7a0d 100644 --- a/first_django_app.md +++ b/first_django_app.md @@ -506,3 +506,26 @@ Fix bug in `polls/models.py` return now - datetime.timedelta(days=1) <= self.pub_date <= now ``` +#### Comprehensive Tests + +Write more tests to make sure it's comprehensive + +```python + def test_was_published_recently_with_old_question(self): + """ + was_published_recently() returns False for questions whose pub_date is older than 1 day. + """ + + time = timezone.now() - datetime.timedelta(days=1, seconds=1) + old_quesion = Question(pub_date=time) + self.assertIs(old_quesion.was_published_recently(), False) + + def test_was_published_recently_with_recent_question(self): + """ + was_published_recently() returns True for questions whose pub_date is within the last day. + """ + + time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59) + recent_question = Question(pub_date=time) + self.assertIs(recent_question.was_published_recently(), True) +``` \ No newline at end of file diff --git a/src/polls/tests.py b/src/polls/tests.py index 63ae988..3a221a0 100644 --- a/src/polls/tests.py +++ b/src/polls/tests.py @@ -14,4 +14,22 @@ class QuestionModelTests(TestCase): time = timezone.now() + datetime.timedelta(days=30) future_question = Question(pub_date=time) - self.assertIs(future_question.was_published_recently(), False) \ No newline at end of file + self.assertIs(future_question.was_published_recently(), False) + + def test_was_published_recently_with_old_question(self): + """ + was_published_recently() returns False for questions whose pub_date is older than 1 day. + """ + + time = timezone.now() - datetime.timedelta(days=1, seconds=1) + old_quesion = Question(pub_date=time) + self.assertIs(old_quesion.was_published_recently(), False) + + def test_was_published_recently_with_recent_question(self): + """ + was_published_recently() returns True for questions whose pub_date is within the last day. + """ + + time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59) + recent_question = Question(pub_date=time) + self.assertIs(recent_question.was_published_recently(), True) \ No newline at end of file