Allow usage of assert_called_once method in unit tests

mock.assert_called_once is valid method since Python 3.6
but it's not allowed to use it in neutron because of
outdated flake8 check.

Change-Id: I8cdcc0e32618613472139ad9094bb19d562d6426
changes/53/710753/1
Igor Malinovskiy 3 years ago
parent f97ae3d6f8
commit 248cdaa6f7
  1. 3
      neutron/hacking/checks.py
  2. 8
      neutron/tests/unit/hacking/test_checks.py

@ -51,7 +51,6 @@ tests_imports_from2 = re.compile(r"\bfrom[\s]+neutron[\s]+import[\s]+tests\b")
@flake8ext
def check_assert_called_once_with(logical_line, filename):
"""N322 - Try to detect unintended calls of nonexistent mock methods like:
assert_called_once
assertCalledOnceWith
assert_has_called
called_once_with
@ -61,7 +60,7 @@ def check_assert_called_once_with(logical_line, filename):
return
uncased_line = logical_line.lower().replace('_', '')
check_calls = ['.assertcalledonce', '.calledoncewith']
check_calls = ['.assertcalledoncewith', '.calledoncewith']
if any(x for x in check_calls if x in uncased_line):
msg = ("N322: Possible use of no-op mock method. "
"please use assert_called_once_with.")

@ -38,11 +38,6 @@ class HackingTestCase(base.BaseTestCase):
self.assertIsInstance(next(func(line)), tuple)
def test_assert_called_once_with(self):
fail_code1 = """
mock = Mock()
mock.method(1, 2, 3, test='wow')
mock.method.assert_called_once()
"""
fail_code2 = """
mock = Mock()
mock.method(1, 2, 3, test='wow')
@ -68,9 +63,6 @@ class HackingTestCase(base.BaseTestCase):
mock.method(1, 2, 3, test='wow')
mock.method.assert_has_calls()
"""
self.assertEqual(
1, len(list(checks.check_assert_called_once_with(fail_code1,
"neutron/tests/test_assert.py"))))
self.assertEqual(
1, len(list(checks.check_assert_called_once_with(fail_code2,
"neutron/tests/test_assert.py"))))

Loading…
Cancel
Save