blob: ca0b7130104568ac9c9840323e1df7f8d9228227 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2020 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file.
4"""Tests for the Paginator class."""
5
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import unittest
11
12from google.appengine.ext import testbed
13
14from api.v3 import paginator
15from api.v3.api_proto import hotlists_pb2
16from framework import exceptions
17from framework import paginate
18
19class PaginatorTest(unittest.TestCase):
20
21 def setUp(self):
22 self.testbed = testbed.Testbed()
23 self.testbed.activate()
24 self.testbed.init_memcache_stub()
25 self.testbed.init_datastore_v3_stub()
26
27 self.paginator = paginator.Paginator(
28 parent='animal/goose/sound/honks', query='chaos')
29
30 def testGetStart(self):
31 """We can get the start index from a page_token."""
32 start = 5
33 page_token = paginate.GeneratePageToken(
34 self.paginator.request_contents, start)
35 self.assertEqual(self.paginator.GetStart(page_token), start)
36
37 def testGetStart_EmptyPageToken(self):
38 """We return the default start for an empty page_token."""
39 request = hotlists_pb2.ListHotlistItemsRequest()
40 self.assertEqual(0, self.paginator.GetStart(request.page_token))
41
42 def testGenerateNextPageToken(self):
43 """We return the next page token."""
44 next_start = 10
45 expected_page_token = paginate.GeneratePageToken(
46 self.paginator.request_contents, next_start)
47 self.assertEqual(
48 self.paginator.GenerateNextPageToken(next_start), expected_page_token)
49
50 def testGenerateNextPageToken_NoStart(self):
51 """We return None if start is not provided."""
52 next_start = None
53 self.assertEqual(self.paginator.GenerateNextPageToken(next_start), None)
54
55 def testCoercePageSize(self):
56 """A valid page_size is used when provided."""
57 self.assertEqual(1, paginator.CoercePageSize(1, 5))
58
59 def testCoercePageSize_Negative(self):
60 """An exception is raised for a negative page_size."""
61 with self.assertRaises(exceptions.InputException):
62 paginator.CoercePageSize(-1, 5)
63
64 def testCoercePageSize_TooBig(self):
65 """A page_size above the max is coerced to the max."""
66 self.assertEqual(5, paginator.CoercePageSize(6, 5, 2))
67
68 def testCoercePageSize_Default(self):
69 """A default page_size different from max_size is used when provided."""
70 self.assertEqual(2, paginator.CoercePageSize(None, 5, 2))
71
72 def testCoercePageSize_NotProvided(self):
73 """max_size is used if no page_size or default_size provided."""
74 self.assertEqual(5, paginator.CoercePageSize(None, 5))
75
76 def testCoercePageSize_Zero(self):
77 """Handles zero equivalently to None."""
78 self.assertEqual(5, paginator.CoercePageSize(0, 5))