def test_register_a_second_defaults_overrides_existing_one(self): env_adapter = Env() defaults_adapter = Defaults({"abc": "123"}) config = Config() config.adapters = [defaults_adapter, env_adapter] new_defaults = Defaults({"easy as": "do re mi"}) config.register(new_defaults) assert config.defaults == new_defaults
def test_adapters_setter_raises_if_defaults_is_not_first(self): defaults_adapter = Defaults({"abc": "123"}) env_adapter = Env() config = Config() with pytest.raises(ValueError): # defaults_adapter should always be first config.adapters = [env_adapter, defaults_adapter]
def test_register_sets_defaults_adapter_as_first_with_empty_adapters(self): defaults_adapter = Defaults({"abc": "123"}) config = Config() config.adapters = [] config.register(defaults_adapter) assert len(config.adapters) == 1 assert config.adapters[0] == defaults_adapter
def test_load_method_loads_values_from_adapters(self): defaults = Defaults({"abc": "123"}) config = Config() config.register(defaults) config.load() assert "ABC" in config assert config["ABC"] == "123"
def test_register_a_second_defaults_raises(self): env_adapter = Env() defaults_adapter = Defaults({"abc": "123"}) config = Config() config.adapters = [defaults_adapter, env_adapter] with pytest.raises(ValueError): config.register(defaults_adapter)
def test_register_sets_defaults_adapter_as_first_with_existing_adapters( self): env_adapter = Env() defaults_adapter = Defaults({"abc": "123"}) config = Config() config.adapters = [env_adapter] config.register(defaults_adapter) assert config.adapters[0] == defaults_adapter assert config.adapters[1] == env_adapter
def __init__(self, defaults=None, adapters=None): self.adapters = adapters or [] if defaults is not None: if not isinstance(defaults, (Defaults, dict)): raise TypeError("defaults has to be of Defaults type") if isinstance(defaults, dict): self.register(Defaults(data=defaults)) else: self.register(defaults)
def test_register_multiple_adapters(self): config = Config() defaults_adapter = Defaults() env_adapter = Env() file_adapter = File('/tmp/test') config.register(defaults_adapter, env_adapter, file_adapter) assert len(config.adapters) == 3 assert config.adapters[0] == defaults_adapter assert config.adapters[1] == env_adapter assert config.adapters[2] == file_adapter
def test_load(self): d = Defaults({"abc": "123"}) d.load() assert "ABC" in d.data assert d.data["ABC"] == "123"
def test_init_with_defaults_provided(self): defaults_adapter = Defaults({"abc": "123"}) config = Config(defaults_adapter) assert len(config.adapters) == 1 assert config.adapters[0] == defaults_adapter