def test_prevent_writing_to_locked_source(): data = {'a': 1, 'b': {'c': 2, 'd': {'e': 3}}} config = DictSource(data, readonly=True) with pytest.raises(TypeError) as exc_info: config.a = 10 assert 'locked' in str(exc_info.value)
def test_write_source_with_custom_types(): data = {'a': 1, 'b': {'c': 2}} types = { 'a': CustomType(customize=str, reset=int), 'c': CustomType(lambda v: 2*v, lambda v: v/2) } config = DictSource(data, type_map=types) config.a = '1' config.b.c = 4 assert config._data == data
def test_write_cached_dict_source(): config = DictSource({}, cached=True) config.a = 1 config.b = {} config.b.c = 2 config.b.d = {} config.b.d.e = 3 assert config._data == {} config.write_cache() assert config._data == {'a': 1, 'b': {'c': 2, 'd': {'e': 3}}}
def test_write_dict_source(): data = {'a': 1, 'b': {'c': 2, 'd': {'e': 3}}} config = DictSource(data) assert config.a == 1 assert config.b.c == 2 assert config.b.d == {'e': 3} config.a = 10 config.b.c = 20 del config.b.d.e assert config.a == 10 assert config.b.c == 20 with pytest.raises(KeyError): config.b.d.e