2
0

test_dispatch.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_invokes_module_function_with_arguments_and_returns_value():
  21. hooks = {'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. {}, 'prefix', 55, value=66
  27. ).and_return(expected_return_value).once()
  28. return_value = module.call_hook('hook_function', hooks, 'prefix', 'super_hook', 55, value=66)
  29. assert return_value == expected_return_value
  30. def test_call_hook_without_corresponding_module_raises():
  31. hooks = {'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', hooks, 'prefix', 'super_hook', 55, value=66)
  37. def test_call_hooks_calls_each_hook_and_collects_return_values():
  38. hooks = {'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('do_stuff', hooks, 'prefix', ('super_hook', 'other_hook'), 55)
  44. assert return_values == expected_return_values
  45. def test_call_hooks_calls_skips_return_values_for_missing_hooks():
  46. hooks = {'super_hook': flexmock()}
  47. expected_return_values = {'super_hook': flexmock()}
  48. flexmock(module).should_receive('call_hook').and_return(expected_return_values['super_hook'])
  49. return_values = module.call_hooks('do_stuff', hooks, 'prefix', ('super_hook', 'other_hook'), 55)
  50. assert return_values == expected_return_values
  51. def test_call_hooks_calls_skips_return_values_for_null_hooks():
  52. hooks = {'super_hook': flexmock(), 'other_hook': None}
  53. expected_return_values = {'super_hook': flexmock()}
  54. flexmock(module).should_receive('call_hook').and_return(expected_return_values['super_hook'])
  55. return_values = module.call_hooks('do_stuff', hooks, 'prefix', ('super_hook', 'other_hook'), 55)
  56. assert return_values == expected_return_values
  57. def test_call_hooks_even_if_unconfigured_calls_each_hook_and_collects_return_values():
  58. hooks = {'super_hook': flexmock(), 'other_hook': flexmock()}
  59. expected_return_values = {'super_hook': flexmock(), 'other_hook': flexmock()}
  60. flexmock(module).should_receive('call_hook').and_return(
  61. expected_return_values['super_hook']
  62. ).and_return(expected_return_values['other_hook'])
  63. return_values = module.call_hooks_even_if_unconfigured(
  64. 'do_stuff', hooks, 'prefix', ('super_hook', 'other_hook'), 55
  65. )
  66. assert return_values == expected_return_values
  67. def test_call_hooks_even_if_unconfigured_calls_each_hook_configured_or_not_and_collects_return_values():
  68. hooks = {'other_hook': flexmock()}
  69. expected_return_values = {'super_hook': flexmock(), 'other_hook': flexmock()}
  70. flexmock(module).should_receive('call_hook').and_return(
  71. expected_return_values['super_hook']
  72. ).and_return(expected_return_values['other_hook'])
  73. return_values = module.call_hooks_even_if_unconfigured(
  74. 'do_stuff', hooks, 'prefix', ('super_hook', 'other_hook'), 55
  75. )
  76. assert return_values == expected_return_values