def setUpClass(cls): """Set up test class.""" super(TestIsbnWikibaseBot, cls).setUpClass() # Check if the unit test item page and the property both exist item_ns = cls.get_repo().item_namespace for page in cls.get_site().search('IsbnWikibaseBotUnitTest', total=1, namespaces=item_ns): cls.test_page_qid = page.title() item_page = ItemPage(cls.get_repo(), page.title()) for pid, claims in item_page.get()['claims'].items(): for claim in claims: prop_page = pywikibot.PropertyPage(cls.get_repo(), claim.getID()) prop_page.get() if ('ISBN-10' in prop_page.labels.values() and claim.getTarget() == '097522980x'): return raise unittest.SkipTest(u'%s: "ISBN-10" property was not found in ' u'"IsbnWikibaseBotUnitTest" item page' % cls.__name__) raise unittest.SkipTest( u'%s: "IsbnWikibaseBotUnitTest" item page was not found' % cls.__name__)
def test_unconnected_with_repo(self): """Test that the ItemPage returned raises NoPage.""" if not self.site.data_repository(): raise unittest.SkipTest('Site is not using a Wikibase repository') if self.site.hostname() == 'test.wikipedia.org': raise unittest.SkipTest('test.wikipedia is misconfigured; T85358') cnt = 0 start_time = datetime.datetime.now() - datetime.timedelta(minutes=5) # Pages which have been connected recently may still be reported as # unconnected. So try on an version that is a few minutes older if the # tested site appears as a sitelink. for page in self.site.unconnected_pages(total=5): try: item = pywikibot.ItemPage.fromPage(page) except pywikibot.NoPage: pass else: revisions = list( item.revisions(total=1, starttime=start_time, content=True)) if revisions: sitelinks = json.loads(revisions[0].text)['sitelinks'] self.assertNotIn( self.site.dbName(), sitelinks, 'Page "{0}" is connected to a Wikibase ' 'repository'.format(page.title())) cnt += 1 self.assertLessEqual(cnt, 5)
def setUpClass(cls): super(LiveRCPageGeneratorTestCase, cls).setUpClass() try: import socketIO_client # noqa except ImportError: raise unittest.SkipTest('socketIO_client not available') if LooseVersion(socketIO_client.__version__) >= LooseVersion('0.6.1'): raise unittest.SkipTest( 'socketIO_client %s not supported by Wikimedia-Stream' % socketIO_client.__version__)
def setUpClass(cls): """Verify that a translation does not yet exist.""" if 'userinterface_lang' in pywikibot.config.__modified__: raise unittest.SkipTest( 'user-config has a modified userinterface_lang') super(InputTestCase, cls).setUpClass() if cls.code in i18n.twget_keys(cls.message): raise unittest.SkipTest('%s has a translation for %s' % (cls.code, cls.message))
def _get_param_values(self, site, module, parameter): """Perform check that a parameter matches the expected list.""" try: param = site._paraminfo.parameter(module, parameter) except ValueError: raise unittest.SkipTest( 'Paraminfo for {0} could not be loaded'.format(module)) if not param or 'type' not in param: raise unittest.SkipTest('No defined values for {0}.{1}'.format( module, parameter)) return param['type']
def setUp(self): """Check if OAuth extension is installed and OAuth tokens are set.""" super(OAuthSiteTestCase, self).setUp() self.site = self.get_site() if not self.site.has_extension('OAuth'): raise unittest.SkipTest('OAuth extension not loaded on test site') tokens = self._get_oauth_tokens() if tokens is None: raise unittest.SkipTest('OAuth tokens not set') self.assertEqual(len(tokens), 4) self.consumer_token = tokens[:2] self.access_token = tokens[2:]
def setUpClass(cls): """Skip test gracefully if i18n package is missing.""" super(TestValidTemplate, cls).setUpClass() if not i18n.messages_available(): raise unittest.SkipTest( "i18n messages package '{}' not available.".format( i18n._messages_package_name))
def test_template(self): """Test validity of template.""" lang = site.lang if lang not in keys: return if not i18n.twhas_key(lang, package): return msg = i18n.twtranslate(lang, package, fallback=False) # check whether the message contains a template templates = extract_templates_and_params_regex_simple(msg) self.assertIsInstance(templates, list) self.assertIsNotEmpty(templates) # known problems if (package == PACKAGES[0] and site.code in ['simple', 'test2'] or package == PACKAGES[1] and site.code == 'test'): raise unittest.SkipTest( "{site} wiki has '{site.lang}' language code but " "missing template for package '{package}'. Must be " 'solved by the corresponding script.'.format( site=site, package=package)) # check whether template exists title = templates[0][0] page = pywikibot.Page(site, title, ns=10) self.assertTrue(page.exists(), msg='Invalid L10N in package "{package}"\n' 'template "{title}" does not exist for lang ' '"{site.lang}" on site "{site}"'.format( package=package, title=title, site=site))
def setUpClass(cls): """Setup test class.""" super(TestLogeventsFactoryGenerator, cls).setUpClass() site = pywikibot.Site() newuser_logevents = list(site.logevents(logtype='newusers', total=1)) if len(newuser_logevents) == 0: raise unittest.SkipTest('No newuser logs found to test with.')
def test_extract_templates_params_mwpfh(self): try: import mwparserfromhell # noqa except ImportError: raise unittest.SkipTest('mwparserfromhell not available') func = textlib.extract_templates_and_params_mwpfh self._extract_templates_params(func) self.assertEqual(func('{{a|}}'), [('a', OrderedDict((('1', ''), )))]) self.assertEqual(func('{{a| b=c}}'), [('a', OrderedDict( ((' b', 'c'), )))]) self.assertEqual(func('{{a|b =c}}'), [('a', OrderedDict( (('b ', 'c'), )))]) self.assertEqual(func('{{a|b= c}}'), [('a', OrderedDict( (('b', ' c'), )))]) self.assertEqual(func('{{a|b=c }}'), [('a', OrderedDict( (('b', 'c '), )))]) self.assertEqual(func('{{a| b={{c}}}}'), [('a', OrderedDict(((' b', '{{c}}'), ))), ('c', OrderedDict())]) self.assertEqual(func('{{a|b={{c}}}}'), [('a', OrderedDict((('b', '{{c}}'), ))), ('c', OrderedDict())]) self.assertEqual(func('{{a|b= {{c}}}}'), [('a', OrderedDict((('b', ' {{c}}'), ))), ('c', OrderedDict())]) self.assertEqual(func('{{a|b={{c}} }}'), [('a', OrderedDict((('b', '{{c}} '), ))), ('c', OrderedDict())]) self.assertEqual(func('{{a|b=<!--{{{1}}}-->}}'), [('a', OrderedDict((('b', '<!--{{{1}}}-->'), )))])
def setUpClass(cls): """Verify that the translations are available.""" if not i18n.messages_available(): raise unittest.SkipTest( "i18n messages package '{}' not available.".format( i18n._messages_package_name)) super().setUpClass()
def setUpClass(cls): """Verify that a translation does not yet exist.""" super(InputTestCase, cls).setUpClass() if cls.code in i18n.twget_keys(cls.message): raise unittest.SkipTest('%s has a translation for %s' % (cls.code, cls.message))
def test_template(self): """Test validity of template.""" lang = site.lang if lang not in keys: return msg = i18n.twtranslate(lang, package, fallback=False) if msg: # check whether the message contains a template templates = extract_templates_and_params_regex_simple(msg) self.assertIsInstance(templates, list) self.assertIsNotEmpty(templates) # known problem if site.code == 'simple': raise unittest.SkipTest( "'simple' wiki has 'en' language code but " 'missing template. Must be solved by the ' 'corresponding script.') # check whether template exists title = templates[0][0] page = pywikibot.Page(site, title, ns=10) self.assertTrue( page.exists(), msg='Invalid L10N in package "{package}"\n' 'template "{title}" does not exist for lang ' '"{site.lang}" on site "{site}"' .format(package=package, title=title, site=site))
def _test_tokens(self, version, test_version, additional_token): """Test tokens.""" if version and (self._version < version or self._version < test_version): raise unittest.SkipTest( 'Site {} version {} is too low for this tests.'.format( self.mysite, self._version)) self.mysite.version = lambda: test_version del self.mysite._mw_version_time # remove cached mw_version redirected_tokens = ['edit', 'move', 'delete'] for ttype in redirected_tokens + ['patrol', additional_token]: try: token = self.mysite.tokens[ttype] except Error as error_msg: if self.mysite.validate_tokens([ttype]): pattern = ("Action '[a-z]+' is not allowed " 'for user .* on .* wiki.') else: pattern = "Requested token '[a-z]+' is invalid on .* wiki." self.assertRegex(str(error_msg), pattern) else: self.assertIsInstance(token, str) self.assertEqual(token, self.mysite.tokens[ttype]) # test __contains__ if test_version < '1.24wmf19': self.assertIn(ttype, self.mysite.tokens) elif ttype in redirected_tokens: self.assertEqual(self.mysite.tokens[ttype], self.mysite.tokens['csrf'])
def testLinks(self): """Test the different types of links from a page.""" if self.site.family.name == 'wpbeta': raise unittest.SkipTest('Test fails on betawiki; T69931') mainpage = self.get_mainpage() for p in mainpage.linkedPages(): self.assertIsInstance(p, pywikibot.Page) iw = list(mainpage.interwiki(expand=True)) for p in iw: self.assertIsInstance(p, pywikibot.Link) for p2 in mainpage.interwiki(expand=False): self.assertIsInstance(p2, pywikibot.Link) self.assertIn(p2, iw) for p in mainpage.langlinks(): self.assertIsInstance(p, pywikibot.Link) for p in mainpage.imagelinks(): self.assertIsInstance(p, pywikibot.FilePage) for p in mainpage.templates(): self.assertIsInstance(p, pywikibot.Page) for t, params in mainpage.templatesWithParams(): self.assertIsInstance(t, pywikibot.Page) self.assertIsInstance(params, list) for p in mainpage.categories(): self.assertIsInstance(p, pywikibot.Category) for p in mainpage.extlinks(): self.assertIsInstance(p, unicode)
def test_https_cert_invalid(self): """Verify vikidia SSL certificate is invalid.""" try: from pyasn1_modules import pem, rfc2459 from pyasn1.codec.der import decoder except ImportError: raise unittest.SkipTest('pyasn1 and pyasn1_modules not available.') import ssl import io cert = ssl.get_server_certificate(addr=('en.vikidia.org', 443)) s = io.StringIO(unicode(cert)) substrate = pem.readPemFromFile(s) cert = decoder.decode(substrate, asn1Spec=rfc2459.Certificate())[0] tbs_cert = cert.getComponentByName('tbsCertificate') issuer = tbs_cert.getComponentByName('issuer') organisation = None for rdn in issuer.getComponent(): for attr in rdn: attr_type = attr.getComponentByName('type') if attr_type == rfc2459.id_at_organizationName: value, _ = decoder.decode(attr.getComponentByName('value'), asn1Spec=rfc2459.X520name()) organisation = str(value.getComponent()) break self.assertEqual(organisation, 'TuxFamily.org non-profit organization')
def test_write_archive_xz(self): """Test writing a xz archive.""" if isinstance(tools.lzma, ImportError): raise unittest.SkipTest('lzma not importable') content = self._write_content('.xz') self.assertEqual(content[:6], b'\xFD7zXZ\x00')
def setUpClass(cls): """Verify that a translation does not yet exist.""" super().setUpClass() if cls.code in i18n.twget_keys(cls.message): raise unittest.SkipTest('{} has a translation for {}'.format( cls.code, cls.message))
def setUpClass(cls): if cls.code in i18n.twget_keys('pywikibot-enter-category-name'): raise unittest.SkipTest( '%s has a translation for %s' % (cls.code, 'pywikibot-enter-category-name')) super(InputTestCase, cls).setUpClass()
def setup_test_pages(self): """Helper function to set up pages that we will use in these tests.""" site = self.get_site() source = pywikibot.Page(site, 'User:Sn1per/MergeTest1') dest = pywikibot.Page(site, 'User:Sn1per/MergeTest2') # Make sure the wiki supports action=mergehistory if site.mw_version < '1.27.0-wmf.13': raise unittest.SkipTest('Wiki version must be 1.27.0-wmf.13 or ' 'newer to support the history merge API.') if source.exists(): source.delete('Pywikibot merge history unit test') if dest.exists(): dest.delete('Pywikibot merge history unit test') source.text = 'Lorem ipsum dolor sit amet' source.save() first_rev = source.editTime() source.text = 'Lorem ipsum dolor sit amet is a common test phrase' source.save() second_rev = source.editTime() dest.text = 'Merge history page unit test destination' dest.save() return first_rev, second_rev
def test_valid_lagpattern(self): """Test whether api.lagpattern is valid.""" mysite = self.get_site() if mysite.siteinfo['dbrepllag'][0]['lag'] == -1: raise unittest.SkipTest( '{0} is not running on a replicated database cluster.'.format( mysite)) mythrottle = DummyThrottle(mysite) mysite._throttle = mythrottle params = { 'action': 'query', 'titles': self.get_mainpage().title(), 'maxlag': -1 } req = api.Request(site=mysite, parameters=params) try: req.submit() except SystemExit: pass # expected exception from DummyThrottle instance except api.APIError as e: pywikibot.warning( 'Wrong api.lagpattern regex, cannot retrieve lag value') raise e self.assertIsInstance(mythrottle._lagvalue, int) self.assertGreaterEqual(mythrottle._lagvalue, 0) self.assertIsInstance(mythrottle.retry_after, int) self.assertGreaterEqual(mythrottle.retry_after, 0)
def _test_tokens(self, version, test_version, additional_token): """Test tokens.""" if version and (self._version < version or self._version < test_version): raise unittest.SkipTest( 'Site {} version {} is too low for this tests.' .format(self.mysite, self._version)) self.mysite.version = lambda: test_version for ttype in ('edit', 'move', 'delete', 'patrol', additional_token): tokentype = self.mysite.validate_tokens([ttype]) try: token = self.mysite.tokens[ttype] except pywikibot.Error as error_msg: if tokentype: self.assertRegex( str(error_msg), "Action '[a-z]+' is not allowed " 'for user .* on .* wiki.') # test __contains__ self.assertNotIn(tokentype[0], self.mysite.tokens) else: self.assertRegex( str(error_msg), "Requested token '[a-z]+' is invalid on .* wiki.") else: self.assertIsInstance(token, str) self.assertEqual(token, self.mysite.tokens[ttype]) # test __contains__ self.assertIn(tokentype[0], self.mysite.tokens)
def test_protected(self): """Test that protected titles raise the appropriate exception.""" if self.site.has_group('sysop'): raise unittest.SkipTest( 'Testing failure of edit protected with a sysop account') page = pywikibot.Page(self.site, 'Wikipedia:Create a new page') self.assertRaises(LockedPage, page.save)
def test_Coordinate_edit(self): """Attempt adding a Coordinate with globe set via item.""" testsite = self.get_repo() item = self._clean_item(testsite, 'P20480') # Make sure the wiki supports wikibase-conceptbaseuri version = testsite.version() if MediaWikiVersion(version) < MediaWikiVersion('1.29.0-wmf.2'): raise unittest.SkipTest('Wiki version must be 1.29.0-wmf.2 or ' 'newer to support unbound uncertainties.') # set new claim claim = pywikibot.page.Claim(testsite, 'P20480', datatype='globe-coordinate') target = pywikibot.Coordinate(site=testsite, lat=12.0, lon=13.0, globe_item=item) claim.setTarget(target) item.addClaim(claim) # confirm new claim item.get(force=True) claim = item.claims['P20480'][0] self.assertEqual(claim.getTarget(), target)
def setUpClass(cls): """Verify that the translations are available.""" if not i18n.messages_available(): raise unittest.SkipTest( "i18n messages package '%s' not available." % i18n._messages_package_name) super(TWNBotTestCase, cls).setUpClass()
def setUpClass(cls): """SetUp tests.""" super(TestDjVuFile, cls).setUpClass() try: subprocess.Popen(['djvudump'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) except OSError: raise unittest.SkipTest('djvulibre library not installed.')
def setUpClass(cls): """Set up the test class.""" super(WikibaseKnownTypesTests, cls).setUpClass() for site in cls.sites.values(): if site['site'].family.name.endswith('beta'): raise unittest.SkipTest( '{}: skipping {!r} due to Bug T210322'.format( cls.__name__, site['site']))
def setUpClass(cls): """Verify that a translation does not yet exist.""" super(InputTestCase, cls).setUpClass() if cls.code in i18n.twget_keys('pywikibot-enter-category-name'): raise unittest.SkipTest( '%s has a translation for %s' % (cls.code, 'pywikibot-enter-category-name'))
def test_unconnected_without_repo(self): """Test that it raises a ValueError on sites without repository.""" if self.site.data_repository(): raise unittest.SkipTest('Site is using a Wikibase repository') with self.assertRaises(ValueError): for page in pagegenerators.UnconnectedPageGenerator(self.site, total=5): assert False # this shouldn't be reached
def testExtractTemplates(self): if not (pywikibot.config.use_mwparserfromhell and mwparserfromhell): raise unittest.SkipTest('mwparserfromhell not available or enabled') func = textlib.extract_templates_and_params # It's really long. self.assertEqual(func('{{a}}'), [('a', {})]) self.assertEqual(func('{{a|b=c}}'), [('a', {'b': 'c'})]) self.assertEqual(func('{{a|b|c=d}}'), [('a', {u'1': 'b', 'c': 'd'})]) self.assertEqual(func('{{a|b={{c}}}}'), [(u'a', {u'b': u'{{c}}'}), ('c', {})])