borgmatic/tests/unit/config/test_override.py

152 lines
5.1 KiB
Python
Raw Normal View History

import pytest
import ruamel.yaml
2021-11-29 23:00:14 +01:00
from flexmock import flexmock
from borgmatic.config import override as module
def test_set_values_with_empty_keys_bails():
config = {}
module.set_values(config, keys=(), value='value')
assert config == {}
def test_set_values_with_one_key_sets_it_into_config():
config = {}
module.set_values(config, keys=('key',), value='value')
assert config == {'key': 'value'}
def test_set_values_with_one_key_overwrites_existing_key():
config = {'key': 'old_value', 'other': 'other_value'}
module.set_values(config, keys=('key',), value='value')
assert config == {'key': 'value', 'other': 'other_value'}
def test_set_values_with_multiple_keys_creates_hierarchy():
config = {}
2023-07-09 08:14:30 +02:00
module.set_values(config, ('option', 'suboption'), 'value')
2023-07-09 08:14:30 +02:00
assert config == {'option': {'suboption': 'value'}}
def test_set_values_with_multiple_keys_updates_hierarchy():
2023-07-09 08:14:30 +02:00
config = {'option': {'other': 'other_value'}}
module.set_values(config, ('option', 'key'), 'value')
2023-07-09 08:14:30 +02:00
assert config == {'option': {'key': 'value', 'other': 'other_value'}}
def test_set_values_with_key_when_list_index_expected_errors():
config = {'option': ['foo', 'bar', 'baz']}
with pytest.raises(ValueError):
module.set_values(config, keys=('option', 'key'), value='value')
@pytest.mark.parametrize(
'schema,option_keys,expected_type',
(
({'properties': {'foo': {'type': 'array'}}}, ('foo',), 'array'),
(
{'properties': {'foo': {'properties': {'bar': {'type': 'array'}}}}},
('foo', 'bar'),
'array',
),
({'properties': {'foo': {'type': 'array'}}}, ('other',), None),
({'properties': {'foo': {'description': 'stuff'}}}, ('foo',), None),
({}, ('foo',), None),
),
)
def test_type_for_option_grabs_type_if_found_in_schema(schema, option_keys, expected_type):
assert module.type_for_option(schema, option_keys) == expected_type
2023-07-09 08:14:30 +02:00
@pytest.mark.parametrize(
'key,expected_key',
(
(('foo', 'bar'), ('foo', 'bar')),
(('location', 'foo'), ('foo',)),
(('storage', 'foo'), ('foo',)),
(('retention', 'foo'), ('foo',)),
(('consistency', 'foo'), ('foo',)),
(('output', 'foo'), ('foo',)),
(('hooks', 'foo', 'bar'), ('foo', 'bar')),
(('foo', 'hooks'), ('foo', 'hooks')),
),
)
def test_strip_section_names_passes_through_key_without_section_name(key, expected_key):
assert module.strip_section_names(key) == expected_key
def test_parse_overrides_splits_keys_and_values():
2023-07-09 08:14:30 +02:00
flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
flexmock(module).should_receive('type_for_option').and_return('string')
flexmock(module).should_receive('convert_value_type').replace_with(
lambda value, option_type: value
)
2023-07-09 08:14:30 +02:00
raw_overrides = ['option.my_option=value1', 'other_option=value2']
expected_result = (
2023-07-09 08:14:30 +02:00
(('option', 'my_option'), 'value1'),
(('other_option'), 'value2'),
)
module.parse_overrides(raw_overrides, schema={}) == expected_result
def test_parse_overrides_allows_value_with_equal_sign():
2023-07-09 08:14:30 +02:00
flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
flexmock(module).should_receive('type_for_option').and_return('string')
flexmock(module).should_receive('convert_value_type').replace_with(
lambda value, option_type: value
)
2023-07-09 08:14:30 +02:00
raw_overrides = ['option=this===value']
expected_result = ((('option',), 'this===value'),)
module.parse_overrides(raw_overrides, schema={}) == expected_result
def test_parse_overrides_raises_on_missing_equal_sign():
2023-07-09 08:14:30 +02:00
flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
flexmock(module).should_receive('type_for_option').and_return('string')
flexmock(module).should_receive('convert_value_type').replace_with(
lambda value, option_type: value
)
2023-07-09 08:14:30 +02:00
raw_overrides = ['option']
with pytest.raises(ValueError):
module.parse_overrides(raw_overrides, schema={})
def test_parse_overrides_raises_on_invalid_override_value():
2023-07-09 08:14:30 +02:00
flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
flexmock(module).should_receive('type_for_option').and_return('string')
flexmock(module).should_receive('convert_value_type').and_raise(ruamel.yaml.parser.ParserError)
2023-07-09 08:14:30 +02:00
raw_overrides = ['option=[in valid]']
with pytest.raises(ValueError):
module.parse_overrides(raw_overrides, schema={})
def test_parse_overrides_allows_value_with_single_key():
2023-07-09 08:14:30 +02:00
flexmock(module).should_receive('strip_section_names').replace_with(lambda value: value)
flexmock(module).should_receive('type_for_option').and_return('string')
flexmock(module).should_receive('convert_value_type').replace_with(
lambda value, option_type: value
)
raw_overrides = ['option=value']
expected_result = ((('option',), 'value'),)
module.parse_overrides(raw_overrides, schema={}) == expected_result
def test_parse_overrides_handles_empty_overrides():
module.parse_overrides(raw_overrides=None, schema={}) == ()