PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mailman/rest/tests/test_paginate.py

https://gitlab.com/khushbuparakh/mailman
Python | 127 lines | 76 code | 19 blank | 32 comment | 7 complexity | 263048baf63663490c95c2f1cc04868d MD5 | raw file
  1. # Copyright (C) 2013-2016 by the Free Software Foundation, Inc.
  2. #
  3. # This file is part of GNU Mailman.
  4. #
  5. # GNU Mailman is free software: you can redistribute it and/or modify it under
  6. # the terms of the GNU General Public License as published by the Free
  7. # Software Foundation, either version 3 of the License, or (at your option)
  8. # any later version.
  9. #
  10. # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. # more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along with
  16. # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
  17. """paginate helper tests."""
  18. import unittest
  19. from falcon import HTTPInvalidParam, Request
  20. from mailman.app.lifecycle import create_list
  21. from mailman.database.transaction import transaction
  22. from mailman.rest.helpers import CollectionMixin
  23. from mailman.testing.layers import RESTLayer
  24. class _FakeRequest(Request):
  25. def __init__(self, count=None, page=None):
  26. self._params = {}
  27. if count is not None:
  28. self._params['count'] = count
  29. if page is not None:
  30. self._params['page'] = page
  31. class TestPaginateHelper(unittest.TestCase):
  32. """Test the @paginate decorator."""
  33. layer = RESTLayer
  34. def setUp(self):
  35. with transaction():
  36. self._mlist = create_list('test@example.com')
  37. def _get_resource(self):
  38. class Resource(CollectionMixin):
  39. def _get_collection(self, request):
  40. return ['one', 'two', 'three', 'four', 'five']
  41. def _resource_as_dict(self, res): # noqa
  42. return {'value': res}
  43. return Resource()
  44. def test_no_pagination(self):
  45. # When there is no pagination params in the request, all 5 items in
  46. # the collection are returned.
  47. resource = self._get_resource()
  48. # Expect 5 items
  49. page = resource._make_collection(_FakeRequest())
  50. self.assertEqual(page['start'], 0)
  51. self.assertEqual(page['total_size'], 5)
  52. self.assertEqual(
  53. [entry['value'] for entry in page['entries']],
  54. ['one', 'two', 'three', 'four', 'five'])
  55. def test_valid_pagination_request_page_one(self):
  56. # ?count=2&page=1 returns the first page, with two items in it.
  57. resource = self._get_resource()
  58. page = resource._make_collection(_FakeRequest(2, 1))
  59. self.assertEqual(page['start'], 0)
  60. self.assertEqual(page['total_size'], 5)
  61. self.assertEqual(
  62. [entry['value'] for entry in page['entries']], ['one', 'two'])
  63. def test_valid_pagination_request_page_two(self):
  64. # ?count=2&page=2 returns the second page, where a page has two items
  65. # in it.
  66. resource = self._get_resource()
  67. page = resource._make_collection(_FakeRequest(2, 2))
  68. self.assertEqual(page['start'], 2)
  69. self.assertEqual(page['total_size'], 5)
  70. self.assertEqual(
  71. [entry['value'] for entry in page['entries']], ['three', 'four'])
  72. def test_2nd_index_larger_than_total(self):
  73. # ?count=2&page=3 returns the third page with page size 2, but the
  74. # last page only has one item in it.
  75. resource = self._get_resource()
  76. page = resource._make_collection(_FakeRequest(2, 3))
  77. self.assertEqual(page['start'], 4)
  78. self.assertEqual(page['total_size'], 5)
  79. self.assertEqual(
  80. [entry['value'] for entry in page['entries']], ['five'])
  81. def test_out_of_range_returns_empty_list(self):
  82. # ?count=2&page=4 returns the fourth page, which doesn't exist, so an
  83. # empty collection is returned.
  84. resource = self._get_resource()
  85. page = resource._make_collection(_FakeRequest(2, 4))
  86. self.assertEqual(page['start'], 6)
  87. self.assertEqual(page['total_size'], 5)
  88. self.assertNotIn('entries', page)
  89. def test_count_as_string_returns_bad_request(self):
  90. # ?count=two&page=2 are not valid values, so a bad request occurs.
  91. resource = self._get_resource()
  92. self.assertRaises(HTTPInvalidParam, resource._make_collection,
  93. _FakeRequest('two', 1))
  94. def test_negative_count(self):
  95. # ?count=-1&page=1
  96. resource = self._get_resource()
  97. self.assertRaises(HTTPInvalidParam, resource._make_collection,
  98. _FakeRequest(-1, 1))
  99. def test_negative_page(self):
  100. # ?count=1&page=-1
  101. resource = self._get_resource()
  102. self.assertRaises(HTTPInvalidParam, resource._make_collection,
  103. _FakeRequest(1, -1))
  104. def test_negative_page_and_count(self):
  105. # ?count=1&page=-1
  106. resource = self._get_resource()
  107. self.assertRaises(HTTPInvalidParam, resource._make_collection,
  108. _FakeRequest(-1, -1))