def test_run(self, m_query, m_items): b = Bot.factory([]) b.run() m_query.assert_called_with() m_items.assert_not_called() m_query.reset_mock() m_items.reset_mock() b = Bot.factory(['--verbose', '--item=Q1']) b.run() m_items.assert_called_with() m_query.assert_not_called()
def test_verify(self): bot = Bot.factory([ '--verbose', '--test', '--user=FLOSSbotCI', '--verification-delay=0', ]) fsd = FSD(bot, bot.args) label = 'Loomio' item = fsd.__getattribute__('Q_' + label) # get rid of leftovers in case the item already exists fsd.clear_entity_label(item.getID()) item = fsd.__getattribute__('Q_' + label) log.debug(">> do nothing if there is no Free Software Directory entry") to_verify = pywikibot.ItemPage(fsd.bot.site, item.getID(), 0) assert fsd.verify(to_verify) is None log.debug(">> add a Free Software Directory entry") entry = pywikibot.Claim(fsd.bot.site, fsd.P_Free_Software_Directory_entry, 0) entry.setTarget(label) item.addClaim(entry) log.debug(">> verified") to_verify = pywikibot.ItemPage(fsd.bot.site, item.getID(), 0) assert 'verified' == fsd.verify(to_verify) log.debug(">> no need") to_verify = pywikibot.ItemPage(fsd.bot.site, item.getID(), 0) fsd.args.verification_delay = 30 assert 'no need' == fsd.verify(to_verify) fsd.args.verification_delay = 0 fsd.clear_entity_label(item.getID())
def test_get_fsd(self, m_fetch_fsd): bot = Bot.factory([ '--verbose', '--test', '--user=FLOSSbotCI', ]) fsd = FSD(bot, bot.args) class Page: def __init__(self, _title): self._title = _title def __repr__(self): return "Page('" + self._title + "')" def title(self): return self._title m_fetch_fsd.side_effect = [ [ (Page('Template:Entry'), ['Name=Loomio']), (Page('Template:Project license'), ['License=LGPLv2']), (Page('Template:Project license'), ['License=LGPLv3']), ], ] assert ({ 'Entry': [{ 'name': 'Loomio' }], 'Project license': [{ 'license': 'LGPLv2' }, { 'license': 'LGPLv3' }], } == fsd.get_fsd('Loomio'))
def test_get_sitelink_item(self): bot = Bot.factory(['--verbose']) plugin = Plugin(bot, bot.args) enwiki = plugin.get_sitelink_item('enwiki') assert 'English Wikipedia' == enwiki.labels['en'] frwiki = plugin.get_sitelink_item('frwiki') assert 'French Wikipedia' == frwiki.labels['en']
def test_translate_title(self): bot = Bot.factory(['--verbose']) plugin = Plugin(bot, bot.args) assert 'GNU Emacs' == plugin.translate_title('GNU Emacs', 'fr') assert 'ГНУ Емакс' == plugin.translate_title('GNU Emacs', 'sr') assert 'Licence' == plugin.translate_title('License', 'fr') assert plugin.translate_title('License', '??') is None
def test_fixup(self): bot = Bot.factory([ '--verbose', '--test', '--user=FLOSSbotCI', ]) fsd = FSD(bot, bot.args) to_fixup = fsd.__getattribute__('Q_' + WikidataHelper.random_name()) assert 'not found' == fsd.fixup(to_fixup) fsd.clear_entity_label(to_fixup.getID()) to_fixup = pywikibot.ItemPage(fsd.bot.site, to_fixup.getID(), 0) assert 'no label' == fsd.fixup(to_fixup) label = 'Loomio' item = fsd.__getattribute__('Q_' + label) # get rid of leftovers in case the item already exists fsd.clear_entity_label(item.getID()) item = fsd.__getattribute__('Q_' + label) to_fixup = pywikibot.ItemPage(fsd.bot.site, item.getID(), 0) assert 'found' == fsd.fixup(to_fixup) to_fixup = pywikibot.ItemPage(fsd.bot.site, item.getID(), 0) assert 'already exists' == fsd.fixup(to_fixup) fsd.clear_entity_label(item.getID())
def setup(self): bot = Bot.factory([ '--verbose', '--test', '--user=FLOSSbotCI', ]) self.r = Repository(bot, bot.args)
def test_create_entity(self): bot = Bot.factory([ '--test', '--user=FLOSSbotCI', ]) plugin = Plugin(bot, bot.args) name = 'Q_' + WikidataHelper.random_name() item = plugin.__getattribute__(name) assert 1 == len(plugin.bot.entities['item']) plugin.clear_entity_label(item.getID()) assert 0 == len(plugin.bot.entities['item']) item = plugin.__getattribute__(name) assert 1 == len(plugin.bot.entities['item']) property2datatype = { 'P_source_code_repository': 'url', 'P_website_username': '******', 'P_protocol': 'wikibase-item', } bot = Bot.factory([ '--test', '--user=FLOSSbotCI', ]) wikidata_plugin = Plugin(bot, bot.args) for (attr, datatype) in property2datatype.items(): plugin.reset_cache() property = plugin.__getattribute__(attr) assert 1 == len(plugin.bot.entities['property']) plugin.clear_entity_label(property) assert 0 == len(plugin.bot.entities['property']) for i in range(120): if (plugin.lookup_entity(attr, type='property') is None): break property = plugin.__getattribute__(attr) assert 1 == len(plugin.bot.entities['property']) new_content = plugin.bot.site.loadcontent({'ids': property}, 'datatype') wikidata_property = wikidata_plugin.__getattribute__(attr) wikidata_content = wikidata_plugin.bot.site.loadcontent( {'ids': wikidata_property}, 'datatype') assert (wikidata_content[wikidata_property]['datatype'] == new_content[property]['datatype']), attr assert (datatype == wikidata_content[wikidata_property]['datatype'] ), attr
def test_run_query_default(self, m_query, m_run): b = Bot.factory([ '--verbose', '--plugin=QA', ]) m_query.side_effect = 'one page' b.run() m_run.assert_called_with(mock.ANY)
def test_run_items(self, m_run): b = Bot.factory([ '--verbose', '--item=Q1', '--plugin=QA', ]) b.run() m_run.assert_called_with(mock.ANY)
def test_template_parse_license(self): bot = Bot.factory(['--verbose'] + self.args) license = License(bot, bot.args) found = license.template_parse_license( '[[GNU GPL#v2]] [[MIT/X11 license|]]', 'en') assert 2 == len(found) for item in found: item.get() license.debug(item, "FOUND") assert item.labels['en'] in (self.gpl, self.mit)
def test_get_item(self): bot = Bot.factory(['--verbose'] + self.args) license = License(bot, bot.args) redirect = 'GPL' license.get_names('en') canonical_item = license.get_item(self.gpl, 'en') assert canonical_item == license.get_item(redirect, 'en') gpl_fr = 'Licence publique générale GNU' names_fr = license.get_names('fr') assert gpl_fr in names_fr assert canonical_item == license.get_item(gpl_fr, 'fr')
def test_fixup(self, m_get_sitelink_item, m_set_license2item): bot = Bot.factory([ '--verbose', '--test', '--user=FLOSSbotCI', ]) l = License(bot, bot.args) gpl = l.Q_GNU_General_Public_License gpl.get() found = False if gpl.claims: for claim in gpl.claims.get(l.P_subclass_of, []): if claim.type != 'wikibase-item': continue if (claim.getTarget().getID() == l.Q_free_software_license.getID()): found = True break if not found: subclass_of = pywikibot.Claim(l.bot.site, l.P_subclass_of, 0) subclass_of.setTarget(l.Q_free_software_license) gpl.addClaim(subclass_of) gpl.setSitelink({'site': 'enwiki', 'title': self.gpl}) gpl.get(force=True) emacs = l.Q_GNU_Emacs emacs.get() if emacs.claims: licenses = emacs.claims.get(l.P_license, []) if licenses: emacs.removeClaims(licenses) emacs.get(force=True) def set_license2item(): l.license2item = {self.gpl: l.Q_GNU_General_Public_License} m_set_license2item.side_effect = set_license2item def get_sitelink_item(dbname): if dbname == 'enwiki': return l.Q_English_Wikipedia elif dbname == 'frwiki': return l.Q_French_Wikipedia else: assert 0, "unexpected " + dbname m_get_sitelink_item.side_effect = get_sitelink_item emacs.removeSitelinks(['enwiki']) emacs.removeSitelinks(['frwiki']) emacs.get(force=True) assert ['nothing'] == l.fixup(emacs) emacs.setSitelink({'site': 'enwiki', 'title': 'GNU Emacs'}) emacs.setSitelink({'site': 'frwiki', 'title': 'GNU Emacs'}) emacs.get(force=True) assert [self.gpl] == l.fixup(emacs)
def test_lookup_item(self): bot = Bot.factory([ '--test', '--user=FLOSSbotCI', ]) plugin = Plugin(bot, bot.args) assert 0 == len(plugin.bot.entities['item']) git = plugin.Q_git assert 1 == len(plugin.bot.entities['item']) assert git == plugin.Q_git assert plugin.Q_Concurrent_Versions_System assert 2 == len(plugin.bot.entities['item'])
def test_run_query_items(self, m_query, m_run, caplog): b = Bot.factory([ '--verbose', '--filter=qa-verify', '--plugin=QA', ]) m_query.side_effect = 'one page' b.run() for record in caplog.records(): if 'running query' in record.message: assert '?qa' in record.message
def test_get_names(self): bot = Bot.factory(['--verbose'] + self.args) license = License(bot, bot.args) redirect = 'GPL' names = license.get_names('en') assert self.gpl in names assert redirect in names canonical_fr = 'Licence publique générale GNU' names = license.get_names('fr') assert canonical_fr in names assert self.gpl in names
def test_get_template_field(self): bot = Bot.factory(['--verbose']) plugin = Plugin(bot, bot.args) item = plugin.Q_GNU_Emacs expected = { 'fr': 'licence', 'en': 'license', } item.get() lang2field = {'en': 'License'} lang2pattern = {'*': 'Infobox'} actual = plugin.get_template_field(item, lang2field, lang2pattern) assert actual.keys() == expected.keys()
def test_factory(self): Bot.factory(['--verbose']) assert ( logging.getLogger('FLOSSbot').getEffectiveLevel() == logging.DEBUG) b = Bot.factory([]) assert ( logging.getLogger('FLOSSbot').getEffectiveLevel() == logging.INFO) assert len(b.plugins) > 0 plugin = 'QA' b = Bot.factory(['--verbose', '--plugin=' + plugin]) assert 1 == len(b.plugins) assert plugin == b.plugins[0].__class__.__name__ b = Bot.factory([ '--verbose', '--plugin=QA', '--plugin=Repository', ]) assert 2 == len(b.plugins)
def test_verify_no_value(self): bot = Bot.factory([ '--verbose', '--test', '--user=FLOSSbotCI', ]) qa = QA(bot, bot.args) item = qa.__getattribute__('Q_' + WikidataHelper.random_name()) claim = pywikibot.Claim(qa.bot.site, qa.P_software_quality_assurance, 'novalue') claim.setTarget(qa.Q_Continuous_integration) item.addClaim(claim) claim.changeTarget(None, 'novalue') item.get(force=True) assert ['no ci'] == qa.verify(item) qa.clear_entity_label(item.getID())
def test_set_retrieved(self): bot = Bot.factory([ '--test', '--user=FLOSSbotCI', ]) plugin = Plugin(bot, bot.args) item = plugin.__getattribute__('Q_' + WikidataHelper.random_name()) claim = pywikibot.Claim(plugin.bot.site, plugin.P_source_code_repository, 0) claim.setTarget("http://repo.com/some") item.addClaim(claim) plugin.set_retrieved(item, claim) assert plugin.need_verification(claim) is False plugin.set_retrieved(item, claim, date(1965, 11, 2)) assert plugin.need_verification(claim) is True plugin.clear_entity_label(item.getID())
def test_search_entity(self): bot = Bot.factory([ '--test', '--user=FLOSSbotCI', '--verbose', ]) plugin = Plugin(bot, bot.args) # ensure space, - and _ are accepted name = WikidataHelper.random_name() + "-some thing_else" entity = { "labels": { "en": { "language": "en", "value": name, } }, } first = plugin.bot.site.editEntity({'new': 'item'}, entity) first = pywikibot.ItemPage(bot.site, first['entity']['id'], 0) second = plugin.bot.site.editEntity({'new': 'item'}, entity) second = pywikibot.ItemPage(bot.site, second['entity']['id'], 0) with pytest.raises(ValueError) as e: plugin.search_entity(plugin.bot.site, name, type='item') assert "found multiple items" in str(e.value) claim = pywikibot.Claim(plugin.bot.site, plugin.P_instance_of, 0) claim.setTarget(plugin.Q_Wikimedia_disambiguation_page) first.addClaim(claim) found = plugin.search_entity(bot.site, name, type='item') assert found.getID() == second.getID() plugin.bot.site.editEntity({'new': 'item'}, entity) with pytest.raises(ValueError) as e: plugin.search_entity(plugin.bot.site, name, type='item') assert "found multiple items" in str(e.value) Plugin.authoritative['test'][name] = second.getID() found = plugin.search_entity(plugin.bot.site, name, type='item') assert found.getID() == second.getID()
def test_verify(self, m_get): url2code = {} def get(url, **kwargs): log.debug(url + " " + str(kwargs)) class c: def __init__(self, code): self.status_code = code return c(url2code.get(url, requests.codes.ok)) m_get.side_effect = get bot = Bot.factory([ '--verbose', '--test', '--user=FLOSSbotCI', '--verification-delay=0', ]) qa = QA(bot, bot.args) item = qa.__getattribute__('Q_' + WikidataHelper.random_name()) log.debug(">> do nothing if there is no source code repository") item.get(force=True) assert ['nothing'] == qa.verify(item) log.debug(">> add a source code repository") repository = pywikibot.Claim(qa.bot.site, qa.P_source_code_repository, 0) url = "http://github.com/FAKE1/FAKE2" repository.setTarget(url) item.addClaim(repository) log.debug(">> add a qa statement") item.get(force=True) qa.fixup(item) log.debug(">> no ci found") item.get(force=True) url2code['https://travis-ci.org/FAKE1/FAKE2'] = 404 assert ['no ci found'] == qa.verify(item) log.debug(">> verified") del url2code['https://travis-ci.org/FAKE1/FAKE2'] assert ['verified'] == qa.verify(item) log.debug(">> no need") qa.args.verification_delay = 30 assert ['no need'] == qa.verify(item) qa.args.verification_delay = 0 log.debug(">> inconsistent qualifier") repository.changeTarget("http://github.com/other/other") item.get(force=True) assert (['archive URL gone', 'described at URL gone'] == qa.verify(item)) log.debug(">> missing qualifier") qa_claim = item.claims[qa.P_software_quality_assurance][0] archive_URL = qa_claim.qualifiers[qa.P_archive_URL][0] qa_claim.removeQualifier(archive_URL) item.get(force=True) assert ['archive URL missing qualifier', 'described at URL gone'] == qa.verify(item) qa.clear_entity_label(item.getID())
def test_get_redirects(self): bot = Bot.factory(['--verbose']) plugin = Plugin(bot, bot.args) titles = plugin.get_redirects('GNU General Public License', 'en') assert 'GPL' in titles