test_override.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, ('section', 'key'), 'value')
  20. assert config == {'section': {'key': 'value'}}
  21. def test_set_values_with_multiple_keys_updates_hierarchy():
  22. config = {'section': {'other': 'other_value'}}
  23. module.set_values(config, ('section', 'key'), 'value')
  24. assert config == {'section': {'key': 'value', 'other': 'other_value'}}
  25. def test_parse_overrides_splits_keys_and_values():
  26. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  27. raw_overrides = ['section.my_option=value1', 'section.other_option=value2']
  28. expected_result = (
  29. (('section', 'my_option'), 'value1'),
  30. (('section', 'other_option'), 'value2'),
  31. )
  32. module.parse_overrides(raw_overrides) == expected_result
  33. def test_parse_overrides_allows_value_with_equal_sign():
  34. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  35. raw_overrides = ['section.option=this===value']
  36. expected_result = ((('section', 'option'), 'this===value'),)
  37. module.parse_overrides(raw_overrides) == expected_result
  38. def test_parse_overrides_raises_on_missing_equal_sign():
  39. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  40. raw_overrides = ['section.option']
  41. with pytest.raises(ValueError):
  42. module.parse_overrides(raw_overrides)
  43. def test_parse_overrides_raises_on_invalid_override_value():
  44. flexmock(module).should_receive('convert_value_type').and_raise(ruamel.yaml.parser.ParserError)
  45. raw_overrides = ['section.option=[in valid]']
  46. with pytest.raises(ValueError):
  47. module.parse_overrides(raw_overrides)
  48. def test_parse_overrides_allows_value_with_single_key():
  49. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  50. raw_overrides = ['option=value']
  51. expected_result = ((('option',), 'value'),)
  52. module.parse_overrides(raw_overrides) == expected_result
  53. def test_parse_overrides_handles_empty_overrides():
  54. module.parse_overrides(raw_overrides=None) == ()