def test_page_has_previous(self): paginator = Paginator(10, 2) page = paginator.page(1) self.assertFalse(page.has_previous()) page = paginator.page(5) self.assertTrue(page.has_previous())
def check_indexes(self, params, page_num, indexes): """ Helper method that instantiates a Paginator object from the passed params and then checks that the start and end indexes of the passed page_num match those given as a 2-tuple in indexes. """ paginator = Paginator(*params) if page_num == 'first': page_num = 1 elif page_num == 'last': page_num = paginator.num_pages page = paginator.page(page_num) start, end = indexes msg = ("For %s of page %s, expected %s but got %s." " Paginator parameters were: %s") self.assertEqual(start, page.start_index(), msg % ('start index', page_num, start, page.start_index(), params)) self.assertEqual(end, page.end_index(), msg % ('end index', page_num, end, page.end_index(), params))
def test_paginator_validate_number(self): paginator = Paginator(10, 2) # Make sure invalid pages throw the appropriate errors self.assertRaises(PageNotAnInteger, paginator.validate_number, "wtf") self.assertRaises(EmptyPage, paginator.validate_number, 0) self.assertRaises(EmptyPage, paginator.validate_number, 100) # Make sure validate_number throws EmptyPage when # allow_empty_first_page is false and there are no items paginator = Paginator(0, 2, allow_empty_first_page=False) self.assertRaises(EmptyPage, paginator.validate_number, 1) # Can't seem to find the conditions for paginator that cause # line 30 to happen, so I'm forcing it. paginator = Paginator(0, 2, allow_empty_first_page=False) num_pages = paginator.num_pages # Pre-calculate the num-pages self.assertEqual(num_pages, 0) paginator.allow_empty_first_page = True paginator.validate_number(1)
def test_next_page_number(self): paginator = Paginator(10, 2) page = paginator.page(1) self.assertEqual(page.next_page_number(), 2)
def test_previous_page_number(self): paginator = Paginator(10, 2) page = paginator.page(5) self.assertEqual(page.previous_page_number(), 4)
def test_page_repr(self): paginator = Paginator(10, 2) page = paginator.page(1) result = page.__repr__() self.assertEqual("<Page 1 of 5>", result)