blob: 44c8b598bdb85b4cf6924e65a85887b7cf01d65c [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2016 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Unit tests for pagination classes."""
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 framework import exceptions
15from framework import paginate
16from testing import testing_helpers
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010017from mrproto import secrets_pb2
Copybara854996b2021-09-07 19:36:02 +000018
19
20class PageTokenTest(unittest.TestCase):
21
22 def setUp(self):
23 self.testbed = testbed.Testbed()
24 self.testbed.activate()
25 self.testbed.init_memcache_stub()
26 self.testbed.init_datastore_v3_stub()
27
28 def testGeneratePageToken_DiffRequests(self):
29 request_cont_1 = secrets_pb2.ListRequestContents(
30 parent='same', page_size=1, order_by='same', query='same')
31 request_cont_2 = secrets_pb2.ListRequestContents(
32 parent='same', page_size=2, order_by='same', query='same')
33 start = 10
34 self.assertNotEqual(
35 paginate.GeneratePageToken(request_cont_1, start),
36 paginate.GeneratePageToken(request_cont_2, start))
37
38 def testValidateAndParsePageToken(self):
39 request_cont_1 = secrets_pb2.ListRequestContents(
40 parent='projects/chicken', page_size=1, order_by='boks', query='hay')
41 start = 2
42 token = paginate.GeneratePageToken(request_cont_1, start)
43 self.assertEqual(
44 start,
45 paginate.ValidateAndParsePageToken(token, request_cont_1))
46
47 def testValidateAndParsePageToken_InvalidContents(self):
48 request_cont_1 = secrets_pb2.ListRequestContents(
49 parent='projects/chicken', page_size=1, order_by='boks', query='hay')
50 start = 2
51 token = paginate.GeneratePageToken(request_cont_1, start)
52
53 request_cont_diff = secrets_pb2.ListRequestContents(
54 parent='projects/goose', page_size=1, order_by='boks', query='hay')
55 with self.assertRaises(exceptions.PageTokenException):
56 paginate.ValidateAndParsePageToken(token, request_cont_diff)
57
58 def testValidateAndParsePageToken_InvalidSerializedToken(self):
59 request_cont = secrets_pb2.ListRequestContents()
60 with self.assertRaises(exceptions.PageTokenException):
61 paginate.ValidateAndParsePageToken('sldkfj87', request_cont)
62
63 def testValidateAndParsePageToken_InvalidTokenFormat(self):
64 request_cont = secrets_pb2.ListRequestContents()
65 with self.assertRaises(exceptions.PageTokenException):
66 paginate.ValidateAndParsePageToken('///sldkfj87', request_cont)
67
68
69class PaginateTest(unittest.TestCase):
70
71 def testVirtualPagination(self):
72 # Paginating 0 results on a page that can hold 100.
73 mr = testing_helpers.MakeMonorailRequest(path='/issues/list')
74 total_count = 0
75 items_per_page = 100
76 start = 0
77 vp = paginate.VirtualPagination(total_count, items_per_page, start)
78 self.assertEqual(vp.num, 100)
79 self.assertEqual(vp.start, 1)
80 self.assertEqual(vp.last, 0)
81 self.assertFalse(vp.visible)
82
83 # Paginating 12 results on a page that can hold 100.
84 mr = testing_helpers.MakeMonorailRequest(path='/issues/list')
85 vp = paginate.VirtualPagination(12, 100, 0)
86 self.assertEqual(vp.num, 100)
87 self.assertEqual(vp.start, 1)
88 self.assertEqual(vp.last, 12)
89 self.assertTrue(vp.visible)
90
91 # Paginating 12 results on a page that can hold 10.
92 mr = testing_helpers.MakeMonorailRequest(path='/issues/list?num=10')
93 vp = paginate.VirtualPagination(12, 10, 0)
94 self.assertEqual(vp.num, 10)
95 self.assertEqual(vp.start, 1)
96 self.assertEqual(vp.last, 10)
97 self.assertTrue(vp.visible)
98
99 # Paginating 12 results starting at 5 on page that can hold 10.
100 mr = testing_helpers.MakeMonorailRequest(
101 path='/issues/list?start=5&num=10')
102 vp = paginate.VirtualPagination(12, 10, 5)
103 self.assertEqual(vp.num, 10)
104 self.assertEqual(vp.start, 6)
105 self.assertEqual(vp.last, 12)
106 self.assertTrue(vp.visible)
107
108 # Paginating 123 results on a page that can hold 100.
109 mr = testing_helpers.MakeMonorailRequest(path='/issues/list')
110 vp = paginate.VirtualPagination(123, 100, 0)
111 self.assertEqual(vp.num, 100)
112 self.assertEqual(vp.start, 1)
113 self.assertEqual(vp.last, 100)
114 self.assertTrue(vp.visible)
115
116 # Paginating 123 results on second page that can hold 100.
117 mr = testing_helpers.MakeMonorailRequest(path='/issues/list?start=100')
118 vp = paginate.VirtualPagination(123, 100, 100)
119 self.assertEqual(vp.num, 100)
120 self.assertEqual(vp.start, 101)
121 self.assertEqual(vp.last, 123)
122 self.assertTrue(vp.visible)
123
124 # Paginating a huge number of objects will show at most 1000 per page.
125 mr = testing_helpers.MakeMonorailRequest(path='/issues/list?num=9999')
126 vp = paginate.VirtualPagination(12345, 9999, 0)
127 self.assertEqual(vp.num, 1000)
128 self.assertEqual(vp.start, 1)
129 self.assertEqual(vp.last, 1000)
130 self.assertTrue(vp.visible)
131
132 # Test urls for a hotlist pagination
133 mr = testing_helpers.MakeMonorailRequest(
134 path='/u/hotlists/17?num=5&start=4')
135 mr.hotlist_id = 17
136 mr.auth.user_id = 112
137 vp = paginate.VirtualPagination(12, 5, 4,
138 list_page_url='/u/112/hotlists/17')
139 self.assertEqual(vp.num, 5)
140 self.assertEqual(vp.start, 5)
141 self.assertEqual(vp.last, 9)
142 self.assertTrue(vp.visible)
143 self.assertEqual('/u/112/hotlists/17?num=5&start=9', vp.next_url)
144 self.assertEqual('/u/112/hotlists/17?num=5&start=0', vp.prev_url)