mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-11 22:45:05 +00:00
unit tests (modules): use f-strings (#10992)
* unit tests (modules): use f-strings * Apply suggestions from code review
This commit is contained in:
parent
f6781f654e
commit
e177d1e61a
33 changed files with 140 additions and 159 deletions
|
|
@ -37,7 +37,7 @@ class UTHelper(object):
|
|||
with open(test_spec_filename, "r") as test_spec_filehandle:
|
||||
return UTHelper.from_file(ansible_module, test_module, test_spec_filehandle, mocks=mocks)
|
||||
|
||||
raise Exception("Cannot find test case file for {0} with one of the extensions: {1}".format(test_module.__file__, extensions))
|
||||
raise Exception(f"Cannot find test case file for {test_module.__file__} with one of the extensions: {extensions}")
|
||||
|
||||
def add_func_to_test_module(self, name, func):
|
||||
setattr(self.test_module, name, func)
|
||||
|
|
@ -50,7 +50,7 @@ class UTHelper(object):
|
|||
|
||||
spec_diff = set(test_spec.keys()) - set(self.TEST_SPEC_VALID_SECTIONS)
|
||||
if spec_diff:
|
||||
raise ValueError("Test specification contain unknown keys: {0}".format(", ".join(spec_diff)))
|
||||
raise ValueError(f"Test specification contain unknown keys: {', '.join(spec_diff)}")
|
||||
|
||||
self.mocks_map = {m.name: m for m in mocks} if mocks else {}
|
||||
|
||||
|
|
@ -125,22 +125,11 @@ class ModuleTestCase:
|
|||
self._fixtures = {}
|
||||
|
||||
def __str__(self):
|
||||
return "<ModuleTestCase: id={id} {input}{output}mocks={mocks} flags={flags}>".format(
|
||||
id=self.id,
|
||||
input="input " if self.input else "",
|
||||
output="output " if self.output else "",
|
||||
mocks="({0})".format(", ".join(self.mocks.keys())),
|
||||
flags=self.flags
|
||||
)
|
||||
return (f"<ModuleTestCase: id={self.id} {'input ' if self.input else ''}{'output ' if self.output else ''}"
|
||||
f"mocks=({', '.join(self.mocks.keys())}) flags={self.flags}>")
|
||||
|
||||
def __repr__(self):
|
||||
return "ModuleTestCase(id={id}, input={input}, output={output}, mocks={mocks}, flags={flags})".format(
|
||||
id=self.id,
|
||||
input=self.input,
|
||||
output=self.output,
|
||||
mocks=repr(self.mocks),
|
||||
flags=self.flags
|
||||
)
|
||||
return f"ModuleTestCase(id={self.id}, input={self.input}, output={self.output}, mocks={self.mocks!r}, flags={self.flags})"
|
||||
|
||||
@staticmethod
|
||||
def make_test_case(test_case_spec, test_module, mocks_map):
|
||||
|
|
@ -159,7 +148,7 @@ class ModuleTestCase:
|
|||
try:
|
||||
mock_class = mocks_map[mock_name]
|
||||
except KeyError:
|
||||
raise Exception("Cannot find TestCaseMock class for: {0}".format(mock_name))
|
||||
raise Exception(f"Cannot find TestCaseMock class for: {mock_name}")
|
||||
self.mocks[mock_name] = mock_class.build_mock(mock_spec)
|
||||
|
||||
self._fixtures.update(self.mocks[mock_name].fixtures())
|
||||
|
|
@ -184,14 +173,14 @@ class ModuleTestCase:
|
|||
mock.setup(mocker)
|
||||
|
||||
def check_testcase(self, results):
|
||||
print("testcase =\n%s" % repr(self))
|
||||
print("results =\n%s" % results)
|
||||
print(f"testcase =\n{self!r}")
|
||||
print(f"results =\n{results}")
|
||||
if 'exception' in results:
|
||||
print("exception = \n%s" % results["exception"])
|
||||
print(f"exception = \n{results['exception']}")
|
||||
|
||||
for test_result in self.output:
|
||||
assert results[test_result] == self.output[test_result], \
|
||||
"'{0}': '{1}' != '{2}'".format(test_result, results[test_result], self.output[test_result])
|
||||
f"'{test_result}': '{results[test_result]}' != '{self.output[test_result]}'"
|
||||
|
||||
def check_mocks(self, test_case, results):
|
||||
for mock in self.mocks.values():
|
||||
|
|
@ -229,7 +218,7 @@ class RunCommandMock(TestCaseMock):
|
|||
@pytest.fixture
|
||||
def patch_bin(mocker):
|
||||
def mockie(self_, path, *args, **kwargs):
|
||||
return "/testbin/{0}".format(path)
|
||||
return f"/testbin/{path}"
|
||||
mocker.patch('ansible.module_utils.basic.AnsibleModule.get_bin_path', mockie)
|
||||
|
||||
return {"patch_bin": patch_bin}
|
||||
|
|
@ -245,7 +234,7 @@ class RunCommandMock(TestCaseMock):
|
|||
def side_effect(self_, **kwargs):
|
||||
result = next(results)
|
||||
if kwargs.get("check_rc", False) and result[0] != 0:
|
||||
raise Exception("rc = {0}".format(result[0]))
|
||||
raise Exception(f"rc = {result[0]}")
|
||||
return result
|
||||
|
||||
self.mock_run_cmd = mocker.patch('ansible.module_utils.basic.AnsibleModule.run_command', side_effect=side_effect)
|
||||
|
|
@ -253,9 +242,9 @@ class RunCommandMock(TestCaseMock):
|
|||
def check(self, test_case, results):
|
||||
call_args_list = [(item[0][0], item[1]) for item in self.mock_run_cmd.call_args_list]
|
||||
expected_call_args_list = [(item['command'], item.get('environ', {})) for item in self.mock_specs]
|
||||
print("call args list =\n%s" % call_args_list)
|
||||
print("expected args list =\n%s" % expected_call_args_list)
|
||||
print(f"call args list =\n{call_args_list}")
|
||||
print(f"expected args list =\n{expected_call_args_list}")
|
||||
|
||||
assert self.mock_run_cmd.call_count == len(self.mock_specs), "{0} != {1}".format(self.mock_run_cmd.call_count, len(self.mock_specs))
|
||||
assert self.mock_run_cmd.call_count == len(self.mock_specs), f"{self.mock_run_cmd.call_count} != {len(self.mock_specs)}"
|
||||
if self.mock_run_cmd.call_count:
|
||||
assert call_args_list == expected_call_args_list
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue