def test_is_last_page(self): paging = Paging(3, PagingRequest(2, 1)) self.assertFalse(paging.is_last_page) paging = Paging(3, PagingRequest(3, 1)) self.assertTrue(paging.is_last_page) paging = Paging(0, PagingRequest(1, 1)) self.assertTrue(paging.is_last_page)
def test_create_url(self): config = testing.setUp() config.add_route('route_name', 'route_name') request = DummyRequest() property_naming = PropertyNamingMock('page-number', 'page-size', 'sort-property', 'sort-direction') pagination_url_creator = RequestUrlCreator( request, 'route_name', property_naming, ) url = pagination_url_creator( PaginationRequest(PagingRequest(3, 10), Sorting('id', 'desc'))) self.assertTrue(url.find('http://example.com/route_name') == 0) self.assertTrue(url.find('page-number=3') > 0) self.assertTrue(url.find('page-size=10') > 0) self.assertTrue(url.find('sort-property=id') > 0) self.assertTrue(url.find('sort-direction=desc') > 0) config.add_route('match_route', 'match_route/{page_number}') request = DummyRequest() request.matchdict['page_number'] = 'some_page' request.params['test'] = 'some_value' pagination_url_creator = RequestUrlCreator( request, 'match_route', property_naming, ) url = pagination_url_creator( PaginationRequest(PagingRequest(3, 10), Sorting('id', 'desc'))) self.assertTrue(url.find('match_route/some_page') > 0) self.assertTrue(url.find('test=some_value') > 0)
def test_has_next_page(self): paging = Paging(16, PagingRequest(1, 3)) self.assertTrue(paging.has_next_page) paging = Paging(16, PagingRequest(5, 3)) self.assertTrue(paging.has_next_page) paging = Paging(16, PagingRequest(6, 3)) self.assertFalse(paging.has_next_page)
def test_iterator(self): # pylint: disable=unused-variable paging = Paging(16, PagingRequest(1, 3)) i = 0 for page in paging: i += 1 self.assertEqual(i, 6) paging = Paging(1, PagingRequest(1, 1)) i = 0 for page in paging: i += 1 self.assertEqual(i, 1) paging = Paging(19, PagingRequest(2, 5)) i = 1 for page in paging: i += 1 self.assertEqual(i, 4) # expect one (empty) page for no content paging = Paging(0, PagingRequest(1, 1)) i = 0 for page in paging: i += 1 self.assertEqual(i, 1)
def test_content_offset(self): pagination = PagingRequest(2, 1) self.assertEqual(pagination.content_offset, 1) pagination = PagingRequest(1, 5) self.assertEqual(pagination.content_offset, 0) pagination = PagingRequest(2, 5) self.assertEqual(pagination.content_offset, 5) pagination = PagingRequest(1, 30) self.assertEqual(pagination.content_offset, 0)
def test_has_page(self): paging = Paging(15, PagingRequest(1, 3)) self.assertTrue(paging.has_page(1)) self.assertTrue(paging.has_page(5)) self.assertFalse(paging.has_page(6)) paging = Paging(16, PagingRequest(1, 5)) self.assertTrue(paging.has_page(4)) self.assertFalse(paging.has_page(5)) paging = Paging(0, PagingRequest(1, 5)) self.assertTrue(paging.has_page(1))
def test_constructor(self): # page size must be larger than 0 with self.assertRaises(ValueError): Paging(-1, PagingRequest(1, 1)) # page size must be of type int with self.assertRaises(TypeError): Paging('1', PagingRequest(1, 1)) # page number must be valid with self.assertRaises(PageNotFoundException): Paging(2, PagingRequest(3, 1)) paging = Paging(0, PagingRequest(1, 1)) paging = Paging(10, PagingRequest(1, 1)) self.assertTrue(paging is not None)
def test_next_page(self): paging = Paging(15, PagingRequest(2, 5)) paging = paging.next_page self.assertEqual(paging.content_size, 15) self.assertEqual(paging.page_number, 3) self.assertEqual(paging.page_size, 5) paging = Paging(3, PagingRequest(3, 1)) with self.assertRaises(PageNotFoundException) as raise_context: next_page = paging.next_page del(next_page) self.assertEqual( str(raise_context.exception), 'A next page does not exist.')
def test_paging(self): paging_request = PagingRequest(3, 10) paging = Paging(50, paging_request) view_pagination = Pagination( paging, None) self.assertEqual(view_pagination.paging, paging)
def test_call(self): writer = DefaultPaginationRequestWriter( PropertyNamingMock( 'cookie_prefix.page_number', 'cookie_prefix.page_size', 'cookie_prefix.sort_property', 'cookie_prefix.sort_direction' ) ) request = DummyRequest() request.response = ResponseMock() pagination_request = PaginationRequest( PagingRequest(3, 24), Sorting('some_sort_property', 'desc')) writer(request, pagination_request) self.assertEqual( request.response.get_cookie('cookie_prefix.page_number'), '3') self.assertEqual( request.response.get_cookie('cookie_prefix.page_size'), '24') self.assertEqual( request.response.get_cookie('cookie_prefix.sort_property'), 'some_sort_property') self.assertEqual( request.response.get_cookie('cookie_prefix.sort_direction'), 'desc')
def test_call(self): config = testing.setUp() config.add_route('some_route', '/') pagination_request_writer = PaginationRequestWriterMock() param_property_naming = PropertyNamingMock('page-number', 'page-size', 'sort-property', 'sort-direction') url_creator_factory = UrlCreatorFactoryMock('http://example.com') request = DummyRequest() request.registry.pagination = { 'some_route': { 'pagination_request_writer': pagination_request_writer, 'param_property_naming': param_property_naming, 'url_creator_factory': url_creator_factory, } } request.pagination = Pagination(Paging(100, PagingRequest(3, 12)), Sorting('some_property', 'desc')) request.matched_route = RouteMock('some_route') event = BeforeRenderEventMock() event['request'] = request subscriber = PaginationBeforeRenderSubscriber() subscriber(event) self.assertEqual(pagination_request_writer.call_count, 1) pagination = event.rendering_val['pagination'] self.assertEqual('http://example.com', str(pagination.url))
def test_write(self): request = DummyRequest() response_mock = ResponseMock() request.response = response_mock pagination_request = PagingRequest(3, 12) pagination_request_writer = CookiePagingRequestWriter( request, PropertyNamingMock( 'page_number', 'page_size', 'sort_property', 'sort_direction' ) ) pagination_request_writer(pagination_request) self.assertEqual(response_mock.get_cookie('page_number'), '3') self.assertEqual(response_mock.get_cookie('page_size'), '12') self.assertEqual(response_mock.call_count(), 2)
def test_constructor(self): # page size must not be 0 with self.assertRaises(ValueError): PagingRequest(1, 0) # page number must be > 0 with self.assertRaises(ValueError): PagingRequest(0, 1) # page size must be of type int with self.assertRaises(TypeError): PagingRequest(1, '1') # page number must be of type int with self.assertRaises(TypeError): PagingRequest('1', 1) pagination = PagingRequest(1, 1) self.assertTrue(pagination is not None) pagination = PagingRequest(10, 10) self.assertTrue(pagination is not None)
def test_page(self): paging = Paging(3, PagingRequest(1, 1)) paging = paging.page(2) self.assertEqual(paging.content_size, 3) self.assertEqual(paging.page_number, 2) self.assertEqual(paging.page_size, 1) with self.assertRaises(PageNotFoundException) as raise_context: paging.page(4) self.assertEqual( str(raise_context.exception), 'Page 4 does not exist.') with self.assertRaises(PageNotFoundException) as raise_context: paging.page(0) self.assertEqual( str(raise_context.exception), 'Page 0 does not exist.') with self.assertRaises(PageNotFoundException) as raise_context: paging.page(-1) self.assertEqual( str(raise_context.exception), 'Page -1 does not exist.')
def test_page_count(self): paging = Paging(15, PagingRequest(1, 5)) self.assertTrue(paging.page_count, 5)
def test_content_size(self): paging = Paging(10, PagingRequest(2, 5)) self.assertEqual(paging.content_size, 10)
def test_last_page(self): paging = Paging(3, PagingRequest(2, 1)) paging = paging.last_page self.assertEqual(paging.content_size, 3) self.assertEqual(paging.page_number, 3) self.assertEqual(paging.page_size, 1)
def test_is_valid_content_size_page(self): pagination = PagingRequest(1, 1) self.assertTrue(pagination.is_valid_content_size_page(0)) pagination = PagingRequest(2, 1) self.assertFalse(pagination.is_valid_content_size_page(0)) pagination = PagingRequest(5, 1) self.assertTrue(pagination.is_valid_content_size_page(5)) pagination = PagingRequest(6, 1) self.assertFalse(pagination.is_valid_content_size_page(5)) pagination = PagingRequest(5, 10) self.assertTrue(pagination.is_valid_content_size_page(70)) pagination = PagingRequest(8, 10) self.assertFalse(pagination.is_valid_content_size_page(70)) pagination = PagingRequest(8, 10) self.assertTrue(pagination.is_valid_content_size_page(71))
def test_page_size(self): pagination = PagingRequest(2, 5) self.assertEqual(pagination.page_size, 5)
def test_page_number(self): pagination = PagingRequest(2, 5) self.assertEqual(pagination.page_number, 2)
def test_has_previous_page(self): paging = Paging(16, PagingRequest(1, 3)) self.assertFalse(paging.has_previous_page) paging = Paging(16, PagingRequest(2, 3)) self.assertTrue(paging.has_previous_page)
def test_paging_request(self): paging_request = PagingRequest(3, 10) pagination_request = PaginationRequest( paging_request, None) self.assertEqual(pagination_request.paging_request, paging_request)
def _create_paging(cls, page_number, page_size, content_size): paging_request = PagingRequest(page_number, page_size) paging = Paging(content_size, paging_request) return paging
def get_response_provider(cls, content_size, page_number, page_size): return UrlBuilder( UrlCreatorMock('http://example.com/test/url'), Pagination( Paging(content_size, PagingRequest(page_number, page_size)), Sorting('id', 'desc')))