Added test coverage for views.generic.dates.MonthMixin.get_month() KeyError branch.

This commit is contained in:
Hasan Ramezani 2018-10-26 17:10:17 +02:00 committed by Tim Graham
parent f892781b95
commit 4c13b90702
2 changed files with 15 additions and 0 deletions

View File

@ -408,6 +408,20 @@ class MonthArchiveViewTests(TestDataMixin, TestCase):
res = self.client.get('/dates/booksignings/2008/apr/') res = self.client.get('/dates/booksignings/2008/apr/')
self.assertEqual(res.status_code, 200) self.assertEqual(res.status_code, 200)
def test_month_view_get_month_from_request(self):
oct1 = datetime.date(2008, 10, 1)
res = self.client.get('/dates/books/without_month/2008/?month=oct')
self.assertEqual(res.status_code, 200)
self.assertTemplateUsed(res, 'generic_views/book_archive_month.html')
self.assertEqual(list(res.context['date_list']), [oct1])
self.assertEqual(list(res.context['book_list']), list(Book.objects.filter(pubdate=oct1)))
self.assertEqual(res.context['month'], oct1)
def test_month_view_without_month_in_url(self):
res = self.client.get('/dates/books/without_month/2008/')
self.assertEqual(res.status_code, 404)
self.assertEqual(res.context['exception'], 'No month specified')
@skipUnlessDBFeature('has_zoneinfo_database') @skipUnlessDBFeature('has_zoneinfo_database')
@override_settings(USE_TZ=True, TIME_ZONE='Africa/Nairobi') @override_settings(USE_TZ=True, TIME_ZONE='Africa/Nairobi')
def test_aware_datetime_month_view(self): def test_aware_datetime_month_view(self):

View File

@ -168,6 +168,7 @@ urlpatterns = [
# MonthArchiveView # MonthArchiveView
path('dates/books/<int:year>/<int:month>/', views.BookMonthArchive.as_view(month_format='%m')), path('dates/books/<int:year>/<int:month>/', views.BookMonthArchive.as_view(month_format='%m')),
path('dates/books/<int:year>/<month>/', views.BookMonthArchive.as_view()), path('dates/books/<int:year>/<month>/', views.BookMonthArchive.as_view()),
path('dates/books/without_month/<int:year>/', views.BookMonthArchive.as_view()),
path('dates/books/<int:year>/<month>/allow_empty/', views.BookMonthArchive.as_view(allow_empty=True)), path('dates/books/<int:year>/<month>/allow_empty/', views.BookMonthArchive.as_view(allow_empty=True)),
path('dates/books/<int:year>/<month>/allow_future/', views.BookMonthArchive.as_view(allow_future=True)), path('dates/books/<int:year>/<month>/allow_future/', views.BookMonthArchive.as_view(allow_future=True)),
path('dates/books/<int:year>/<month>/paginated/', views.BookMonthArchive.as_view(paginate_by=30)), path('dates/books/<int:year>/<month>/paginated/', views.BookMonthArchive.as_view(paginate_by=30)),