2
0

test_override.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import pytest
  2. import ruamel.yaml
  3. from flexmock import flexmock
  4. from borgmatic.config import override as module
  5. def test_set_values_with_empty_keys_bails():
  6. config = {}
  7. module.set_values(config, keys=(), value='value')
  8. assert config == {}
  9. def test_set_values_with_one_key_sets_it_into_config():
  10. config = {}
  11. module.set_values(config, keys=('key',), value='value')
  12. assert config == {'key': 'value'}
  13. def test_set_values_with_one_key_overwrites_existing_key():
  14. config = {'key': 'old_value', 'other': 'other_value'}
  15. module.set_values(config, keys=('key',), value='value')
  16. assert config == {'key': 'value', 'other': 'other_value'}
  17. def test_set_values_with_multiple_keys_creates_hierarchy():
  18. config = {}
  19. module.set_values(config, ('option', 'suboption'), 'value')
  20. assert config == {'option': {'suboption': 'value'}}
  21. def test_set_values_with_multiple_keys_updates_hierarchy():
  22. config = {'option': {'other': 'other_value'}}
  23. module.set_values(config, ('option', 'key'), 'value')
  24. assert config == {'option': {'key': 'value', 'other': 'other_value'}}
  25. @pytest.mark.parametrize(
  26. 'key,expected_key',
  27. (
  28. (('foo', 'bar'), ('foo', 'bar')),
  29. (('location', 'foo'), ('foo',)),
  30. (('storage', 'foo'), ('foo',)),
  31. (('retention', 'foo'), ('foo',)),
  32. (('consistency', 'foo'), ('foo',)),
  33. (('output', 'foo'), ('foo',)),
  34. (('hooks', 'foo', 'bar'), ('foo', 'bar')),
  35. (('foo', 'hooks'), ('foo', 'hooks')),
  36. ),
  37. )
  38. def test_strip_section_names_passes_through_key_without_section_name(key, expected_key):
  39. assert module.strip_section_names(key) == expected_key
  40. def test_parse_overrides_splits_keys_and_values():
  41. flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
  42. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  43. raw_overrides = ['option.my_option=value1', 'other_option=value2']
  44. expected_result = (
  45. (('option', 'my_option'), 'value1'),
  46. (('other_option'), 'value2'),
  47. )
  48. module.parse_overrides(raw_overrides) == expected_result
  49. def test_parse_overrides_allows_value_with_equal_sign():
  50. flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
  51. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  52. raw_overrides = ['option=this===value']
  53. expected_result = ((('option',), 'this===value'),)
  54. module.parse_overrides(raw_overrides) == expected_result
  55. def test_parse_overrides_raises_on_missing_equal_sign():
  56. flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
  57. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  58. raw_overrides = ['option']
  59. with pytest.raises(ValueError):
  60. module.parse_overrides(raw_overrides)
  61. def test_parse_overrides_raises_on_invalid_override_value():
  62. flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
  63. flexmock(module).should_receive('convert_value_type').and_raise(ruamel.yaml.parser.ParserError)
  64. raw_overrides = ['option=[in valid]']
  65. with pytest.raises(ValueError):
  66. module.parse_overrides(raw_overrides)
  67. def test_parse_overrides_allows_value_with_single_key():
  68. flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
  69. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  70. raw_overrides = ['option=value']
  71. expected_result = ((('option',), 'value'),)
  72. module.parse_overrides(raw_overrides) == expected_result
  73. def test_parse_overrides_handles_empty_overrides():
  74. module.parse_overrides(raw_overrides=None) == ()