jenkins-bot has submitted this change and it was merged.
Change subject: Change pagegenerator tests to dry tests ......................................................................
Change pagegenerator tests to dry tests
Adjust NamespaceFilterPageGenerator such that the site parameter is only needed if namespaces contains strings that are not integers.
Update many of the pagegenerators tests to work using a dry site.
Change-Id: I25ee1f17f6c0478da43c8c2e1576b69f0140b737 --- M pywikibot/pagegenerators.py M tests/pagegenerators_tests.py 2 files changed, 31 insertions(+), 17 deletions(-)
Approvals: Ladsgroup: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py index b79dfda..928652d 100644 --- a/pywikibot/pagegenerators.py +++ b/pywikibot/pagegenerators.py @@ -871,6 +871,7 @@ @param total: Maxmum number of pages to retrieve in total @type total: int @param namespaces: list of namespace numbers to fetch contribs from + @type namespaces: list of int @param site: Site for generator results. @type site: L{pywikibot.site.BaseSite}
@@ -894,11 +895,12 @@ NOTE: API-based generators that have a "namespaces" parameter perform namespace filtering more efficiently than this generator.
- @param site: Site for generator results. + @param namespaces: list of namespace numbers to limit results + @type namespaces: list of int + @param site: Site for generator results, only needed if + namespaces contains namespace names. @type site: L{pywikibot.site.BaseSite} """ - if site is None: - site = pywikibot.Site() if isinstance(namespaces, (int, basestring)): namespaces = [namespaces] # convert namespace names to namespace numbers @@ -909,6 +911,9 @@ # namespace might be given as str representation of int index = int(ns) except ValueError: + # FIXME: deprecate providing strings as namespaces + if site is None: + site = pywikibot.Site() index = site.getNamespaceIndex(ns) if index is None: raise ValueError(u'Unknown namespace: %s' % ns) diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py index 96e5dfa..aba6751 100755 --- a/tests/pagegenerators_tests.py +++ b/tests/pagegenerators_tests.py @@ -26,7 +26,7 @@ family = 'wikipedia' code = 'en'
- cached = True + dry = True
titles = [ # just a bunch of randomly selected titles @@ -58,49 +58,49 @@
def test_PagesFromTitlesGenerator(self): self.assertFunction("PagesFromTitlesGenerator") - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) self.assertEqual(len(self.titles), len(tuple(gen)))
def test_NamespaceFilterPageGenerator(self): self.assertFunction("NamespaceFilterPageGenerator") - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) gen = pagegenerators.NamespaceFilterPageGenerator(gen, 0) self.assertEqual(len(tuple(gen)), 3) - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) gen = pagegenerators.NamespaceFilterPageGenerator(gen, 1) self.assertEqual(len(tuple(gen)), 4) - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) gen = pagegenerators.NamespaceFilterPageGenerator(gen, 10) self.assertEqual(len(tuple(gen)), 6) - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) gen = pagegenerators.NamespaceFilterPageGenerator(gen, (1, 10)) self.assertEqual(len(tuple(gen)), 10)
def test_RegexFilterPageGenerator(self): self.assertFunction("RegexFilterPageGenerator") - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) gen = pagegenerators.RegexFilterPageGenerator(gen, '/doc') self.assertEqual(len(tuple(gen)), 2) - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) gen = pagegenerators.RegexFilterPageGenerator(gen, '/doc', quantifier='none') self.assertEqual(len(tuple(gen)), 11) - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) gen = pagegenerators.RegexFilterPageGenerator(gen, ['/doc', '/meta']) self.assertEqual(len(tuple(gen)), 4) - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) gen = pagegenerators.RegexFilterPageGenerator(gen, ['/doc', '/meta'], quantifier='none') self.assertEqual(len(tuple(gen)), 9) - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) gen = pagegenerators.RegexFilterPageGenerator(gen, ['/doc', '/meta'], quantifier='all') self.assertEqual(len(tuple(gen)), 0) - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) gen = pagegenerators.RegexFilterPageGenerator(gen, ['Template', '/meta'], quantifier='all') self.assertEqual(len(tuple(gen)), 1) - gen = pagegenerators.PagesFromTitlesGenerator(self.titles) + gen = pagegenerators.PagesFromTitlesGenerator(self.titles, self.site) gen = pagegenerators.RegexFilterPageGenerator(gen, ['template', '/meta'], quantifier='any') self.assertEqual(len(tuple(gen)), 4) @@ -139,8 +139,17 @@ quantifier='none') self.assertEqual(len(tuple(gen)), 9)
+ +class TestRepeatingGenerator(TestCase): + + """Test RepeatingGenerator.""" + + family = 'wikipedia' + code = 'en' + + cached = True + def test_RepeatingGenerator(self): - self.assertFunction("RepeatingGenerator") # site.recentchanges() includes external edits (from wikidata), # so total=4 is not too high items = list(
pywikibot-commits@lists.wikimedia.org