Beispiel #1
0
    def paginator_and_page(self, cache, buf_size=CACHE_CHUNK_SIZE):
        paginator = BufferedPaginator(
            count=cache["count"], offset=cache["offset"], buf_size=buf_size, per_page=cache["per_page"]
        )

        try:
            page = paginator.page(cache["page_num"])
        except (EmptyPage, InvalidPage):
            page = paginator.page(paginator.num_pages)

        return paginator, page
Beispiel #2
0
    def test_partial(self):
        kwargs = {
            "count": 20,
            "offset": 0,
            "buf_size": 10,
            "object_list": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            "per_page": 40,
        }

        bp = BufferedPaginator(**kwargs)

        self.assertEqual(bp.num_pages, 1)
        self.assertEqual(bp.cached_page_indices(), (1, 2))
        self.assertEqual(bp.cached_pages(), 1)

        p1 = bp.page(1)
        self.assertTrue(p1.in_cache())
        self.assertEqual((p1.start_index(), p1.end_index()), (1, 20))
        self.assertEqual(p1.object_list, kwargs["object_list"])

        kwargs["offset"] = 10

        bp = BufferedPaginator(**kwargs)

        self.assertEqual(bp.num_pages, 1)
        self.assertEqual(bp.cached_page_indices(), (0, 0))
        self.assertEqual(bp.cached_pages(), 0)
Beispiel #3
0
    def test_base(self):
        kwargs = {
            "count": 100,
            "offset": 0,
            "buf_size": 10,
            "object_list": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            "per_page": 2,
        }

        bp = BufferedPaginator(**kwargs)

        self.assertEqual(bp.num_pages, 50)
        self.assertEqual(bp.cached_page_indices(), (1, 6))
        self.assertEqual(bp.cached_pages(), 5)

        self.assertTrue(bp.page(2).in_cache())
        self.assertFalse(bp.page(6).in_cache())
Beispiel #4
0
    def test_offset(self):
        kwargs = {
            "count": 100,
            "offset": 40,
            "buf_size": 10,
            "object_list": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
            "per_page": 2,
        }

        bp = BufferedPaginator(**kwargs)

        self.assertEqual(bp.num_pages, 50)
        self.assertEqual(bp.cached_page_indices(), (21, 26))
        self.assertEqual(bp.cached_pages(), 5)

        self.assertFalse(bp.page(20).in_cache())
        self.assertTrue(bp.page(21).in_cache())
        self.assertFalse(bp.page(26).in_cache())

        # try as a negative offset
        kwargs["offset"] = -60

        bp = BufferedPaginator(**kwargs)

        self.assertEqual(bp.num_pages, 50)
        self.assertEqual(bp.cached_page_indices(), (21, 26))
        self.assertEqual(bp.cached_pages(), 5)

        self.assertFalse(bp.page(20).in_cache())
        self.assertTrue(bp.page(21).in_cache())
        self.assertFalse(bp.page(26).in_cache())