[Tests] Ignore too long mock names

It is very inconvenient and even sometimes not possible
(PEP8 restrictions for line length) to use long mock names,
so they are ignored if name if longer than
FuncMockArgsDecoratorsChecker.SHORTEST_VARIANT_LEN_LIMIT.

For example, existing name
`mock_murano_scenario__create_environment' is simplified
in this patch.

Change-Id: Ia2adf7098c60497c3b1fe5048e1145e47b56bc18
This commit is contained in:
Alexander Maretskiy 2016-09-09 19:11:15 +03:00
parent 22e781e965
commit b9f21e5bc0
2 changed files with 18 additions and 16 deletions

View File

@ -63,27 +63,20 @@ class MuranoEnvironmentGeneratorTestCase(test.TestCase):
}
@mock.patch("%s.murano.utils.MuranoScenario._create_environment" % SCN)
def test_setup(self, mock_murano_scenario__create_environment):
mock_env = mock.MagicMock()
mock_murano_scenario__create_environment.return_value = mock_env
def test_setup(self, mock_create_env):
murano_ctx = murano_environments.EnvironmentGenerator(
self._get_context())
murano_ctx.setup()
self.assertEqual(2, len(murano_ctx.context["tenants"]))
tenant_id = murano_ctx.context["users"][0]["tenant_id"]
self.assertEqual([mock_env],
self.assertEqual([mock_create_env.return_value],
murano_ctx.context["tenants"][tenant_id][
"environments"])
@mock.patch("%s.murano.utils.MuranoScenario._create_environment" % SCN)
@mock.patch("%s.resource_manager.cleanup" % CTX)
def test_cleanup(self, mock_cleanup,
mock_murano_scenario__create_environment):
mock_env = mock.Mock()
mock_murano_scenario__create_environment.return_value = mock_env
def test_cleanup(self, mock_cleanup, mock_create_env):
murano_ctx = murano_environments.EnvironmentGenerator(
self._get_context())
murano_ctx.setup()

View File

@ -90,6 +90,12 @@ class FuncMockArgsDecoratorsChecker(ast.NodeVisitor):
def test_foobar(self, mock_class_abc):
# must match the python-styled class name + method name
"""
# NOTE(amaretskiy): Disable check if shortest variant is too long
# because long name is not convenient and could
# even be blocked by PEP8
SHORTEST_VARIANT_LEN_LIMIT = 25
def __init__(self):
self.errors = []
self.globals_ = {}
@ -252,12 +258,15 @@ class FuncMockArgsDecoratorsChecker(ast.NodeVisitor):
for arg, dec_vars in six.moves.zip_longest(mock_args, mock_decs):
if not self.check_name(arg, dec_vars):
if arg and dec_vars:
error_msgs.append(
("Argument '%(arg)s' misnamed; should be either of "
"%(dec)s that is derived from the mock decorator "
"args.\n") % {
"arg": arg, "dec": dec_vars}
)
sorted_by_len = sorted(
dec_vars.variants, key=lambda i: len(i), reverse=True)
shortest_name = sorted_by_len.pop()
if len(shortest_name) <= self.SHORTEST_VARIANT_LEN_LIMIT:
error_msgs.append(
("Argument '%(arg)s' misnamed; should be either "
"of %(dec)s that is derived from the mock "
"decorator args.\n") % {"arg": arg,
"dec": dec_vars})
elif not arg:
error_msgs.append(
"Missing or malformed argument for %s decorator."