jenkins-bot has submitted this change and it was merged.
Change subject: Perform site tests against the config site ......................................................................
Perform site tests against the config site
Add WikimediaDefaultSiteTestCase to specify the test class site should be a WMF site, using the default site if it is a WMF site.
Run the site tests against the config defined site, or a WMF site as required.
The travis build matrix will catch differences on each defined site, for each platform.
Change-Id: I466f87f3d7db30ccfa75f6425c2383822a1ae307 --- M tests/aspects.py M tests/site_tests.py 2 files changed, 157 insertions(+), 42 deletions(-)
Approvals: XZise: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/aspects.py b/tests/aspects.py index 21a76d6..4e120ea 100644 --- a/tests/aspects.py +++ b/tests/aspects.py @@ -19,7 +19,6 @@ skip if the user is blocked. sysop flag, implement in site & page, and possibly some of the script tests. - wikimedia flag labs flag, for wikidataquery slow flag wikiquerydata - quite slow @@ -38,6 +37,7 @@
from pywikibot import config, Site from pywikibot.site import BaseSite +from pywikibot.family import WikimediaFamily
import tests from tests import unittest, patch_request, unpatch_request @@ -469,6 +469,48 @@ code = config.mylang
+class WikimediaSiteTestCase(TestCase): + + """Test class uses only WMF sites.""" + + wmf = True + + +class WikimediaDefaultSiteTestCase(DefaultSiteTestCase, WikimediaSiteTestCase): + + """Test class to run against a WMF site, preferring the default site.""" + + @classmethod + def setUpClass(cls): + """ + Set up the test class. + + Check that the default site is a Wikimedia site. + Use en.wikipedia.org as a fallback. + """ + super(WikimediaDefaultSiteTestCase, cls).setUpClass() + + assert(hasattr(cls, 'site') and hasattr(cls, 'sites')) + + assert(len(cls.sites) == 1) + + site = cls.get_site() + + if not isinstance(site.family, WikimediaFamily): + print('%s using English Wikipedia instead of non-WMF config.family %s.' + % (cls.__name__, cls.family)) + cls.family = 'wikipedia' + cls.code = 'en' + cls.site = pywikibot.Site('en', 'wikipedia') + cls.sites = { + cls.site: { + 'family': 'wikipedia', + 'code': 'en', + 'site': cls.site + } + } + + class WikibaseTestCase(TestCase):
"""Run tests against a wikibase site.""" diff --git a/tests/site_tests.py b/tests/site_tests.py index 5bbdc0e..26f514e 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -15,21 +15,27 @@ import re
import pywikibot -from tests.aspects import unittest, TestCase +from tests.aspects import ( + unittest, TestCase, + DefaultSiteTestCase, + WikimediaDefaultSiteTestCase, +)
if sys.version_info[0] > 2: basestring = (str, ) unicode = str
-class TestSiteObject(TestCase): +class TestSiteObject(DefaultSiteTestCase):
"""Test cases for Site methods."""
- family = "wikipedia" - code = "en" - cached = True + + def testPickleAbility(self): + import pickle + mysite = self.get_site() + pickle.dumps(mysite)
def testBaseMethods(self): """Test cases for BaseSite methods.""" @@ -37,7 +43,7 @@ self.assertEqual(mysite.family.name, self.family) self.assertEqual(mysite.code, self.code) self.assertIsInstance(mysite.lang, basestring) - self.assertEqual(mysite, pywikibot.Site("en", "wikipedia")) + self.assertEqual(mysite, pywikibot.Site(self.code, self.family)) self.assertIsInstance(mysite.user(), (basestring, type(None))) self.assertEqual(mysite.sitename(), "%s:%s" % (self.family, @@ -47,8 +53,16 @@ % (self.code, self.family)) self.assertIsInstance(mysite.linktrail(), basestring) self.assertIsInstance(mysite.redirect(default=True), basestring) - self.assertIsInstance(mysite.disambcategory(), pywikibot.Category) - self.assertEqual(unicode(pywikibot.Link("foo", source=mysite)), u"[[Foo]]") + try: + dabcat = mysite.disambcategory() + except pywikibot.Error as e: + self.assertIn('No disambiguation category name found', str(e)) + else: + self.assertIsInstance(dabcat, pywikibot.Category) + + foo = unicode(pywikibot.Link("foo", source=mysite)) + self.assertEqual(foo, u"[[foo]]" if mysite.nocapitalize else u"[[Foo]]") + self.assertFalse(mysite.isInterwikiLink("foo")) self.assertIsInstance(mysite.redirectRegex().pattern, basestring) self.assertIsInstance(mysite.category_on_one_line(), bool) @@ -125,30 +139,6 @@ for key in ns for item in mysite.namespace(key, True)))
- def testNamespaceCase(self): - site = pywikibot.Site('en', 'wiktionary') - main_namespace = site.namespaces()[0] - self.assertEqual(main_namespace.case, 'case-sensitive') - user_namespace = site.namespaces()[2] - self.assertEqual(user_namespace.case, 'first-letter') - - def testNamespaceAliases(self): - site = pywikibot.Site('nn', 'wikipedia') - - namespaces = site.namespaces() - image_namespace = namespaces[6] - self.assertEqual(image_namespace.custom_name, 'Fil') - self.assertEqual(image_namespace.canonical_name, 'File') - self.assertEqual(str(image_namespace), ':File:') - self.assertEqual(unicode(image_namespace), ':Fil:') - self.assertEqual(image_namespace.aliases, ['Image']) - self.assertEqual(len(image_namespace), 3) - - self.assertEqual(len(namespaces[1].aliases), 0) - self.assertEqual(len(namespaces[4].aliases), 1) - self.assertEqual(namespaces[4].aliases[0], 'WP') - self.assertIn('WP', namespaces[4]) - def testApiMethods(self): """Test generic ApiSite methods.""" mysite = self.get_site() @@ -198,8 +188,16 @@
self.assertIsInstance(mysite.siteinfo, pywikibot.site.Siteinfo) self.assertIsInstance(mysite.months_names, list) - self.assertEqual(mysite.months_names[4], (u'May', u'May')) self.assertEqual(mysite.list_to_text(('pywikibot',)), 'pywikibot') + + def testEnglishSpecificMethods(self): + """Test Site methods using English specific inputs and outputs.""" + mysite = self.get_site() + if mysite.lang != 'en': + raise unittest.SkipTest( + 'English-specific tests not valid on %s' % mysite) + + self.assertEqual(mysite.months_names[4], (u'May', u'May')) self.assertEqual(mysite.list_to_text(('Pride', 'Prejudice')), 'Pride and Prejudice') self.assertEqual(mysite.list_to_text(('This', 'that', 'the other')), 'This, that and the other')
@@ -225,7 +223,12 @@ """Test ability to get page tokens.""" mysite = self.get_site() for ttype in ("edit", "move"): # token types for non-sysops - token = mysite.tokens[ttype] + try: + token = self.site.tokens[ttype] + except KeyError: + raise unittest.SkipTest( + "Testing '%s' token not possible with user on %s" + % (ttype, self.site)) self.assertIsInstance(token, basestring) self.assertEqual(token, mysite.tokens[ttype])
@@ -251,6 +254,10 @@ def testItemPreload(self): """Test that ItemPage preloading works.""" mysite = self.get_site() + if not mysite.has_data_repository: + raise unittest.SkipTest('%s does not have a data repository' + % mysite) + datasite = mysite.data_repository()
items = [pywikibot.ItemPage(datasite, 'q' + str(num)) for num in range(1, 6)] @@ -388,6 +395,10 @@ for page in mysite.allpages(maxsize=200, total=5): self.assertIsInstance(page, pywikibot.Page) self.assertTrue(mysite.page_exists(page)) + if len(page.text) > 200 and mysite.data_repository() == mysite: + print('%s.text is > 200 bytes while raw JSON is <= 200' + % page) + continue self.assertLessEqual(len(page.text), 200) for page in mysite.allpages(protect_type="edit", total=5): self.assertIsInstance(page, pywikibot.Page) @@ -737,11 +748,12 @@ """Test the site.search() method""" mysite = self.get_site() try: - se = list(mysite.search("wiki", total=10)) - self.assertLessEqual(len(se), 10) + se = list(mysite.search("wiki", total=100)) + self.assertLessEqual(len(se), 100) self.assertTrue(all(isinstance(hit, pywikibot.Page) for hit in se)) - self.assertTrue(all(hit.namespace() == 0 for hit in se)) + search_ns = [ns.id for ns in mysite.get_searched_namespaces(force=True)] + self.assertTrue(all(hit.namespace() in search_ns for hit in se)) for hit in mysite.search("common", namespaces=4, total=5): self.assertIsInstance(hit, pywikibot.Page) self.assertEqual(hit.namespace(), 4) @@ -991,6 +1003,13 @@ # TODO: test newimages, longpages, shortpages, ancientpages, unwatchedpages # and the other following methods in site.py
+ +class TestSiteExtensions(WikimediaDefaultSiteTestCase): + + """Test cases for Site extensions.""" + + cached = True + def testExtensions(self): mysite = self.get_site() # test automatically getting extensions cache @@ -1002,6 +1021,16 @@ self.assertTrue(mysite.has_extension('disambiguator'))
self.assertFalse(mysite.has_extension('ThisExtensionDoesNotExist')) + + +class TestSiteAPILimits(TestCase): + + """Test cases for Site method that use API limits.""" + + family = 'wikipedia' + code = 'en' + + cached = True
def test_API_limits_with_site_methods(self): # test step/total parameters for different sitemethods @@ -1024,13 +1053,16 @@ mysite.loadrevisions(mypage, step=5, total=12) self.assertEqual(len(mypage._revisions), 12)
- def testPickleAbility(self): - site = self.get_site() - import pickle - pickle.dumps(site) + +class TestSiteInfo(WikimediaDefaultSiteTestCase): + + """Test cases for Site metadata and capabilities.""" + + cached = True
def testSiteinfo(self): """Test the siteinfo property.""" + mysite = self.get_site() # general enteries mysite = self.get_site() self.assertIsInstance(mysite.siteinfo['timeoffset'], (int, float)) @@ -1210,6 +1242,47 @@ self.assertEqual(ll.site.family.name, 'wikipedia')
+class TestWiktionarySite(TestCase): + + family = 'wiktionary' + code = 'en' + + cached = True + + def testNamespaceCase(self): + site = self.get_site() + + main_namespace = site.namespaces()[0] + self.assertEqual(main_namespace.case, 'case-sensitive') + user_namespace = site.namespaces()[2] + self.assertEqual(user_namespace.case, 'first-letter') + + +class TestNonEnglishWikipediaSite(TestCase): + + family = 'wikipedia' + code = 'nn' + + cached = True + + def testNamespaceAliases(self): + site = self.get_site() + + namespaces = site.namespaces() + image_namespace = namespaces[6] + self.assertEqual(image_namespace.custom_name, 'Fil') + self.assertEqual(image_namespace.canonical_name, 'File') + self.assertEqual(str(image_namespace), ':File:') + self.assertEqual(unicode(image_namespace), ':Fil:') + self.assertEqual(image_namespace.aliases, ['Image']) + self.assertEqual(len(image_namespace), 3) + + self.assertEqual(len(namespaces[1].aliases), 0) + self.assertEqual(len(namespaces[4].aliases), 1) + self.assertEqual(namespaces[4].aliases[0], 'WP') + self.assertIn('WP', namespaces[4]) + + class TestUploadEnabledSite(TestCase):
sites = {