Merge "Replace assertEqual(None, *) with assertIsNone in tests"

This commit is contained in:
Jenkins 2016-11-22 02:15:19 +00:00 committed by Gerrit Code Review
commit 0c563b804d
5 changed files with 27 additions and 3 deletions

View File

@ -31,6 +31,7 @@ Neutron Specific Commandments
- [N343] Production code must not import from neutron.tests.*
- [N344] Python 3: Do not use filter(lambda obj: test(obj), data). Replace it
with [obj for obj in data if test(obj)].
- [N345] Detect wrong usage with assertEqual(None, A) and assertEqual(A, None).
Creating Unit Tests
-------------------

View File

@ -389,6 +389,19 @@ def check_python3_no_filter(logical_line):
yield(0, msg)
@flake8ext
def check_assertIsNone(logical_line, filename):
"""N345 - Enforce using assertIsNone."""
if 'neutron/tests/' in filename:
asse_eq_end_with_none_re = re.compile(r"assertEqual\(.*?,\s+None\)$")
asse_eq_start_with_none_re = re.compile(r"assertEqual\(None,")
res = (asse_eq_start_with_none_re.search(logical_line) or
asse_eq_end_with_none_re.search(logical_line))
if res:
yield (0, "N345: assertEqual(A, None) or assertEqual(None, A) "
"sentences not allowed")
def factory(register):
register(validate_log_translations)
register(use_jsonutils)
@ -409,3 +422,4 @@ def factory(register):
register(check_unittest_imports)
register(check_no_imports_from_tests)
register(check_python3_no_filter)
register(check_assertIsNone)

View File

@ -100,4 +100,4 @@ class FloatingIPTestJSON(base.BaseNetworkTest):
# disassociate
body = self.client.update_floatingip(body['floatingip']['id'],
port_id=None)
self.assertEqual(None, body['floatingip']['port_id'])
self.assertIsNone(body['floatingip']['port_id'])

View File

@ -766,8 +766,7 @@ class L3HATestCase(L3HATestFramework):
def test_get_active_host_for_ha_router(self):
router = self._create_router()
self.assertEqual(
None,
self.assertIsNone(
self.plugin.get_active_host_for_ha_router(
self.admin_ctx, router['id']))
self.plugin.update_routers_states(

View File

@ -327,6 +327,16 @@ class HackingTestCase(base.BaseTestCase):
self.assertLinePasses(f, "filter(function, range(0,10))")
self.assertLinePasses(f, "lambda x, y: x+y")
def test_check_assertIsNone(self):
self.assertEqual(1, len(list(checks.check_assertIsNone(
"self.assertEqual(A, None)", "neutron/tests/test_assert.py"))))
self.assertEqual(1, len(list(checks.check_assertIsNone(
"self.assertEqual(None, A)", "neutron/tests/test_assert.py"))))
self.assertEqual(0, len(list(checks.check_assertIsNone(
"self.assertIsNone()", "neutron/tests/test_assert.py"))))
# The following is borrowed from hacking/tests/test_doctest.py.
# Tests defined in docstring is easier to understand