jenkins-bot has submitted this change and it was merged.
Change subject: page.py: base isEmptyCategory on cat.categoryinfo ......................................................................
page.py: base isEmptyCategory on cat.categoryinfo
No change of functionality. Test is based on check of properties rather than try to retrieve content.
Tests added.
Change-Id: I6070c72882fbcd94a5526a158a9827738386bf51 --- M pywikibot/page.py M tests/page_tests.py 2 files changed, 13 insertions(+), 3 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index e77b23e..5d8c8f7 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -2121,9 +2121,8 @@
def isEmptyCategory(self): """Return True if category has no members (including subcategories).""" - for member in self.site.categorymembers(self, total=1): - return False - return True + ci = self.categoryinfo + return sum(ci[k] for k in ['files', 'pages', 'subcats']) == 0
def isHiddenCategory(self): """Return True if the category is hidden.""" diff --git a/tests/page_tests.py b/tests/page_tests.py index ebc37d6..05d3228 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -400,6 +400,17 @@ # def contributingUsers(self):
+class TestCategoryObject(PywikibotTestCase): + + def test_isEmptyCategory(self): + """Test if category is empty or not""" + site = pywikibot.Site('en', 'wikipedia') + cat_empty = pywikibot.Category(site, u'Category:foooooo') + cat_not_empty = pywikibot.Category(site, u'Category:Wikipedia categories') + self.assertTrue(cat_empty.isEmptyCategory()) + self.assertFalse(cat_not_empty.isEmptyCategory()) + + if __name__ == '__main__': try: unittest.main()
pywikibot-commits@lists.wikimedia.org