PageRenderTime 27ms CodeModel.GetById 9ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 1ms

/tests/regressiontests/pagination_regress/tests.py

https://code.google.com/p/mango-py/
Python | 156 lines | 102 code | 6 blank | 48 comment | 5 complexity | c5a0a93647cdd06701d8ce77030c367d MD5 | raw file
  1from django.core.paginator import Paginator, EmptyPage
  2from django.utils.unittest import TestCase
  3
  4class PaginatorTests(TestCase):
  5    """
  6    Tests for the Paginator and Page classes.
  7    """
  8
  9    def check_paginator(self, params, output):
 10        """
 11        Helper method that instantiates a Paginator object from the passed
 12        params and then checks that its attributes match the passed output.
 13        """
 14        count, num_pages, page_range = output
 15        paginator = Paginator(*params)
 16        self.check_attribute('count', paginator, count, params)
 17        self.check_attribute('num_pages', paginator, num_pages, params)
 18        self.check_attribute('page_range', paginator, page_range, params)
 19
 20    def check_attribute(self, name, paginator, expected, params):
 21        """
 22        Helper method that checks a single attribute and gives a nice error
 23        message upon test failure.
 24        """
 25        got = getattr(paginator, name)
 26        self.assertEqual(expected, got,
 27            "For '%s', expected %s but got %s.  Paginator parameters were: %s"
 28            % (name, expected, got, params))
 29
 30    def test_paginator(self):
 31        """
 32        Tests the paginator attributes using varying inputs.
 33        """
 34        nine = [1, 2, 3, 4, 5, 6, 7, 8, 9]
 35        ten = nine + [10]
 36        eleven = ten + [11]
 37        tests = (
 38            # Each item is two tuples:
 39            #     First tuple is Paginator parameters - object_list, per_page,
 40            #         orphans, and allow_empty_first_page.
 41            #     Second tuple is resulting Paginator attributes - count,
 42            #         num_pages, and page_range.
 43            # Ten items, varying orphans, no empty first page.
 44            ((ten, 4, 0, False), (10, 3, [1, 2, 3])),
 45            ((ten, 4, 1, False), (10, 3, [1, 2, 3])),
 46            ((ten, 4, 2, False), (10, 2, [1, 2])),
 47            ((ten, 4, 5, False), (10, 2, [1, 2])),
 48            ((ten, 4, 6, False), (10, 1, [1])),
 49            # Ten items, varying orphans, allow empty first page.
 50            ((ten, 4, 0, True), (10, 3, [1, 2, 3])),
 51            ((ten, 4, 1, True), (10, 3, [1, 2, 3])),
 52            ((ten, 4, 2, True), (10, 2, [1, 2])),
 53            ((ten, 4, 5, True), (10, 2, [1, 2])),
 54            ((ten, 4, 6, True), (10, 1, [1])),
 55            # One item, varying orphans, no empty first page.
 56            (([1], 4, 0, False), (1, 1, [1])),
 57            (([1], 4, 1, False), (1, 1, [1])),
 58            (([1], 4, 2, False), (1, 1, [1])),
 59            # One item, varying orphans, allow empty first page.
 60            (([1], 4, 0, True), (1, 1, [1])),
 61            (([1], 4, 1, True), (1, 1, [1])),
 62            (([1], 4, 2, True), (1, 1, [1])),
 63            # Zero items, varying orphans, no empty first page.
 64            (([], 4, 0, False), (0, 0, [])),
 65            (([], 4, 1, False), (0, 0, [])),
 66            (([], 4, 2, False), (0, 0, [])),
 67            # Zero items, varying orphans, allow empty first page.
 68            (([], 4, 0, True), (0, 1, [1])),
 69            (([], 4, 1, True), (0, 1, [1])),
 70            (([], 4, 2, True), (0, 1, [1])),
 71            # Number if items one less than per_page.
 72            (([], 1, 0, True), (0, 1, [1])),
 73            (([], 1, 0, False), (0, 0, [])),
 74            (([1], 2, 0, True), (1, 1, [1])),
 75            ((nine, 10, 0, True), (9, 1, [1])),
 76            # Number if items equal to per_page.
 77            (([1], 1, 0, True), (1, 1, [1])),
 78            (([1, 2], 2, 0, True), (2, 1, [1])),
 79            ((ten, 10, 0, True), (10, 1, [1])),
 80            # Number if items one more than per_page.
 81            (([1, 2], 1, 0, True), (2, 2, [1, 2])),
 82            (([1, 2, 3], 2, 0, True), (3, 2, [1, 2])),
 83            ((eleven, 10, 0, True), (11, 2, [1, 2])),
 84            # Number if items one more than per_page with one orphan.
 85            (([1, 2], 1, 1, True), (2, 1, [1])),
 86            (([1, 2, 3], 2, 1, True), (3, 1, [1])),
 87            ((eleven, 10, 1, True), (11, 1, [1])),
 88        )
 89        for params, output in tests:
 90            self.check_paginator(params, output)
 91
 92    def check_indexes(self, params, page_num, indexes):
 93        """
 94        Helper method that instantiates a Paginator object from the passed
 95        params and then checks that the start and end indexes of the passed
 96        page_num match those given as a 2-tuple in indexes.
 97        """
 98        paginator = Paginator(*params)
 99        if page_num == 'first':
