def setUp(self): """Mock warning and error.""" super(TestAPIMWException, self).setUp() self.warning_patcher = patch.object(pywikibot, 'warning') self.error_patcher = patch.object(pywikibot, 'error') self.warning_patcher.start() self.error_patcher.start()
def test_with_delete_and_non_existing_sdtemplate(self): """Test with delete and non-exisitng sdtemplate.""" options = {'delete': True, 'sdtemplate': 'txt {{n|a}} txt'} with patch.object(Page, 'exists', new=Mock(return_value=False)): with patch.object(pywikibot, 'warning') as w: bot = RedirectRobot('broken', **options) w.assert_called_with('No speedy deletion template "n" available.') self.assertEqual(bot.sdtemplate, None)
def test_with_delete_and_non_existing_sdtemplate(self): """Test with delete and non-exisitng sdtemplate.""" options = {'delete': True, 'sdtemplate': 'txt {{n|a}} txt'} bot = RedirectTestRobot('broken', **options) with patch.object(Page, 'exists', new=Mock(return_value=False)): with patch.object(pywikibot, 'warning') as w: self.assertIsNone(bot.sdtemplate, None) w.assert_called_with('No speedy deletion template "n" available.')
def test_copy_and_keep(self): """Test the copyAndKeep method.""" site = self.get_site() foo_cat = pywikibot.Category(site, 'Category:Foo') with patch.object(pywikibot.Category, 'exists', return_value=True), \ patch.object(pywikibot, 'warning') as w: self.assertFalse(foo_cat.copyAndKeep('cattitle', [], 'message')) w.assert_called_once_with( 'Target page Category:Cattitle already exists!')
def test_siteinfo_timestamps(self): """Test that cache has the timestamp of CachedRequest.""" site = self.get_site() request_mock = MagicMock() request_mock.submit = lambda: {'query': {'_prop': '_value'}} request_mock._cachetime = '_cache_time' with patch.object(site, '_request', return_value=request_mock): siteinfo = pywikibot.site.Siteinfo(site) result = siteinfo._get_siteinfo('_prop', False) self.assertEqual(result, {'_prop': ('_value', '_cache_time')})
def test_with_invalid_module(self): """Test requesting different kind of invalid modules.""" site = self.get_site() pi = api.ParamInfo(site) self.assertIsEmpty(pi) with patch.object(pywikibot, 'warning') as w: pi.fetch('foobar') self.assertRaises(KeyError, pi.__getitem__, 'foobar') self.assertRaises(KeyError, pi.__getitem__, 'foobar+foobar') # The warning message may be different with older MW versions. self.assertIn('API warning (paraminfo): ', w.call_args[0][0]) self.assertNotIn('foobar', pi._paraminfo) self.assertIn('main', pi._paraminfo) self.assertIn('paraminfo', pi._paraminfo) self.assertLength(pi, pi.preloaded_modules)
def test_autoblocked_user_with_namespace(self): """Test autoblocked user.""" # Suppress output: This is an autoblock ID, you can only use to unblock with patch.object(pywikibot, 'output'): user = User(self.site, 'User:#1242976') self.assertEqual('#1242976', user.username) with suppress_warnings('pywikibot.page.User.name is deprecated'): self.assertEqual(user.name(), user.username) self.assertEqual(user.title(with_ns=False), user.username[1:]) self.assertFalse(user.isRegistered()) self.assertFalse(user.isAnonymous()) self.assertIsNone(user.registration()) self.assertFalse(user.isEmailable()) self.assertIn('invalid', user.getprops()) self.assertTrue(user._isAutoblock) self.assertRaisesRegex(AutoblockUser, 'This is an autoblock ID', user.getUserPage) self.assertRaisesRegex(AutoblockUser, 'This is an autoblock ID', user.getUserTalkPage)
def test_autoblocked_user(self): """Test autoblocked user.""" with patch.object(pywikibot, 'output') as p: user = User(self.site, '#1242976') p.assert_called_once_with( 'This is an autoblock ID, you can only use to unblock it.') self.assertEqual('#1242976', user.username) self.assertEqual(user.title(with_ns=False), user.username[1:]) self.assertFalse(user.isRegistered()) self.assertFalse(user.isAnonymous()) self.assertIsNone(user.registration()) self.assertFalse(user.isEmailable()) self.assertIn('invalid', user.getprops()) self.assertTrue(user._isAutoblock) with self.assertRaisesRegex(AutoblockUserError, 'This is an autoblock ID'): user.getUserPage() with self.assertRaisesRegex(AutoblockUserError, 'This is an autoblock ID'): user.getUserTalkPage()
def test_submodules(self): """Test another module apart from query having submodules.""" pi = api.ParamInfo(self.site) self.assertFalse(pi._modules) pi.fetch(['query']) self.assertIn('query', pi._modules) self.assertIsInstance(pi._modules['query'], frozenset) self.assertIn('revisions', pi._modules['query']) self.assertEqual(pi.submodules('query'), pi.query_modules) for mod in pi.submodules('query', True): self.assertEqual(mod[:6], 'query+') self.assertEqual(mod[6:], pi[mod]['name']) self.assertEqual(mod, pi[mod]['path']) with patch.object(pywikibot, 'warning') as w: self.assertRaises(KeyError, pi.__getitem__, 'query+foobar') # The warning message may be different with older MW versions. self.assertIn('API warning (paraminfo): ', w.call_args[0][0]) self.assertRaises(KeyError, pi.submodules, 'edit')
def test_with_invalid_module(self): """Test requesting different kind of invalid modules.""" site = self.get_site() pi = api.ParamInfo(site) self.assertEqual(len(pi), 0) with patch.object(pywikibot, 'warning') as w: pi.fetch('foobar') self.assertRaises(KeyError, pi.__getitem__, 'foobar') self.assertRaises(KeyError, pi.__getitem__, 'foobar+foobar') # The warning message does not end with a '.' in older MW versions. self.assertIn( 'API warning (paraminfo): ' 'The module "main" does not have a submodule "foobar"', w.call_args[0][0]) self.assertNotIn('foobar', pi._paraminfo) self.assertIn('main', pi._paraminfo) self.assertIn('paraminfo', pi._paraminfo) if MediaWikiVersion(self.site.version()) < MediaWikiVersion("1.12"): return self.assertEqual(len(pi), len(pi.preloaded_modules))
def test_with_delete_no_sd_no_i18n(self): """Test with delete and no i18n template.""" bot = RedirectTestRobot('broken', delete=True) with patch.object(pywikibot, 'warning') as w: self.assertIsNone(bot.sdtemplate) w.assert_called_with('No speedy deletion template available.')
def test_nobots(self): """Test that {{nobots}} raise the appropriate exception.""" page = pywikibot.Page(self.site, 'User:John Vandenberg/nobots') with patch.object(config, 'ignore_bot_templates', False): with self.assertRaisesRegex(OtherPageSaveError, 'nobots'): page.save()
def setUp(self): self.setup_patcher = patch.object(SwordBible, '_setup') self.addCleanup(self.setup_patcher.stop) self.setup_patcher.start()
def test_with_delete_no_sd_no_i18n(self): """Test with delete and no i18n template.""" with patch.object(pywikibot, 'warning') as w: bot = RedirectRobot('broken', delete=True) w.assert_called_with('No speedy deletion template available.') self.assertEqual(bot.sdtemplate, None)