jenkins-bot has submitted this change and it was merged.
Change subject: [bugfix] Add test for lagpattern ......................................................................
[bugfix] Add test for lagpattern
- When the regex acts as expected site.throttle.lag is called. The DummyThrottle saves the lag value and raises an exception to escape from the while loop inside api.Request.submit(). Do some minor tests with that value. Otherwise when regex cannot extract the value api raises an APIError in his normal way. Print an additional warning that the lagpattern fails in that case.
Bug: T144023 Change-Id: I96adc5b5d8bbf65a7c99d380a0f1df6108a5dcfd --- M tests/api_tests.py 1 file changed, 41 insertions(+), 1 deletion(-)
Approvals: Merlijn van Deen: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/api_tests.py b/tests/api_tests.py index c236f68..5507956 100644 --- a/tests/api_tests.py +++ b/tests/api_tests.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """API test module.""" # -# (C) Pywikibot team, 2007-2015 +# (C) Pywikibot team, 2007-2016 # # Distributed under the terms of the MIT license. # @@ -18,6 +18,7 @@ import pywikibot.page import pywikibot.site
+from pywikibot.throttle import Throttle from pywikibot.tools import ( MediaWikiVersion, PY2, @@ -1026,6 +1027,45 @@ self.assertEqual(result, expect) self.assertIsInstance(result, str)
+ +class DummyThrottle(Throttle): + + """Dummy Throttle class.""" + + def lag(self, lag): + """Override lag method, save the lag value and exit the api loop.""" + self._lagvalue = lag # save the lag value + raise SystemExit # exit the api loop + + +class TestLagpattern(DefaultSiteTestCase): + + """Test the lag pattern.""" + + cached = False + + def test_valid_lagpattern(self): + """Test whether api.lagpattern is valid.""" + mysite = self.get_site() + 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 + value = mysite.throttle._lagvalue + self.assertIsInstance(value, int) + self.assertGreaterEqual(value, 0) + + if __name__ == '__main__': # pragma: no cover try: unittest.main()
pywikibot-commits@lists.wikimedia.org