From b9f21e5bc0170ec7d569067499d9d8b7ad71709e Mon Sep 17 00:00:00 2001 From: Alexander Maretskiy Date: Fri, 9 Sep 2016 19:11:15 +0300 Subject: [PATCH] [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 --- .../murano/test_murano_environments.py | 13 +++--------- tests/unit/test_mock.py | 21 +++++++++++++------ 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/tests/unit/plugins/openstack/context/murano/test_murano_environments.py b/tests/unit/plugins/openstack/context/murano/test_murano_environments.py index 8285c8c9..40eed37f 100644 --- a/tests/unit/plugins/openstack/context/murano/test_murano_environments.py +++ b/tests/unit/plugins/openstack/context/murano/test_murano_environments.py @@ -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() diff --git a/tests/unit/test_mock.py b/tests/unit/test_mock.py index 15e83d24..0514864e 100644 --- a/tests/unit/test_mock.py +++ b/tests/unit/test_mock.py @@ -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."