Skip to content

Commit

Permalink
Implement tests for controller.run_hook
Browse files Browse the repository at this point in the history
  • Loading branch information
c-mita committed Aug 15, 2016
1 parent 630ed94 commit ade350c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion malcolm/core/controller.py
Expand Up @@ -267,6 +267,6 @@ def _gather_task_return_value(func, task):
task.stop()
for task in func_tasks.values():
task.wait()
raise
raise ret

return return_dict
38 changes: 34 additions & 4 deletions tests/test_core/test_controller.py
Expand Up @@ -42,12 +42,42 @@ def test_set_writeable_methods(self):
self.assertEqual(self.c.methods_writeable['Ready'][m], True)

def test_run_hook(self):
# TODO: write this
pass
hook = MagicMock()
func = MagicMock()
task = MagicMock()
part = MagicMock()
hook_tasks = {func:task}
part_tasks = {part:task}
hook_queue = self.c.process.create_queue.return_value
hook_queue.get.return_value = (func, func.return_value)
hook.find_func_tasks.return_value = {func:task}
self.c.test_hook = hook
self.c.hook_names = {hook:"test_hook"}
self.c.parts = {"test_part":part}
result = self.c.run_hook(hook, part_tasks)

# TODO: would like this assertion - difficult to test with mocks
#hook_queue.put.assert_called_once_with((func, func.return_value))
self.assertEquals({"test_part":func.return_value}, result)

def test_run_hook_raises(self):
# TODO: write this
pass
hook = MagicMock()
func = MagicMock(side_effect=Exception("Test Exception"))
task = MagicMock()
part = MagicMock()
hook_tasks = {func:task}
part_tasks = {part:task}
hook_queue = self.c.process.create_queue.return_value
hook_queue.get.return_value = (func, func.side_effect)
hook.find_func_tasks.return_value = {func:task}
self.c.test_hook = hook
self.c.hook_names = {hook:"test_hook"}
self.c.parts = {"test_part":part}

with self.assertRaises(Exception) as cm:
self.c.run_hook(hook, part_tasks)
self.assertIs(func.side_effect, cm.exception)
#TODO: test hook_queue.put/get mechanism

if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit ade350c

Please sign in to comment.