test_override.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import pytest
  2. from flexmock import flexmock
  3. from borgmatic.config import override as module
  4. def test_set_values_with_empty_keys_bails():
  5. config = {}
  6. module.set_values(config, keys=(), value='value')
  7. assert config == {}
  8. def test_set_values_with_one_key_sets_it_into_config():
  9. config = {}
  10. module.set_values(config, keys=('key',), value='value')
  11. assert config == {'key': 'value'}
  12. def test_set_values_with_one_key_overwrites_existing_key():
  13. config = {'key': 'old_value', 'other': 'other_value'}
  14. module.set_values(config, keys=('key',), value='value')
  15. assert config == {'key': 'value', 'other': 'other_value'}
  16. def test_set_values_with_multiple_keys_creates_hierarchy():
  17. config = {}
  18. module.set_values(config, ('section', 'key'), 'value')
  19. assert config == {'section': {'key': 'value'}}
  20. def test_set_values_with_multiple_keys_updates_hierarchy():
  21. config = {'section': {'other': 'other_value'}}
  22. module.set_values(config, ('section', 'key'), 'value')
  23. assert config == {'section': {'key': 'value', 'other': 'other_value'}}
  24. def test_parse_overrides_splits_keys_and_values():
  25. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  26. raw_overrides = ['section.my_option=value1', 'section.other_option=value2']
  27. expected_result = (
  28. (('section', 'my_option'), 'value1'),
  29. (('section', 'other_option'), 'value2'),
  30. )
  31. module.parse_overrides(raw_overrides) == expected_result
  32. def test_parse_overrides_allows_value_with_equal_sign():
  33. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  34. raw_overrides = ['section.option=this===value']
  35. expected_result = ((('section', 'option'), 'this===value'),)
  36. module.parse_overrides(raw_overrides) == expected_result
  37. def test_parse_overrides_raises_on_missing_equal_sign():
  38. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  39. raw_overrides = ['section.option']
  40. with pytest.raises(ValueError):
  41. module.parse_overrides(raw_overrides)
  42. def test_parse_overrides_allows_value_with_single_key():
  43. flexmock(module).should_receive('convert_value_type').replace_with(lambda value: value)
  44. raw_overrides = ['option=value']
  45. expected_result = ((('option',), 'value'),)
  46. module.parse_overrides(raw_overrides) == expected_result
  47. def test_parse_overrides_handles_empty_overrides():
  48. module.parse_overrides(raw_overrides=None) == ()