5.1 Comprehensive Tests

master
Jason Zhu 2020-10-20 21:19:51 +11:00
parent 07b5806efa
commit 7e2d0e2d46
2 changed files with 42 additions and 1 deletions

View File

@ -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)
```

View File

@ -15,3 +15,21 @@ class QuestionModelTests(TestCase):
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
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)