test_dispatch.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import sys
  2. import pytest
  3. from flexmock import flexmock
  4. from borgmatic.hooks import dispatch as module
  5. def hook_function(hook_config, 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. config = {'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. config['super_hook'], config, 'prefix', 55, value=66
  17. ).and_return(expected_return_value).once()
  18. return_value = module.call_hook('hook_function', config, 'prefix', 'super_hook', 55, value=66)
  19. assert return_value == expected_return_value
  20. def test_call_hook_without_hook_config_invokes_module_function_with_arguments_and_returns_value():
  21. config = {'other_hook': flexmock()}
  22. expected_return_value = flexmock()
  23. test_module = sys.modules[__name__]
  24. flexmock(module).HOOK_NAME_TO_MODULE = {'super_hook': test_module}
  25. flexmock(test_module).should_receive('hook_function').with_args(
  26. {}, config, 'prefix', 55, value=66
  27. ).and_return(expected_return_value).once()
  28. return_value = module.call_hook('hook_function', config, 'prefix', 'super_hook', 55, value=66)
  29. assert return_value == expected_return_value
  30. def test_call_hook_without_corresponding_module_raises():
  31. config = {'super_hook': flexmock(), 'other_hook': flexmock()}
  32. test_module = sys.modules[__name__]
  33. flexmock(module).HOOK_NAME_TO_MODULE = {'other_hook': test_module}
  34. flexmock(test_module).should_receive('hook_function').never()
  35. with pytest.raises(ValueError):
  36. module.call_hook('hook_function', config, 'prefix', 'super_hook', 55, value=66)
  37. def test_call_hooks_calls_each_hook_and_collects_return_values():
  38. config = {'super_hook': flexmock(), 'other_hook': flexmock()}
  39. expected_return_values = {'super_hook': flexmock(), 'other_hook': flexmock()}
  40. flexmock(module).should_receive('call_hook').and_return(
  41. expected_return_values['super_hook']
  42. ).and_return(expected_return_values['other_hook'])
  43. return_values = module.call_hooks(
  44. 'do_stuff', config, 'prefix', ('super_hook', 'other_hook'), 55
  45. )
  46. assert return_values == expected_return_values
  47. def test_call_hooks_calls_skips_return_values_for_missing_hooks():
  48. config = {'super_hook': flexmock()}
  49. expected_return_values = {'super_hook': flexmock()}
  50. flexmock(module).should_receive('call_hook').and_return(expected_return_values['super_hook'])
  51. return_values = module.call_hooks(
  52. 'do_stuff', config, 'prefix', ('super_hook', 'other_hook'), 55
  53. )
  54. assert return_values == expected_return_values
  55. def test_call_hooks_calls_skips_return_values_for_null_hooks():
  56. config = {'super_hook': flexmock(), 'other_hook': None}
  57. expected_return_values = {'super_hook': flexmock()}
  58. flexmock(module).should_receive('call_hook').and_return(expected_return_values['super_hook'])
  59. return_values = module.call_hooks(
  60. 'do_stuff', config, 'prefix', ('super_hook', 'other_hook'), 55
  61. )
  62. assert return_values == expected_return_values
  63. def test_call_hooks_even_if_unconfigured_calls_each_hook_and_collects_return_values():
  64. config = {'super_hook': flexmock(), 'other_hook': flexmock()}
  65. expected_return_values = {'super_hook': flexmock(), 'other_hook': flexmock()}
  66. flexmock(module).should_receive('call_hook').and_return(
  67. expected_return_values['super_hook']
  68. ).and_return(expected_return_values['other_hook'])
  69. return_values = module.call_hooks_even_if_unconfigured(
  70. 'do_stuff', config, 'prefix', ('super_hook', 'other_hook'), 55
  71. )
  72. assert return_values == expected_return_values
  73. def test_call_hooks_even_if_unconfigured_calls_each_hook_configured_or_not_and_collects_return_values():
  74. config = {'other_hook': flexmock()}
  75. expected_return_values = {'super_hook': flexmock(), 'other_hook': flexmock()}
  76. flexmock(module).should_receive('call_hook').and_return(
  77. expected_return_values['super_hook']
  78. ).and_return(expected_return_values['other_hook'])
  79. return_values = module.call_hooks_even_if_unconfigured(
  80. 'do_stuff', config, 'prefix', ('super_hook', 'other_hook'), 55
  81. )
  82. assert return_values == expected_return_values