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