/ tests / PaginatedList.py
PaginatedList.py
  1  ############################ Copyrights and license ############################
  2  #                                                                              #
  3  # Copyright 2012 Vincent Jacques <vincent@vincent-jacques.net>                 #
  4  # Copyright 2012 Zearin <zearin@gonk.net>                                      #
  5  # Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net>                 #
  6  # Copyright 2013 davidbrai <davidbrai@gmail.com>                               #
  7  # Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net>                 #
  8  # Copyright 2015 Eliot Walker <eliot@lyft.com>                                 #
  9  # Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com>          #
 10  # Copyright 2018 sfdye <tsfdye@gmail.com>                                      #
 11  #                                                                              #
 12  # This file is part of PyGithub.                                               #
 13  # http://pygithub.readthedocs.io/                                              #
 14  #                                                                              #
 15  # PyGithub is free software: you can redistribute it and/or modify it under    #
 16  # the terms of the GNU Lesser General Public License as published by the Free  #
 17  # Software Foundation, either version 3 of the License, or (at your option)    #
 18  # any later version.                                                           #
 19  #                                                                              #
 20  # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
 21  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
 22  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
 23  # details.                                                                     #
 24  #                                                                              #
 25  # You should have received a copy of the GNU Lesser General Public License     #
 26  # along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
 27  #                                                                              #
 28  ################################################################################
 29  
 30  from github.PaginatedList import PaginatedList as PaginatedListImpl
 31  
 32  from . import Framework
 33  
 34  
 35  class PaginatedList(Framework.TestCase):
 36      def setUp(self):
 37          super().setUp()
 38          self.repo = self.g.get_user("openframeworks").get_repo("openFrameworks")
 39          self.list = self.repo.get_issues()
 40          self.licenses = self.g.get_enterprise("beaver-group").get_consumed_licenses()
 41  
 42      def testIteration(self):
 43          self.assertEqual(len(list(self.list)), 333)
 44  
 45      def testIterationWithPrefetchedFirstPage(self):
 46          # test data taken from EnterpriseAdmin.testGetEnterpriseUsers
 47          users = self.licenses.get_users()
 48          self.assertEqual(len(list(users)), 102)
 49          self.assertEqual(len({user.github_com_login for user in users}), 102)
 50  
 51      def testSeveralIterations(self):
 52          self.assertEqual(len(list(self.list)), 333)
 53          self.assertEqual(len(list(self.list)), 333)
 54          self.assertEqual(len(list(self.list)), 333)
 55          self.assertEqual(len(list(self.list)), 333)
 56  
 57      def testIntIndexingInFirstPage(self):
 58          self.assertEqual(self.list[0].id, 4772349)
 59          self.assertEqual(self.list[24].id, 4286936)
 60  
 61      def testReversedIterationWithSinglePage(self):
 62          r = self.list.reversed
 63          self.assertEqual(r[0].id, 4286936)
 64          self.assertEqual(r[1].id, 4317009)
 65  
 66      def testReversedIterationWithMultiplePages(self):
 67          r = self.list.reversed
 68          self.assertEqual(r[0].id, 94898)
 69          self.assertEqual(r[1].id, 104702)
 70          self.assertEqual(r[13].id, 166211)
 71          self.assertEqual(r[14].id, 166212)
 72          self.assertEqual(r[15].id, 166214)
 73  
 74      def testReversedIterationSupportsIterator(self):
 75          r = self.list.reversed
 76          for i in r:
 77              self.assertEqual(i.id, 4286936)
 78              return
 79          self.fail("empty iterator")
 80  
 81      def testGettingTheReversedListDoesNotModifyTheOriginalList(self):
 82          self.assertEqual(self.list[0].id, 18345408)
 83          self.assertEqual(self.list[30].id, 17916118)
 84          r = self.list.reversed
 85          self.assertEqual(self.list[0].id, 18345408)
 86          self.assertEqual(self.list[30].id, 17916118)
 87          self.assertEqual(r[0].id, 132373)
 88          self.assertEqual(r[30].id, 543694)
 89  
 90      def testIntIndexingInThirdPage(self):
 91          self.assertEqual(self.list[50].id, 3911629)
 92          self.assertEqual(self.list[74].id, 3605277)
 93  
 94      def testGetFirstPage(self):
 95          self.assertListKeyEqual(
 96              self.list.get_page(0),
 97              lambda i: i.id,
 98              [
 99                  4772349,
100                  4767675,
101                  4758608,
102                  4700182,
103                  4662873,
104                  4608132,
105                  4604661,
106                  4588997,
107                  4557803,
108                  4554058,
109                  4539985,
110                  4507572,
111                  4507492,
112                  4507416,
113                  4447561,
114                  4406584,
115                  4384548,
116                  4383465,
117                  4373361,
118                  4373201,
119                  4370619,
120                  4356530,
121                  4352401,
122                  4317009,
123                  4286936,
124              ],
125          )
126  
127      def testGetThirdPage(self):
128          self.assertListKeyEqual(
129              self.list.get_page(2),
130              lambda i: i.id,
131              [
132                  3911629,
133                  3911537,
134                  3910580,
135                  3910555,
136                  3910549,
137                  3897090,
138                  3883598,
139                  3856005,
140                  3850655,
141                  3825582,
142                  3813852,
143                  3812318,
144                  3812275,
145                  3807459,
146                  3799872,
147                  3799653,
148                  3795495,
149                  3754055,
150                  3710293,
151                  3662214,
152                  3647640,
153                  3631618,
154                  3627067,
155                  3614231,
156                  3605277,
157              ],
158          )
159  
160      def testIntIndexingAfterIteration(self):
161          self.assertEqual(len(list(self.list)), 333)
162          self.assertEqual(self.list[11].id, 4507572)
163          self.assertEqual(self.list[73].id, 3614231)
164          self.assertEqual(self.list[332].id, 94898)
165  
166      def testSliceIndexingInFirstPage(self):
167          self.assertListKeyEqual(
168              self.list[:13],
169              lambda i: i.id,
170              [
171                  4772349,
172                  4767675,
173                  4758608,
174                  4700182,
175                  4662873,
176                  4608132,
177                  4604661,
178                  4588997,
179                  4557803,
180                  4554058,
181                  4539985,
182                  4507572,
183                  4507492,
184              ],
185          )
186          self.assertListKeyEqual(
187              self.list[:13:3],
188              lambda i: i.id,
189              [4772349, 4700182, 4604661, 4554058, 4507492],
190          )
191          self.assertListKeyEqual(self.list[10:13], lambda i: i.id, [4539985, 4507572, 4507492])
192          self.assertListKeyEqual(self.list[5:13:3], lambda i: i.id, [4608132, 4557803, 4507572])
193  
194      def testSliceIndexingUntilFourthPage(self):
195          self.assertListKeyEqual(
196              self.list[:99:10],
197              lambda i: i.id,
198              [
199                  4772349,
200                  4539985,
201                  4370619,
202                  4207350,
203                  4063366,
204                  3911629,
205                  3813852,
206                  3647640,
207                  3528378,
208                  3438233,
209              ],
210          )
211          self.assertListKeyEqual(
212              self.list[73:78],
213              lambda i: i.id,
214              [3614231, 3605277, 3596240, 3594731, 3593619],
215          )
216          self.assertListKeyEqual(
217              self.list[70:80:2],
218              lambda i: i.id,
219              [3647640, 3627067, 3605277, 3594731, 3593430],
220          )
221  
222      def testSliceIndexingUntilEnd(self):
223          self.assertListKeyEqual(
224              self.list[310::3],
225              lambda i: i.id,
226              [268332, 204247, 169176, 166211, 165898, 163959, 132373, 104702],
227          )
228          self.assertListKeyEqual(
229              self.list[310:],
230              lambda i: i.id,
231              [
232                  268332,
233                  211418,
234                  205935,
235                  204247,
236                  172424,
237                  171615,
238                  169176,
239                  166214,
240                  166212,
241                  166211,
242                  166209,
243                  166208,
244                  165898,
245                  165537,
246                  165409,
247                  163959,
248                  132671,
249                  132377,
250                  132373,
251                  130269,
252                  111018,
253                  104702,
254                  94898,
255              ],
256          )
257  
258      def testInterruptedIteration(self):
259          # No asserts, but checks that only three pages are fetched
260          count = 0
261          for element in self.list:  # pragma no branch (exits only by break)
262              count += 1
263              if count == 75:
264                  break
265  
266      def testInterruptedIterationInSlice(self):
267          # No asserts, but checks that only three pages are fetched
268          count = 0
269          for element in self.list[:100]:  # pragma no branch (exits only by break)
270              count += 1
271              if count == 75:
272                  break
273  
274      def testTotalCountWithNoLastPage(self):
275          # Fudged replay data, we don't need the data, only the headers
276          repos = self.g.get_repos()
277          self.assertEqual(0, repos.totalCount)
278  
279      def testTotalCountWithDictionary(self):
280          # PullRequest.get_review_requests() actually returns a dictionary that
281          # we fudge into two lists, which means data is a dict, not a list.
282          # We should check the member, not data itself for totalCount.
283          pr = self.g.get_repo("PyGithub/PyGithub").get_pull(2078)
284          review_requests = pr.get_review_requests()
285          self.assertEqual(review_requests[0].totalCount, 0)
286          self.assertEqual(review_requests[1].totalCount, 0)
287  
288      def testCustomPerPage(self):
289          self.assertEqual(self.g.per_page, 30)
290          self.g.per_page = 100
291          self.assertEqual(self.g.per_page, 100)
292          self.assertEqual(len(list(self.repo.get_issues())), 456)
293  
294      def testCustomPerPageWithNoUrlParams(self):
295          from . import (  # Don't polute github.tests namespace, it would conflict with github.tests.CommitComment
296              CommitComment,
297          )
298  
299          self.g.per_page = 100
300          PaginatedListImpl(
301              CommitComment.CommitComment,
302              self.repo._requester,
303              f"{self.repo.url}/comments",
304              None,
305          )
306  
307      def testCustomPerPageWithNoUrlParams2(self):
308          # This test is redundant and less unitary than testCustomPerPageWithNoUrlParams
309          # but I hope it will be more robust if we refactor PaginatedList,
310          # because testCustomPerPageWithNoUrlParams only tests the constructor
311          self.g.per_page = 100
312          self.assertEqual(len(list(self.repo.get_comments())), 325)
313  
314      def testCustomPerPageWithGetPage(self):
315          self.g.per_page = 100
316          self.assertEqual(len(self.repo.get_issues().get_page(2)), 100)
317  
318      def testNoFirstPage(self):
319          self.assertFalse(next(iter(self.list), None))