Skip to content

Commit

Permalink
test helper: remove magically finding the testcasemock in the test mo…
Browse files Browse the repository at this point in the history
…dule (#9675)
  • Loading branch information
russoz authored Feb 3, 2025
1 parent 7a6125b commit 4a31c75
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions tests/unit/plugins/modules/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,19 @@ def make_test_case(test_case_spec, test_module, mocks_map):
mocks=test_case_spec.get("mocks", {}),
flags=test_case_spec.get("flags", {})
)
tc.build_mocks(test_module, mocks_map)
tc.build_mocks(mocks_map)
return tc

def build_mocks(self, test_module, mocks_map):
def build_mocks(self, mocks_map):
for mock_name, mock_spec in self.mock_specs.items():
mock_class = mocks_map.get(mock_name, self.get_mock_class(test_module, mock_name))
try:
mock_class = mocks_map[mock_name]
except KeyError:
raise Exception("Cannot find TestCaseMock class for: {0}".format(mock_name))
self.mocks[mock_name] = mock_class.build_mock(mock_spec)

self._fixtures.update(self.mocks[mock_name].fixtures())

@staticmethod
def get_mock_class(test_module, mock):
try:
class_name = "".join(x.capitalize() for x in mock.split("_")) + "Mock"
plugin_class = getattr(test_module, class_name)
assert issubclass(plugin_class, TestCaseMock), "Class {0} is not a subclass of TestCaseMock".format(class_name)
return plugin_class
except AttributeError:
raise ValueError("Cannot find class {0} for mock {1}".format(class_name, mock))

@property
def fixtures(self):
return dict(self._fixtures)
Expand Down Expand Up @@ -207,10 +200,6 @@ def check_mocks(self, test_case, results):


class TestCaseMock:
@property
def name(self):
raise NotImplementedError()

@classmethod
def build_mock(cls, mock_specs):
return cls(mock_specs)
Expand All @@ -229,9 +218,7 @@ def check(self, test_case, results):


class RunCommandMock(TestCaseMock):
@property
def name(self):
return "run_command"
name = "run_command"

def __str__(self):
return "<RunCommandMock specs={specs}>".format(specs=self.mock_specs)
Expand Down

0 comments on commit 4a31c75

Please sign in to comment.