jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/563714 )
Change subject: [tests] Skip redirected urls in generate_family_files_tests ......................................................................
[tests] Skip redirected urls in generate_family_files_tests
A lot of wikisource urls of siteinfo are redirected to mul main page and the pywikibot._code_fam_from_url function fails because it cannot find the site code in the family file. Skip the test_attributes_after_run test of TestGenerateFamilyFiles class in this case until we have an exception list of derived sites in our family file.
- Load 10 different MWSite instances within FamilyTestGenerator.getapis instead of only two. These are needed to check whether an url is redirected. - Remove allow_failure decorator and implement the behaviour inside the test to enable subTest context manager. - Shorten self.generator_instance to gen. - Use subTest for all tests. - Additional test whether the default site is loaded. - Shorten the test whether gen.prefixed are in gen.wikis by using assertCountEqual; take into account that the default site could be selected via choice in getapis. - Run through all langs from siteinfo but filter them; only process those which are loaded and a MWSite exists. - Check whether the siteinfo url is redirected and skip test in that case. - Also skip AssertionError due to T194138 which has the same effect as allow_failure decorator to skip tests.
Bug: T241413 Change-Id: I1e0c2a5dc8cb0b9cbaac4a09b7b51b6c28775025 --- M tests/generate_family_files_tests.py 1 file changed, 56 insertions(+), 13 deletions(-)
Approvals: Mpaa: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/generate_family_files_tests.py b/tests/generate_family_files_tests.py index 236e697..e8471ac 100644 --- a/tests/generate_family_files_tests.py +++ b/tests/generate_family_files_tests.py @@ -10,22 +10,36 @@ from random import choice
from pywikibot import Site +from pywikibot.tools import PY2
from tests.aspects import unittest, DefaultSiteTestCase -from tests.utils import allowed_failure
import generate_family_file
+if not PY2: + from urllib.parse import urlparse +else: + from urlparse import urlparse +
class FamilyTestGenerator(generate_family_file.FamilyFileGenerator):
"""Family file test creator."""
def getapis(self): - """Only load additional two additional wikis randomly.""" + """Only load additional ten additional different wikis randomly.""" save = self.langs - self.langs = [choice(save), choice(save)] - self.prefixes = [item['prefix'] for item in self.langs] + seen = set() + self.langs = [] + + while len(seen) < 10: + new = choice(save) + key = new['prefix'] + if key not in seen: + seen.add(key) + self.langs.append(new) + + self.prefixes = list(seen) super(FamilyTestGenerator, self).getapis() self.langs = save
@@ -55,17 +69,46 @@ self.assertIsInstance(self.generator_instance.wikis, dict) self.assertIsInstance(self.generator_instance.langs, list)
- @allowed_failure # T194138 def test_attributes_after_run(self): """Test FamilyFileGenerator attributes after run().""" - self.generator_instance.run() - langs = [self.site.lang] + self.generator_instance.prefixes - for lang in langs: - self.assertIn(lang, self.generator_instance.wikis) - for i in range(10): - lang = choice(self.generator_instance.langs) - site = Site(url=lang['url']) - self.assertEqual(site.lang, lang['prefix']) + gen = self.generator_instance + gen.run() + + with self.subTest(test='Test whether default is loaded'): + self.assertIn(self.site.lang, gen.wikis) + + with self.subTest(test='Test element counts'): + if self.site.lang not in gen.prefixes: + gen.prefixes += [self.site.lang] + self.assertCountEqual(gen.prefixes, gen.wikis) + + # test creating Site from url + # only test Sites for downloaded wikis (T241413) + for language in filter(lambda x: x['prefix'] in gen.wikis, gen.langs): + lang = language['prefix'] + url = language['url'] + wiki = gen.wikis[lang] + lang_parse = urlparse(url) + wiki_parse = urlparse(wiki.server) + + with self.subTest(url=url): + if lang_parse.netloc != wiki_parse.netloc: + # skip redirected url (T241413) + self.skipTest( + '{} is redirected to {}' + .format(lang_parse.netloc, wiki_parse.netloc)) + + site = Site(url=url) + + try: # T194138 to be solved + self.assertEqual(site.lang, lang, + 'url has lang "{lang}" ' + 'but Site {site} has lang "{site.lang}"' + .format(site=site, lang=lang)) + except AssertionError: + self.skipTest('KNOWN BUG: url has lang "{lang}" ' + 'but Site {site} has lang "{site.lang}"' + .format(site=site, lang=lang))
if __name__ == '__main__': # pragma: no cover
pywikibot-commits@lists.wikimedia.org