PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/pagination_regress/tests.py

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