@@ -171,12 +171,12 @@ Put the following in the ``tests.py`` file in the ``polls`` application:
171
171
from .models import Question
172
172
173
173
174
- class QuestionMethodTests (TestCase):
174
+ class QuestionModelTests (TestCase):
175
175
176
176
def test_was_published_recently_with_future_question(self):
177
177
"""
178
- was_published_recently() should return False for questions whose
179
- pub_date is in the future.
178
+ was_published_recently() returns False for questions whose pub_date
179
+ is in the future.
180
180
"""
181
181
time = timezone.now() + datetime.timedelta(days=30)
182
182
future_question = Question(pub_date=time)
@@ -200,7 +200,7 @@ and you'll see something like::
200
200
System check identified no issues (0 silenced).
201
201
F
202
202
======================================================================
203
- FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionMethodTests )
203
+ FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionModelTests )
204
204
----------------------------------------------------------------------
205
205
Traceback (most recent call last):
206
206
File "/path/to/mysite/polls/tests.py", line 16, in test_was_published_recently_with_future_question
@@ -282,17 +282,17 @@ more comprehensively:
282
282
283
283
def test_was_published_recently_with_old_question(self):
284
284
"""
285
- was_published_recently() should return False for questions whose
286
- pub_date is older than 1 day.
285
+ was_published_recently() returns False for questions whose pub_date
286
+ is older than 1 day.
287
287
"""
288
288
time = timezone.now() - datetime.timedelta(days=1)
289
289
old_question = Question(pub_date=time)
290
290
self.assertIs(old_question.was_published_recently(), False)
291
291
292
292
def test_was_published_recently_with_recent_question(self):
293
293
"""
294
- was_published_recently() should return True for questions whose
295
- pub_date is within the last day.
294
+ was_published_recently() returns True for questions whose pub_date
295
+ is within the last day.
296
296
"""
297
297
time = timezone.now() - datetime.timedelta(hours=23, minutes=59, seconds=59)
298
298
recent_question = Question(pub_date=time)
@@ -450,27 +450,27 @@ class:
450
450
451
451
def create_question(question_text, days):
452
452
"""
453
- Creates a question with the given `question_text` and published the
453
+ Create a question with the given `question_text` and published the
454
454
given number of `days` offset to now (negative for questions published
455
455
in the past, positive for questions that have yet to be published).
456
456
"""
457
457
time = timezone.now() + datetime.timedelta(days=days)
458
458
return Question.objects.create(question_text=question_text, pub_date=time)
459
459
460
460
461
- class QuestionViewTests (TestCase):
462
- def test_index_view_with_no_questions (self):
461
+ class QuestionIndexViewTests (TestCase):
462
+ def test_no_questions (self):
463
463
"""
464
- If no questions exist, an appropriate message should be displayed.
464
+ If no questions exist, an appropriate message is displayed.
465
465
"""
466
466
response = self.client.get(reverse('polls:index'))
467
467
self.assertEqual(response.status_code, 200)
468
468
self.assertContains(response, "No polls are available.")
469
469
self.assertQuerysetEqual(response.context['latest_question_list'], [])
470
470
471
- def test_index_view_with_a_past_question (self):
471
+ def test_past_question (self):
472
472
"""
473
- Questions with a pub_date in the past should be displayed on the
473
+ Questions with a pub_date in the past are displayed on the
474
474
index page.
475
475
"""
476
476
create_question(question_text="Past question.", days=-30)
@@ -480,20 +480,20 @@ class:
480
480
['<Question: Past question.>']
481
481
)
482
482
483
- def test_index_view_with_a_future_question (self):
483
+ def test_future_question (self):
484
484
"""
485
- Questions with a pub_date in the future should not be displayed on
485
+ Questions with a pub_date in the future aren't displayed on
486
486
the index page.
487
487
"""
488
488
create_question(question_text="Future question.", days=30)
489
489
response = self.client.get(reverse('polls:index'))
490
490
self.assertContains(response, "No polls are available.")
491
491
self.assertQuerysetEqual(response.context['latest_question_list'], [])
492
492
493
- def test_index_view_with_future_question_and_past_question (self):
493
+ def test_future_question_and_past_question (self):
494
494
"""
495
495
Even if both past and future questions exist, only past questions
496
- should be displayed.
496
+ are displayed.
497
497
"""
498
498
create_question(question_text="Past question.", days=-30)
499
499
create_question(question_text="Future question.", days=30)
@@ -503,7 +503,7 @@ class:
503
503
['<Question: Past question.>']
504
504
)
505
505
506
- def test_index_view_with_two_past_questions (self):
506
+ def test_two_past_questions (self):
507
507
"""
508
508
The questions index page may display multiple questions.
509
509
"""
@@ -521,20 +521,19 @@ Let's look at some of these more closely.
521
521
First is a question shortcut function, ``create_question``, to take some
522
522
repetition out of the process of creating questions.
523
523
524
- ``test_index_view_with_no_questions `` doesn't create any questions, but checks
525
- the message: "No polls are available." and verifies the ``latest_question_list``
526
- is empty. Note that the :class:`django.test.TestCase` class provides some
527
- additional assertion methods. In these examples, we use
524
+ ``test_no_questions `` doesn't create any questions, but checks the message:
525
+ "No polls are available." and verifies the ``latest_question_list`` is empty.
526
+ Note that the :class:`django.test.TestCase` class provides some additional
527
+ assertion methods. In these examples, we use
528
528
:meth:`~django.test.SimpleTestCase.assertContains()` and
529
529
:meth:`~django.test.TransactionTestCase.assertQuerysetEqual()`.
530
530
531
- In ``test_index_view_with_a_past_question ``, we create a question and verify that it
532
- appears in the list.
531
+ In ``test_past_question ``, we create a question and verify that it appears in
532
+ the list.
533
533
534
- In ``test_index_view_with_a_future_question``, we create a question with a
535
- ``pub_date`` in the future. The database is reset for each test method, so the
536
- first question is no longer there, and so again the index shouldn't have any
537
- questions in it.
534
+ In ``test_future_question``, we create a question with a ``pub_date`` in the
535
+ future. The database is reset for each test method, so the first question is no
536
+ longer there, and so again the index shouldn't have any questions in it.
538
537
539
538
And so on. In effect, we are using the tests to tell a story of admin input
540
539
and user experience on the site, and checking that at every state and for every
@@ -565,21 +564,21 @@ in the future is not:
565
564
.. snippet::
566
565
:filename: polls/tests.py
567
566
568
- class QuestionIndexDetailTests (TestCase):
569
- def test_detail_view_with_a_future_question (self):
567
+ class QuestionDetailViewTests (TestCase):
568
+ def test_future_question (self):
570
569
"""
571
- The detail view of a question with a pub_date in the future should
572
- return a 404 not found.
570
+ The detail view of a question with a pub_date in the future
571
+ returns a 404 not found.
573
572
"""
574
573
future_question = create_question(question_text='Future question.', days=5)
575
574
url = reverse('polls:detail', args=(future_question.id,))
576
575
response = self.client.get(url)
577
576
self.assertEqual(response.status_code, 404)
578
577
579
- def test_detail_view_with_a_past_question (self):
578
+ def test_past_question (self):
580
579
"""
581
- The detail view of a question with a pub_date in the past should
582
- display the question's text.
580
+ The detail view of a question with a pub_date in the past
581
+ displays the question's text.
583
582
"""
584
583
past_question = create_question(question_text='Past Question.', days=-5)
585
584
url = reverse('polls:detail', args=(past_question.id,))
0 commit comments