jenkins-bot has submitted this change and it was merged.
Change subject: [FEAT] patrol: Basic test of PatrolBot class ......................................................................
[FEAT] patrol: Basic test of PatrolBot class
This makes the `PatrolBot` class a `SingleSiteBot` class and allows to set a site via the constructor. It also adds basic tests for `PatrolBot.parse_page_tuples` and `PatrolBot.in_list`.
Change-Id: I2cfb34a10e40e84634cd1ee15c5879741abe26d9 --- M scripts/patrol.py A tests/patrolbot_tests.py 2 files changed, 86 insertions(+), 6 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/patrol.py b/scripts/patrol.py index 5cc61e0..e37907d 100755 --- a/scripts/patrol.py +++ b/scripts/patrol.py @@ -25,11 +25,14 @@ from __future__ import absolute_import, unicode_literals
__version__ = '$Id$' -import pywikibot -from pywikibot import pagegenerators, Bot import mwlib.uparser # used to parse the whitelist import mwlib.parser # used to parse the whitelist import time + +import pywikibot + +from pywikibot import pagegenerators +from pywikibot.bot import SingleSiteBot
_logger = 'patrol'
@@ -40,7 +43,7 @@ }
-class PatrolBot(Bot): +class PatrolBot(SingleSiteBot):
"""Bot marks the edits as patrolled based on info obtained by whitelist."""
@@ -49,7 +52,7 @@ 'en': u'patrol_whitelist', }
- def __init__(self, **kwargs): + def __init__(self, site=True, **kwargs): """ Constructor.
@@ -67,10 +70,9 @@ 'versionchecktime': 300, 'autopatroluserns': False }) - super(PatrolBot, self).__init__(**kwargs) + super(PatrolBot, self).__init__(site, **kwargs) self.recent_gen = True self.user = None - self.site = pywikibot.Site() if self.getOption('whitelist'): self.whitelist_pagename = self.getOption('whitelist') else: diff --git a/tests/patrolbot_tests.py b/tests/patrolbot_tests.py new file mode 100644 index 0000000..961d637 --- /dev/null +++ b/tests/patrolbot_tests.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +"""Tests for the patrol script.""" +# +# (C) Pywikibot team, 2015 +# +# Distributed under the terms of the MIT license. +# +from __future__ import absolute_import, unicode_literals + +__version__ = '$Id$' +# +try: + from scripts import patrol +except ImportError: + patrol = None # if mwlib is not installed + +from tests.aspects import require_modules, unittest, DefaultDrySiteTestCase + +DUMMY_PAGE_TUPLES = """ +This is some text above the entries: + +== Header == +* [[User:Test 1]]: [[Page 1]], [[Page 2]] +* [[User:Test 2]]: [[Page 2]], [[Page 4]], [[Page 6]] + +== Others == +* [[User:Prefixed]]: [[Special:PrefixIndex/Page 1]], [[Special:PREFIXINDEX/Page 2]] + +== More test 1 == +* [[User:Test 1]]: [[Page 3]] +""" + + +@require_modules('mwlib') +class TestPatrolBot(DefaultDrySiteTestCase): + + """Test the PatrolBot class.""" + + def setUp(self): + """Create a bot dummy instance.""" + super(TestPatrolBot, self).setUp() + self.bot = patrol.PatrolBot(self.site) + + def test_parse_page_tuples(self): + """Test parsing the page tuples from a dummy text.""" + tuples = self.bot.parse_page_tuples(DUMMY_PAGE_TUPLES) + for gen_user in (1, 2): + user = 'Test {0}'.format(gen_user) + self.assertIn(user, tuples) + self.assertEqual(tuples[user], ['Page {0}'.format(i * gen_user) + for i in range(1, 4)]) + self.assertIn('Prefixed', tuples) + self.assertEqual(tuples['Prefixed'], ['Page 1', 'Page 2']) + self.assertRaises(Exception, self.bot.parse_page_tuples, '[[link]]') + + def test_in_list(self): + """Test the method which returns whether a page is in the list.""" + # Return the title if there is an exact match + self.assertEqual(self.bot.in_list(['Foo', 'Foobar'], 'Foo'), 'Foo') + self.assertEqual(self.bot.in_list(['Foo', 'Foobar'], 'Foobar'), 'Foobar') + + # Return the first entry which starts with the title if there is no + # exact match + self.assertEqual(self.bot.in_list(['Foo', 'Foobar'], 'Foob'), 'Foo') + self.assertEqual(self.bot.in_list(['Foo', 'Foobar'], 'Foobarz'), 'Foo') + self.assertEqual(self.bot.in_list(['Foo', 'Foobar', 'Bar'], 'Barz'), 'Bar') + + # '' returns .* if there is no exact match + self.assertEqual(self.bot.in_list([''], 'Foo'), '.*') + self.assertEqual(self.bot.in_list(['', 'Foobar'], 'Foo'), '.*') + self.assertEqual(self.bot.in_list(['', 'Foo'], 'Foo'), 'Foo') + + +if __name__ == '__main__': + try: + unittest.main() + except SystemExit: + pass