From d69c7eab041d30c1cf8a4ee8d6426d47e90254b1 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 1 Oct 2010 02:01:20 +0000 Subject: [PATCH] Fixed #13897 -- Added tests for pagination feature of the generic object_list view. Thanks, d0ugal and SmileyChris. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13965 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/regressiontests/views/tests/__init__.py | 1 + .../views/tests/generic/object_list.py | 36 +++++++++++++++++++ tests/regressiontests/views/urls.py | 16 +++++++++ tests/templates/views/article_list.html | 1 + 4 files changed, 54 insertions(+) create mode 100644 tests/regressiontests/views/tests/generic/object_list.py create mode 100644 tests/templates/views/article_list.html diff --git a/tests/regressiontests/views/tests/__init__.py b/tests/regressiontests/views/tests/__init__.py index edd533e175..bc28f3e9d0 100644 --- a/tests/regressiontests/views/tests/__init__.py +++ b/tests/regressiontests/views/tests/__init__.py @@ -2,6 +2,7 @@ from debug import * from defaults import * from generic.create_update import * from generic.date_based import * +from generic.object_list import * from generic.simple import * from i18n import * from specials import * diff --git a/tests/regressiontests/views/tests/generic/object_list.py b/tests/regressiontests/views/tests/generic/object_list.py new file mode 100644 index 0000000000..4f902eeca9 --- /dev/null +++ b/tests/regressiontests/views/tests/generic/object_list.py @@ -0,0 +1,36 @@ +from django.test import TestCase + + +class ObjectListTest(TestCase): + fixtures = ['testdata.json'] + + def check_pagination(self, url, expected_status_code, object_count=None): + response = self.client.get(url) + self.assertEqual(response.status_code, expected_status_code) + + if object_count: + self.assertEqual(response.context['is_paginated'], True) + self.assertEqual(len(response.context['page_obj'].object_list), + object_count) + + return response + + def test_finds_pages(self): + # Check page count doesn't start at 0. + self.check_pagination('/views/object_list/page0/', 404) + + # Check basic pages. + self.check_pagination('/views/object_list/page/', 200, 2) + self.check_pagination('/views/object_list/page1/', 200, 2) + self.check_pagination('/views/object_list/page2/', 200, 1) + self.check_pagination('/views/object_list/page3/', 404) + + # Check the special "last" page. + self.check_pagination('/views/object_list/pagelast/', 200, 1) + self.check_pagination('/views/object_list/pagenotlast/', 404) + + def test_no_paginate_by(self): + # Ensure that the view isn't paginated by default. + url = '/views/object_list_no_paginate_by/page1/' + response = self.check_pagination(url, 200) + self.assertEqual(response.context['is_paginated'], False) diff --git a/tests/regressiontests/views/urls.py b/tests/regressiontests/views/urls.py index b42700baeb..d1412cdeff 100644 --- a/tests/regressiontests/views/urls.py +++ b/tests/regressiontests/views/urls.py @@ -31,6 +31,16 @@ date_based_info_dict = { 'date_field': 'date_created', 'month_format': '%m', } + +object_list_dict = { + 'queryset': Article.objects.all(), + 'paginate_by': 2, +} + +object_list_no_paginate_by = { + 'queryset': Article.objects.all(), +} + numeric_days_info_dict = dict(date_based_info_dict, day_format='%d') date_based_datefield_info_dict = dict(date_based_info_dict, queryset=DateArticle.objects.all()) @@ -104,6 +114,12 @@ urlpatterns += patterns('django.views.generic.create_update', 'update_object', dict(slug_field='slug', model=UrlArticle)), ) +urlpatterns += patterns('django.views.generic.list_detail', + (r'^object_list/page(?P[\w]*)/$', 'object_list', object_list_dict), + (r'^object_list_no_paginate_by/page(?P[0-9]+)/$', 'object_list', + object_list_no_paginate_by), +) + # a view that raises an exception for the debug view urlpatterns += patterns('', (r'^raises/$', views.raises), diff --git a/tests/templates/views/article_list.html b/tests/templates/views/article_list.html new file mode 100644 index 0000000000..3840895aa4 --- /dev/null +++ b/tests/templates/views/article_list.html @@ -0,0 +1 @@ +{{ object_list }} \ No newline at end of file