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
This commit is contained in:
Igor Malinovskiy 2020-03-02 14:11:40 +02:00
parent f97ae3d6f8
commit 248cdaa6f7
2 changed files with 1 additions and 10 deletions

View File

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

View File

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