PageRenderTime 55ms CodeModel.GetById 38ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/modeltests/pagination/tests.py

https://code.google.com/p/mango-py/
Python | 132 lines | 104 code | 19 blank | 9 comment | 4 complexity | 6a1f9949759de680f2c3c7a314d937f1 MD5 | raw file
  1from datetime import datetime
  2from operator import attrgetter
  3
  4from django.core.paginator import Paginator, InvalidPage, EmptyPage
  5from django.test import TestCase
  6
  7from models import Article
  8
  9
 10class CountContainer(object):
 11    def count(self):
 12        return 42
 13
 14class LenContainer(object):
 15    def __len__(self):
 16        return 42
 17
 18class PaginationTests(TestCase):
 19    def setUp(self):
 20        # Prepare a list of objects for pagination.
 21        for x in range(1, 10):
 22            a = Article(headline='Article %s' % x, pub_date=datetime(2005, 7, 29))
 23            a.save()
 24        
 25    def test_paginator(self):
 26        paginator = Paginator(Article.objects.all(), 5)
 27        self.assertEqual(9, paginator.count)
 28        self.assertEqual(2, paginator.num_pages)
 29        self.assertEqual([1, 2], paginator.page_range)
 30        
 31    def test_first_page(self):
 32        paginator = Paginator(Article.objects.all(), 5)
 33        p = paginator.page(1)
 34        self.assertEqual(u"<Page 1 of 2>", unicode(p))
 35        self.assertQuerysetEqual(p.object_list, [
 36                "<Article: Article 1>",
 37                "<Article: Article 2>",
 38                "<Article: Article 3>",
 39                "<Article: Article 4>",
 40                "<Article: Article 5>"
 41            ]
 42        )
 43        self.assertTrue(p.has_next())
 44        self.assertFalse(p.has_previous())
 45        self.assertTrue(p.has_other_pages())
 46        self.assertEqual(2, p.next_page_number())
 47        self.assertEqual(0, p.previous_page_number())
 48        self.assertEqual(1, p.start_index())
 49        self.assertEqual(5, p.end_index())
 50    
 51    def test_last_page(self):
 52        paginator = Paginator(Article.objects.all(), 5)
 53        p = paginator.page(2)
 54        self.assertEqual(u"<Page 2 of 2>", unicode(p))
 55        self.assertQuerysetEqual(p.object_list, [
 56                "<Article: Article 6>",
 57                "<Article: Article 7>",
 58                "<Article: Article 8>",
 59                "<Article: Article 9>"
 60            ]
 61        )
 62        self.assertFalse(p.has_next())
 63        self.assertTrue(p.has_previous())
 64        self.assertTrue(p.has_other_pages())
 65        self.assertEqual(3, p.next_page_number())
 66        self.assertEqual(1, p.previous_page_number())
 67        self.assertEqual(6, p.start_index())
 68        self.assertEqual(9, p.end_index())
 69    
 70    def test_empty_page(self):
 71        paginator = Paginator(Article.objects.all(), 5)
 72        self.assertRaises(EmptyPage, paginator.page, 0)
 73        self.assertRaises(EmptyPage, paginator.page, 3)
 74        
 75        # Empty paginators with allow_empty_first_page=True.
 76        paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=True)
 77        self.assertEqual(0, paginator.count)
 78        self.assertEqual(1, paginator.num_pages)
 79        self.assertEqual([1], paginator.page_range)
 80        
 81        # Empty paginators with allow_empty_first_page=False.
 82        paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=False)
 83        self.assertEqual(0, paginator.count)
 84        self.assertEqual(0, paginator.num_pages)
 85        self.assertEqual([], paginator.page_range)
 86    
 87    def test_invalid_page(self):
 88        paginator = Paginator(Article.objects.all(), 5)
 89        self.assertRaises(InvalidPage, paginator.page, 7)
 90    
 91    def test_orphans(self):
 92        # Add a few more records to test out the orphans feature.
 93        for x in range(10, 13):
 94            Article(headline="Article %s" % x, pub_date=datetime(2006, 10, 6)).save()
 95            
 96        # With orphans set to 3 and 10 items per page, we should get all 12 items on a single page.
 97        paginator = Paginator(Article.objects.all(), 10, orphans=3)
 98        self.assertEqual(1, paginator.num_pages)
 99        
100        # With orphans only set to 1, we should get two pages.
101        paginator = Paginator(Article.objects.all(), 10, orphans=1)
102        self.assertEqual(2, paginator.num_pages)
103    
104    def test_paginate_list(self):
105        # Paginators work with regular lists/tuples, too -- not just with QuerySets.
106        paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
107        self.assertEqual(9, paginator.count)
108        self.assertEqual(2, paginator.num_pages)
109        self.assertEqual([1, 2], paginator.page_range)
110        p = paginator.page(1)
111        self.assertEqual(u"<Page 1 of 2>", unicode(p))
112        self.assertEqual([1, 2, 3, 4, 5], p.object_list)
113        self.assertTrue(p.has_next())
114        self.assertFalse(p.has_previous())
115        self.assertTrue(p.has_other_pages())
116        self.assertEqual(2, p.next_page_number())
117        self.assertEqual(0, p.previous_page_number())
118        self.assertEqual(1, p.start_index())
119        self.assertEqual(5, p.end_index())
120    
121    def test_paginate_misc_classes(self):
122        # Paginator can be passed other objects with a count() method.
123        paginator = Paginator(CountContainer(), 10)
124        self.assertEqual(42, paginator.count)
125        self.assertEqual(5, paginator.num_pages)
126        self.assertEqual([1, 2, 3, 4, 5], paginator.page_range)
127        
128        # Paginator can be passed other objects that implement __len__.
129        paginator = Paginator(LenContainer(), 10)
130        self.assertEqual(42, paginator.count)
131        self.assertEqual(5, paginator.num_pages)
132        self.assertEqual([1, 2, 3, 4, 5], paginator.page_range)