jenkins-bot has submitted this change and it was merged.
Change subject: Allow APISite randompages to yield indefinitely ......................................................................
Allow APISite randompages to yield indefinitely
randompages currently supplies a default limit of 10, and will stop after the first batch if limit is None.
Change-Id: I67066d587f70978f33bb48d341f3e90b4f17bdd3 --- M pywikibot/data/api.py M tests/site_tests.py 2 files changed, 23 insertions(+), 3 deletions(-)
Approvals: XZise: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py index 0400f5d..c0b6e2a 100644 --- a/pywikibot/data/api.py +++ b/pywikibot/data/api.py @@ -1519,7 +1519,7 @@ # self.resultkey not in data in last request.submit() # only "(query-)continue" was retrieved. previous_result_had_data = False - if self.modules[0] == "random" and self.limit: + if self.modules[0] == "random": # "random" module does not return "(query-)continue" # now we loop for a new random query del self.data # a new request is needed diff --git a/tests/site_tests.py b/tests/site_tests.py index e738859..ae2c17f 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -1166,17 +1166,37 @@
"""Test random methods of a site."""
- def testRandompages(self): - """Test the site.randompages() method.""" + def test_unlimited_small_step(self): + """Test site.randompages() without limit.""" + mysite = self.get_site() + pages = [] + for rndpage in mysite.randompages(step=5, total=None): + self.assertIsInstance(rndpage, pywikibot.Page) + self.assertNotIn(rndpage, pages) + pages.append(rndpage) + if len(pages) == 11: + break + self.assertEqual(len(pages), 11) + + def test_limit_10(self): + """Test site.randompages() with limit.""" mysite = self.get_site() rn = list(mysite.randompages(total=10)) self.assertLessEqual(len(rn), 10) self.assertTrue(all(isinstance(a_page, pywikibot.Page) for a_page in rn)) self.assertFalse(all(a_page.isRedirectPage() for a_page in rn)) + + def test_redirects(self): + """Test site.randompages() with redirects.""" + mysite = self.get_site() for rndpage in mysite.randompages(total=5, redirects=True): self.assertIsInstance(rndpage, pywikibot.Page) self.assertTrue(rndpage.isRedirectPage()) + + def test_namespaces(self): + """Test site.randompages() with namespaces.""" + mysite = self.get_site() for rndpage in mysite.randompages(total=5, namespaces=[6, 7]): self.assertIsInstance(rndpage, pywikibot.Page) self.assertIn(rndpage.namespace(), [6, 7])