100            page_num = 1
101        elif page_num == 'last':
102            page_num = paginator.num_pages
103        page = paginator.page(page_num)
104        start, end = indexes
105        msg = ("For %s of page %s, expected %s but got %s."
106               " Paginator parameters were: %s")
107        self.assertEqual(start, page.start_index(),
108            msg % ('start index', page_num, start, page.start_index(), params))
109        self.assertEqual(end, page.end_index(),
110            msg % ('end index', page_num, end, page.end_index(), params))
111
112    def test_page_indexes(self):
113        """
114        Tests that paginator pages have the correct start and end indexes.
115        """
116        ten = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
117        tests = (
118            # Each item is three tuples:
119            #     First tuple is Paginator parameters - object_list, per_page,
120            #         orphans, and allow_empty_first_page.
121            #     Second tuple is the start and end indexes of the first page.
122            #     Third tuple is the start and end indexes of the last page.
123            # Ten items, varying per_page, no orphans.
124            ((ten, 1, 0, True), (1, 1), (10, 10)),
125            ((ten, 2, 0, True), (1, 2), (9, 10)),
126            ((ten, 3, 0, True), (1, 3), (10, 10)),
127            ((ten, 5, 0, True), (1, 5), (6, 10)),
128            # Ten items, varying per_page, with orphans.
129            ((ten, 1, 1, True), (1, 1), (9, 10)),
130            ((ten, 1, 2, True), (1, 1), (8, 10)),
131            ((ten, 3, 1, True), (1, 3), (7, 10)),
132            ((ten, 3, 2, True), (1, 3), (7, 10)),
133            ((ten, 3, 4, True), (1, 3), (4, 10)),
134            ((ten, 5, 1, True), (1, 5), (6, 10)),
135            ((ten, 5, 2, True), (1, 5), (6, 10)),
136            ((ten, 5, 5, True), (1, 10), (1, 10)),
137            # One item, varying orphans, no empty first page.
138            (([1], 4, 0, False), (1, 1), (1, 1)),
139            (([1], 4, 1, False), (1, 1), (1, 1)),
140            (([1], 4, 2, False), (1, 1), (1, 1)),
141            # One item, varying orphans, allow empty first page.
142            (([1], 4, 0, True), (1, 1), (1, 1)),
143            (([1], 4, 1, True), (1, 1), (1, 1)),
144            (([1], 4, 2, True), (1, 1), (1, 1)),
145            # Zero items, varying orphans, allow empty first page.
146            (([], 4, 0, True), (0, 0), (0, 0)),
147            (([], 4, 1, True), (0, 0), (0, 0)),
148            (([], 4, 2, True), (0, 0), (0, 0)),
149        )
150        for params, first, last in tests:
151            self.check_indexes(params, 'first', first)
152            self.check_indexes(params, 'last', last)
153        # When no items and no empty first page, we should get EmptyPage error.
154        self.assertRaises(EmptyPage, self.check_indexes, ([], 4, 0, False), 1, None)
155        self.assertRaises(EmptyPage, self.check_indexes, ([], 4, 1, False), 1, None)
156        self.assertRaises(EmptyPage, self.check_indexes, ([], 4, 2, False), 1, None)