Source code for tests.generate_family_file_tests

#!/usr/bin/env python3
"""Test generate_family_file script."""
# (C) Pywikibot team, 2018-2024
# Distributed under the terms of the MIT license.
from __future__ import annotations

import unittest
from contextlib import suppress
from random import sample
from urllib.parse import urlparse

from pywikibot import Site
from import WikimediaFamily
from pywikibot.scripts import generate_family_file
from tests.aspects import DefaultSiteTestCase
from tests.utils import skipping

[docs] class FamilyTestGenerator(generate_family_file.FamilyFileGenerator): """Family file test creator."""
[docs] def getapis(self): """Only load up to additional ten different wikis randomly.""" save = self.langs prefixes = {lang['prefix'] for lang in self.langs} tests = set(sample(list(prefixes), min(len(prefixes), 10))) # add closed wiki due to T334714 if 'ii' in prefixes and 'ii' not in tests: tests.add('ii') # collect wikis self.langs = [] for wiki in save: code = wiki['prefix'] if code in tests: self.langs.append(wiki) tests.remove(code) if not tests: break super().getapis() # super().getapis() might change self.langs self.prefixes = [item['prefix'] for item in self.langs] self.langs = save
[docs] def writefile(self, verify): """Pass writing."""
[docs] class TestGenerateFamilyFile(DefaultSiteTestCase): """Test generate_family_file functionality.""" familyname = 'testgff'
[docs] @classmethod def setUpClass(cls): """Set up tests class.""" super().setUpClass() # test fails on wowwiki (T297042) if == 'wowwiki': raise unittest.SkipTest(f'skipping {} due to T297042')
[docs] def setUp(self): """Set up tests.""" super().setUp() answer = 's' if isinstance(, WikimediaFamily) else 'y' self.generator_instance = FamilyTestGenerator(''), name=self.familyname, dointerwiki=answer)
[docs] def test_initial_attributes(self): """Test initial FamilyFileGenerator attributes.""" self.assertEqual(self.generator_instance.base_url,'')) self.assertEqual(, self.familyname) self.assertIn(self.generator_instance.dointerwiki, ['s', 'y']) self.assertIsInstance(self.generator_instance.wikis, dict) self.assertIsInstance(self.generator_instance.langs, list)
[docs] def test_attributes_after_run(self): """Test FamilyFileGenerator attributes after run().""" gen = self.generator_instance with self.subTest(test='Test whether default is loaded'): self.assertIn(, gen.wikis) # Subtest fails on musicbrainz (T130381) and wsbeta (T243669) if not in ('wsbeta', 'musicbrainz'): with self.subTest(test='Test element counts'): if not in gen.prefixes: gen.prefixes.append( 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(f'{lang_parse.netloc} is redirected to ' f'{wiki_parse.netloc}') site = Site(url=url) with skipping(AssertionError, msg='KNOWN BUG (T194138): url has lang "{lang}" ' 'but Site {site} has lang "{site.lang}"' .format(site=site, lang=lang)): self.assertEqual(site.lang, lang, 'url has lang "{lang}" ' 'but Site {site} has lang "{site.lang}"' .format(site=site, lang=lang))
if __name__ == '__main__': with suppress(SystemExit): unittest.main()