jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/509808 )
Change subject: [IMPR] Use assertLength/assertIsEmpty/assertIsNotEmpty ......................................................................
[IMPR] Use assertLength/assertIsEmpty/assertIsNotEmpty
Additional changes in assertIsEmpty/assertIsNotEmpty/assertLength: - check whether seq argument of these methods is of type collections.abc.Sized i.e has a __len__ attribute - use fail method instead of raising failureException
Change-Id: Ic136f9a826b6f57df00b038524412887370c01a8 --- M tests/api_tests.py M tests/aspects.py M tests/compat2core_tests.py M tests/site_tests.py 4 files changed, 49 insertions(+), 43 deletions(-)
Approvals: Lokal Profil: Looks good to me, approved jenkins-bot: Verified
diff --git a/tests/api_tests.py b/tests/api_tests.py index c7b6cb4..05a6af7 100644 --- a/tests/api_tests.py +++ b/tests/api_tests.py @@ -162,10 +162,10 @@ self.assertEqual(req._params['one'], ['1']) # test compliance with dict interface # req.keys() should contain 'action', 'foo', 'bar', 'one' - self.assertEqual(len(req.keys()), 4) + self.assertLength(req.keys(), 4) self.assertIn('test', req._encoded_items().values()) for item in req.items(): - self.assertEqual(len(item), 2, item) + self.assertLength(item, 2)
@suppress_warnings( 'Instead of using kwargs |Both kwargs and parameters are set', @@ -189,12 +189,12 @@ """Test common initialization.""" site = self.get_site() pi = api.ParamInfo(site) - self.assertEqual(len(pi), 0) + self.assertIsEmpty(pi) pi._init()
self.assertIn('main', pi._paraminfo) self.assertIn('paraminfo', pi._paraminfo) - self.assertEqual(len(pi), len(pi.preloaded_modules)) + self.assertLength(pi, pi.preloaded_modules)
self.assertIn('info', pi.query_modules) self.assertIn('login', pi._action_modules) @@ -212,7 +212,7 @@ assert 'query' not in modules original_generate_submodules(modules) pi = api.ParamInfo(self.site, {'query', 'main'}) - self.assertEqual(len(pi), 0) + self.assertIsEmpty(pi) original_generate_submodules = pi._generate_submodules pi._generate_submodules = patched_generate_submodules pi._init() @@ -225,7 +225,7 @@ self.assertNotIn('query', api.ParamInfo.init_modules) pi = api.ParamInfo(site, {'pageset'}) self.assertNotIn('query', api.ParamInfo.init_modules) - self.assertEqual(len(pi), 0) + self.assertIsEmpty(pi) pi._init()
self.assertIn('main', pi._paraminfo) @@ -234,13 +234,12 @@
if 'query' in pi.preloaded_modules: self.assertIn('query', pi._paraminfo) - self.assertEqual(len(pi), 4) + self.assertLength(pi, 4) else: self.assertNotIn('query', pi._paraminfo) - self.assertEqual(len(pi), 3) + self.assertLength(pi, 3)
- self.assertEqual(len(pi), - len(pi.preloaded_modules)) + self.assertLength(pi, pi.preloaded_modules)
if site.mw_version >= '1.21': # 'generator' was added to 'pageset' in 1.21 @@ -251,7 +250,7 @@ """Test requesting the generator parameter.""" site = self.get_site() pi = api.ParamInfo(site, {'pageset', 'query'}) - self.assertEqual(len(pi), 0) + self.assertIsEmpty(pi) pi._init()
self.assertIn('main', pi._paraminfo) @@ -270,13 +269,13 @@ """Test requesting the module info.""" site = self.get_site() pi = api.ParamInfo(site) - self.assertEqual(len(pi), 0) + self.assertIsEmpty(pi) pi.fetch(['info']) self.assertIn('query+info', pi._paraminfo)
self.assertIn('main', pi._paraminfo) self.assertIn('paraminfo', pi._paraminfo) - self.assertEqual(len(pi), 1 + len(pi.preloaded_modules)) + self.assertLength(pi, 1 + len(pi.preloaded_modules))
self.assertEqual(pi['info']['prefix'], 'in')
@@ -294,13 +293,13 @@ """Test requesting the module revisions.""" site = self.get_site() pi = api.ParamInfo(site) - self.assertEqual(len(pi), 0) + self.assertIsEmpty(pi) pi.fetch(['revisions']) self.assertIn('query+revisions', pi._paraminfo)
self.assertIn('main', pi._paraminfo) self.assertIn('paraminfo', pi._paraminfo) - self.assertEqual(len(pi), 1 + len(pi.preloaded_modules)) + self.assertLength(pi, 1 + len(pi.preloaded_modules))
self.assertEqual(pi['revisions']['prefix'], 'rv')
@@ -318,7 +317,7 @@ """Test requesting multiple modules in one fetch.""" site = self.get_site() pi = api.ParamInfo(site) - self.assertEqual(len(pi), 0) + self.assertIsEmpty(pi) pi.fetch(['info', 'revisions']) self.assertIn('query+info', pi._paraminfo) self.assertIn('query+revisions', pi._paraminfo) @@ -326,13 +325,13 @@ self.assertIn('main', pi._paraminfo) self.assertIn('paraminfo', pi._paraminfo)
- self.assertEqual(len(pi), 2 + len(pi.preloaded_modules)) + self.assertLength(pi, 2 + len(pi.preloaded_modules))
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) + self.assertIsEmpty(pi)
with patch.object(pywikibot, 'warning') as w: pi.fetch('foobar') @@ -345,7 +344,7 @@ self.assertIn('main', pi._paraminfo) self.assertIn('paraminfo', pi._paraminfo)
- self.assertEqual(len(pi), len(pi.preloaded_modules)) + self.assertLength(pi, pi.preloaded_modules)
def test_submodules(self): """Test another module apart from query having submodules.""" @@ -444,7 +443,7 @@ self.assertIn('main', pi._paraminfo) self.assertIn('paraminfo', pi._paraminfo)
- self.assertEqual(len(pi), 1 + len(pi.preloaded_modules)) + self.assertLength(pi, 1 + len(pi.preloaded_modules))
self.assertIn('query+revisions', pi.prefix_map)
@@ -458,13 +457,10 @@ pi = api.ParamInfo(site, modules_only_mode=True) pi.fetch(['info']) self.assertIn('query+info', pi._paraminfo) - self.assertIn('main', pi._paraminfo) self.assertIn('paraminfo', pi._paraminfo)
- self.assertEqual(len(pi), - 1 + len(pi.preloaded_modules)) - + self.assertLength(pi, 1 + len(pi.preloaded_modules)) self.assertIn('query+revisions', pi.prefix_map)
@@ -528,19 +524,19 @@ options['anon'] = True self.assertCountEqual(['anon'], options._enabled) self.assertEqual(set(), options._disabled) - self.assertEqual(1, len(options)) + self.assertLength(options, 1) self.assertEqual(['anon'], list(options)) self.assertEqual(['anon'], list(options.api_iter())) options['bot'] = False self.assertCountEqual(['anon'], options._enabled) self.assertCountEqual(['bot'], options._disabled) - self.assertEqual(2, len(options)) + self.assertLength(options, 2) self.assertEqual(['anon', 'bot'], list(options)) self.assertEqual(['anon', '!bot'], list(options.api_iter())) options.clear() self.assertEqual(set(), options._enabled) self.assertEqual(set(), options._disabled) - self.assertEqual(0, len(options)) + self.assertIsEmpty(options) self.assertEqual([], list(options)) self.assertEqual([], list(options.api_iter()))
@@ -550,12 +546,12 @@ options['invalid_name'] = True options['anon'] = True self.assertIn('invalid_name', options._enabled) - self.assertEqual(2, len(options)) + self.assertLength(options, 2) self.assertRaises(KeyError, options._set_site, self.get_site(), 'recentchanges', 'show') - self.assertEqual(2, len(options)) + self.assertLength(options, 2) options._set_site(self.get_site(), 'recentchanges', 'show', True) - self.assertEqual(1, len(options)) + self.assertLength(options, 1) self.assertRaises(TypeError, options._set_site, self.get_site(), 'recentchanges', 'show')
@@ -707,7 +703,7 @@ self.assertIn('pageid', pagedata) self.assertIn('lastrevid', pagedata) count += 1 - self.assertEqual(len(links), count) + self.assertLength(links, count)
def test_one_continuation(self): """Test PropertyGenerator with prop 'revisions'.""" @@ -727,7 +723,7 @@ self.assertIn('revisions', pagedata) self.assertIn('revid', pagedata['revisions'][0]) count += 1 - self.assertEqual(len(links), count) + self.assertLength(links, count)
def test_two_continuations(self): """Test PropertyGenerator with prop 'revisions' and 'coordinates'.""" @@ -747,7 +743,7 @@ self.assertIn('revisions', pagedata) self.assertIn('revid', pagedata['revisions'][0]) count += 1 - self.assertEqual(len(links), count) + self.assertLength(links, count)
@allowed_failure def test_many_continuations_limited(self): @@ -776,7 +772,7 @@ self.assertIsInstance(pagedata, dict) self.assertIn('pageid', pagedata) count += 1 - self.assertEqual(len(links), count) + self.assertLength(links, count) # FIXME: AssertionError: 30 != 6150
@allowed_failure @@ -798,7 +794,7 @@ self.assertIsInstance(pagedata, dict) self.assertIn('pageid', pagedata) count += 1 - self.assertEqual(len(links), count) + self.assertLength(links, count) # FIXME: AssertionError: 30 != 11550
# FIXME: test disabled as it takes longer than 10 minutes @@ -819,7 +815,7 @@ self.assertIsInstance(pagedata, dict) self.assertIn('pageid', pagedata) count += 1 - self.assertEqual(len(links), count) + self.assertLength(links, count)
class TestDryQueryGeneratorNamespaceParam(TestCase): diff --git a/tests/aspects.py b/tests/aspects.py index 71f14e6..ab2e966 100644 --- a/tests/aspects.py +++ b/tests/aspects.py @@ -22,6 +22,10 @@ import warnings
from contextlib import contextmanager +try: + from collections.abc import Sized +except ImportError: # Python 2.7 + from collections import Sized
import pywikibot
@@ -102,19 +106,25 @@
def assertIsEmpty(self, seq, msg=None): """Check that the sequence is empty.""" + self.assertIsInstance( + seq, Sized, 'seq argument is not a Sized class containing __len__') if seq: msg = self._formatMessage(msg, '%s is not empty' % safe_repr(seq)) - raise self.failureException(msg) + self.fail(msg)
def assertIsNotEmpty(self, seq, msg=None): """Check that the sequence is not empty.""" + self.assertIsInstance( + seq, Sized, 'seq argument is not a Sized class containing __len__') if not seq: msg = self._formatMessage(msg, '%s is empty' % safe_repr(seq)) - raise self.failureException(msg) + self.fail(msg)
def assertLength(self, seq, other, msg=None): """Verify that a sequence expr has the length of other.""" # the other parameter may be given as a sequence too + self.assertIsInstance( + seq, Sized, 'seq argument is not a Sized class containing __len__') first_len = len(seq) try: second_len = len(other) @@ -124,7 +134,7 @@ if first_len != second_len: msg = self._formatMessage( msg, 'len(%s) != %s' % (safe_repr(seq), second_len)) - raise self.failureException(msg) + self.fail(msg)
def _addUnexpectedSuccess(self, result): """Report and ignore.""" @@ -1651,7 +1661,7 @@ # deprecation message from the set. self.assertCountEqual(set(self.deprecation_messages), [self.deprecation_messages[0]]) - self.assertEqual(len(self.deprecation_messages), count) + self.assertLength(self.deprecation_messages, count) self._reset_messages()
def assertNoDeprecation(self, msg=None): diff --git a/tests/compat2core_tests.py b/tests/compat2core_tests.py index a6986f4..122c8c0 100644 --- a/tests/compat2core_tests.py +++ b/tests/compat2core_tests.py @@ -23,14 +23,14 @@ def test_replacements(self): """Test compat2core replacements.""" for item in c2c.replacements: - self.assertEqual(len(item), 2) + self.assertLength(item, 2) self.assertIsInstance(item[0], StringTypes) self.assertIsInstance(item[1], StringTypes)
def test_warnings(self): """Test compat2core warnings.""" for item in c2c.warnings: - self.assertEqual(len(item), 2) + self.assertLength(item, 2) self.assertIsInstance(item[0], StringTypes) self.assertIsInstance(item[1], StringTypes)
diff --git a/tests/site_tests.py b/tests/site_tests.py index 0e012c4..b555258 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -2639,7 +2639,7 @@ """Test the site.loadrevisions() method with rollback.""" mainpage = self.get_mainpage() self.site.loadrevisions(mainpage, total=12, rollback=True, sysop=True) - self.assertGreater(len(mainpage._revisions), 0) + self.assertIsNotEmpty(mainpage._revisions) self.assertLessEqual(len(mainpage._revisions), 12) self.assertTrue(all(rev.rollbacktoken is not None for rev in mainpage._revisions.values()))