Selaa lähdekoodia

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
Jannis Leidel 14 vuotta sitten
vanhempi
commit
d69c7eab04

+ 1 - 0
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 *

+ 36 - 0
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)

+ 16 - 0
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<page>[\w]*)/$', 'object_list', object_list_dict),
+    (r'^object_list_no_paginate_by/page(?P<page>[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),

+ 1 - 0
tests/templates/views/article_list.html

@@ -0,0 +1 @@
+{{ object_list }}