From 4c13b907023692f75f37ba980cf2215e413aa1d8 Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Fri, 26 Oct 2018 17:10:17 +0200 Subject: [PATCH] Added test coverage for views.generic.dates.MonthMixin.get_month() KeyError branch. --- tests/generic_views/test_dates.py | 14 ++++++++++++++ tests/generic_views/urls.py | 1 + 2 files changed, 15 insertions(+) diff --git a/tests/generic_views/test_dates.py b/tests/generic_views/test_dates.py index 6a18e090e6..2daac10b31 100644 --- a/tests/generic_views/test_dates.py +++ b/tests/generic_views/test_dates.py @@ -408,6 +408,20 @@ class MonthArchiveViewTests(TestDataMixin, TestCase): res = self.client.get('/dates/booksignings/2008/apr/') 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') @override_settings(USE_TZ=True, TIME_ZONE='Africa/Nairobi') def test_aware_datetime_month_view(self): diff --git a/tests/generic_views/urls.py b/tests/generic_views/urls.py index 1c1fed3386..f2af8ad1a3 100644 --- a/tests/generic_views/urls.py +++ b/tests/generic_views/urls.py @@ -168,6 +168,7 @@ urlpatterns = [ # MonthArchiveView path('dates/books///', views.BookMonthArchive.as_view(month_format='%m')), path('dates/books///', views.BookMonthArchive.as_view()), + path('dates/books/without_month//', views.BookMonthArchive.as_view()), path('dates/books///allow_empty/', views.BookMonthArchive.as_view(allow_empty=True)), path('dates/books///allow_future/', views.BookMonthArchive.as_view(allow_future=True)), path('dates/books///paginated/', views.BookMonthArchive.as_view(paginate_by=30)),