Browse Source

Use assertIsNotNone instead of assertEqual(** is not None)

Developer should use assertIsNotNone to replace the
assertEqual(** is not None).

Closes-Bug: #1510371
Change-Id: I5afd03aaf591e047b855bc416bc60fbc182bdc19
changes/85/239585/8
wangqun 7 years ago
parent
commit
082c8ed0b8
  1. 2
      HACKING.rst
  2. 11
      magnum/hacking/checks.py
  3. 6
      magnum/tests/functional/test_k8s_python_client.py
  4. 8
      magnum/tests/functional/test_magnum_python_client.py
  5. 7
      magnum/tests/unit/test_hacking.py

2
HACKING.rst

@ -14,3 +14,5 @@ Magnum Specific Commandments
- [M322] Method's default argument shouldn't be mutable.
- [M323] Change assertEqual(True, A) or assertEqual(False, A) by optimal assert
like assertTrue(A) or assertFalse(A)
- [M302] Change assertEqual(A is not None) by optimal assert like
assertIsNotNone(A).

11
magnum/hacking/checks.py

@ -41,6 +41,8 @@ assert_equal_with_true_re = re.compile(
r"assertEqual\(True,")
assert_equal_with_false_re = re.compile(
r"assertEqual\(False,")
asse_equal_with_is_not_none_re = re.compile(
r"assertEqual\(.*?\s+is+\s+not+\s+None\)$")
def check_policy_enforce_decorator(logical_line,
@ -84,8 +86,17 @@ def assert_equal_true_or_false(logical_line):
"sentences not allowed")
def assert_equal_not_none(logical_line):
"""Check for assertEqual(A is not None) sentences M302"""
msg = "M302: assertEqual(A is not None) sentences not allowed."
res = asse_equal_with_is_not_none_re.search(logical_line)
if res:
yield (0, msg)
def factory(register):
register(check_policy_enforce_decorator)
register(no_mutable_default_args)
register(assert_equal_none)
register(assert_equal_true_or_false)
register(assert_equal_not_none)

6
magnum/tests/functional/test_k8s_python_client.py

@ -128,10 +128,10 @@ class TestKubernetesAPIs(BaseMagnumClient):
name='frontend', body={}, namespace='default')
def test_pods_list(self):
self.assertTrue(self.cs.pods.list(self.bay.uuid) is not None)
self.assertIsNotNone(self.cs.pods.list(self.bay.uuid))
def test_rcs_list(self):
self.assertTrue(self.cs.rcs.list(self.bay.uuid) is not None)
self.assertIsNotNone(self.cs.rcs.list(self.bay.uuid))
def test_services_list(self):
self.assertTrue(self.cs.services.list(self.bay.uuid) is not None)
self.assertIsNotNone(self.cs.services.list(self.bay.uuid))

8
magnum/tests/functional/test_magnum_python_client.py

@ -16,13 +16,13 @@ from magnum.tests.functional.python_client_base import BaseMagnumClient
class TestListResources(BaseMagnumClient):
def test_bay_model_list(self):
self.assertTrue(self.cs.baymodels.list() is not None)
self.assertIsNotNone(self.cs.baymodels.list())
def test_bay_list(self):
self.assertTrue(self.cs.bays.list() is not None)
self.assertIsNotNone(self.cs.bays.list())
def test_containers_list(self):
self.assertTrue(self.cs.containers.list() is not None)
self.assertIsNotNone(self.cs.containers.list())
def test_nodes_list(self):
self.assertTrue(self.cs.nodes.list() is not None)
self.assertIsNotNone(self.cs.nodes.list())

7
magnum/tests/unit/test_hacking.py

@ -120,3 +120,10 @@ class HackingTestCase(base.TestCase):
self.assertEqual(0, len(list(checks.no_mutable_default_args(
"defined, undefined = [], {}"))))
def test_assert_is_not_none(self):
self.assertEqual(1, len(list(checks.assert_equal_not_none(
"self.assertEqual(A is not None)"))))
self.assertEqual(0, len(list(checks.assert_equal_not_none(
"self.assertIsNone()"))))

Loading…
Cancel
Save