def test_page_for_id(self): # test on empty builder p = ObjPaginator(self.builder_empty.step_set.all()) self.assertRaises(InvalidInput, p.page_for_id, -1) self.assertRaises(InvalidInput, p.page_for_id, 0) self.assertRaises(InvalidInput, p.page_for_id, "str") # test on qs with content count = self.stepcount # expect ids = [1:count] p = ObjPaginator(self.builder.step_set.all()) self.assertRaises(InvalidInput, p.page_for_id, 0) self.assertRaises(InvalidInput, p.page_for_id, count+1) self.assertRaises(InvalidInput, p.page_for_id, "str") for i in xrange(1, count+1): page = p.page_for_id(i) self.assertEqual(page.obj.id, i)
def test_page_navigation(self): count = self.stepcount # expect ids = [1:count] p = ObjPaginator(self.builder.step_set.all()) # first page has no prev page = p.page_for_id(1) self.assertFalse(page.has_previous()) self.assertTrue(page.has_next()) self.assertEqual(None, page.get_previous_obj()) self.assertEqual(None, page.previous_obj) # last page has no next page = p.page_for_id(count) self.assertFalse(page.has_next()) self.assertTrue(page.has_previous()) self.assertEqual(None, page.get_next_obj()) self.assertEqual(None, page.next_obj) # something in between should have next and previous self.assertTrue(count > 3) # make sure we have enough elems page = p.page_for_id(count-2) self.assertTrue(page.has_next()) self.assertTrue(page.has_previous()) self.assertNotEqual(None, page.get_next_obj()) self.assertNotEqual(None, page.next_obj) self.assertNotEqual(None, page.get_previous_obj()) self.assertNotEqual(None, page.previous_obj) # use page as linked list and iterate till the end # well, not quite a linked list since next/prev_obj links to # obj and not the next Page. page = p.page_for_id(1) for i in xrange(1, count): # till second last self.assertNotEqual(None, page) self.assertEqual(i, page.obj.id) page = p.page_for_id(page.next_obj.id) self.assertNotEqual(None, page) self.assertEqual(count, page.obj.id) self.assertEqual(None, page.next_obj) # not run backwards for i in xrange(count,1,-1): # till second self.assertNotEqual(None, page) self.assertEqual(i, page.obj.id) page = p.page_for_id(page.previous_obj.id) self.assertNotEqual(None, page) self.assertEqual(1, page.obj.id) self.assertEqual(None, page.previous_obj)