def test_yaml_loading_empty_config(yaml_path): """Test loading for empty config fails like setting non existing attributes.""" config = Configuration() with pytest.raises(ConfigurationError) as exc: config.load_yaml(yaml_path) assert "Configuration does not have an attribute 'test'." in str(exc.value)
def test_yaml_precedence(yaml_path): """Test that yaml definition has precedence over default values""" config = Configuration() config.add_option('test', option_type=str, default="voici_voila", env_var="TOP_SECRET_MESSAGE") assert config.test == "voici_voila" config.load_yaml(yaml_path) assert config.test == "from my yaml!"
def test_load_yaml_unknown_option(broken_yaml_path): """Test error message when yaml config contains unknown options""" config = Configuration() config.add_option("test", option_type=str, default="hello") assert config.test == "hello" with pytest.raises(ConfigurationError) as exc: config.load_yaml(broken_yaml_path) assert exc.match("Configuration does not have an attribute 'coucou'")
def test_load_yaml_with_dict_items(subdict_yaml_path): """Test that yaml config with items assigned with dicts is supported""" config = Configuration() default = {"default": "sub-dict"} config.add_option("test", option_type=dict, default=default) assert config.test == default config.test2 = Configuration() config.test2.add_option("test", option_type=dict, default=default) assert config.test2.test == default config.load_yaml(subdict_yaml_path) assert config.test == {"i am": "a sub-dict"} assert config.test2.test == {"me is": "sub-conf sub-dict"}
def test_env_var_precedence(yaml_path): """Test that env_var has precedence over yaml values""" config = Configuration() config.add_option('test', option_type=str, default="voici_voila", env_var="TOP_SECRET_MESSAGE") assert config.test == "voici_voila" config.load_yaml(yaml_path) assert config.test == "from my yaml!" os.environ['TOP_SECRET_MESSAGE'] = 'coussi_coussa' assert config.test == "coussi_coussa" del os.environ['TOP_SECRET_MESSAGE'] assert config.test == "from my yaml!"
def test_local_precedence(yaml_path): """Test local setting has precedence over env var values""" config = Configuration() config.add_option("test", option_type=str, default="voici_voila", env_var="TOP_SECRET_MESSAGE") assert config.test == "voici_voila" config.load_yaml(yaml_path) assert config.test == "from my yaml!" os.environ["TOP_SECRET_MESSAGE"] = "coussi_coussa" assert config.test == "coussi_coussa" config.test = "comme_ci_comme_ca" assert config.test == "comme_ci_comme_ca" del os.environ["TOP_SECRET_MESSAGE"]