test_dispatch.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import sys
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks import dispatch as module
  5. def hook_function(config, log_prefix, thing, value):
  6. '''
  7. This test function gets mocked out below.
  8. '''
  9. pass
  10. def test_call_hook_invokes_module_function_with_arguments_and_returns_value():
  11. hooks = {'super_hook': flexmock(), 'other_hook': flexmock()}
  12. expected_return_value = flexmock()
  13. test_module = sys.modules[__name__]
  14. flexmock(module).HOOK_NAME_TO_MODULE = {'super_hook': test_module}
  15. flexmock(test_module).should_receive('hook_function').with_args(
  16. hooks['super_hook'], 'prefix', 55, value=66
  17. ).and_return(expected_return_value).once()
  18. return_value = module.call_hook('hook_function', hooks, 'prefix', 'super_hook', 55, value=66)
  19. assert return_value == expected_return_value
  20. def test_call_hook_without_hook_config_skips_call():
  21. hooks = {'other_hook': flexmock()}
  22. test_module = sys.modules[__name__]
  23. flexmock(module).HOOK_NAME_TO_MODULE = {'super_hook': test_module}
  24. flexmock(test_module).should_receive('hook_function').never()
  25. module.call_hook('hook_function', hooks, 'prefix', 'super_hook', 55, value=66)
  26. def test_call_hook_without_corresponding_module_raises():
  27. hooks = {'super_hook': flexmock(), 'other_hook': flexmock()}
  28. test_module = sys.modules[__name__]
  29. flexmock(module).HOOK_NAME_TO_MODULE = {'other_hook': test_module}
  30. flexmock(test_module).should_receive('hook_function').never()
  31. with pytest.raises(ValueError):
  32. module.call_hook('hook_function', hooks, 'prefix', 'super_hook', 55, value=66)
  33. def test_call_hooks_calls_each_hook_and_collects_return_values():
  34. hooks = {'super_hook': flexmock(), 'other_hook': flexmock()}
  35. expected_return_values = {'super_hook': flexmock(), 'other_hook': flexmock()}
  36. flexmock(module).should_receive('call_hook').and_return(
  37. expected_return_values['super_hook']
  38. ).and_return(expected_return_values['other_hook'])
  39. return_values = module.call_hooks('do_stuff', hooks, 'prefix', ('super_hook', 'other_hook'), 55)
  40. assert return_values == expected_return_values
  41. def test_call_hooks_calls_skips_return_values_for_unconfigured_hooks():
  42. hooks = {'super_hook': flexmock()}
  43. expected_return_values = {'super_hook': flexmock()}
  44. flexmock(module).should_receive('call_hook').and_return(expected_return_values['super_hook'])
  45. return_values = module.call_hooks('do_stuff', hooks, 'prefix', ('super_hook', 'other_hook'), 55)
  46. assert return_values == expected_return_values