def test_default_keyfmt(): os.environ["TEST"] = "test" config = Environment() assert "test" in config assert "test" == config["test"] del os.environ["TEST"]
def test_cache_false(env_config): config = ChildClassyConf(loaders=[Environment(), EnvFile(env_config)], cache=False) assert config.ENVFILE == "Environment File Value" os.environ["ENVFILE"] = "Environment Variable Value" assert config.ENVFILE == "Environment Variable Value" del os.environ["ENVFILE"]
def test_basic_config(): os.environ["TEST"] = "test" config = Environment() assert "TEST" in config assert "test" == config["TEST"] assert repr(config).startswith("Environment(keyfmt=") del os.environ["TEST"]
def test_customized_loaders(env_config, ini_config): os.environ["ENVVAR"] = "Environment Variable Value" os.environ["ENVVAR2"] = "Foo" loaders = [EnvFile(env_config), Environment(), IniFile(ini_config)] assert getconf("ENVVAR", loaders=loaders) == "Must be overrided" assert getconf("ENVVAR2", loaders=loaders) == "Foo" assert getconf("ENVFILE", loaders=loaders) == "Environment File Value" assert getconf("INIFILE", loaders=loaders) == "INI File Value" del os.environ["ENVVAR"] del os.environ["ENVVAR2"]
def test_custom_keyfmt(): def formatter(x): return "_{}".format(x) os.environ["_TEST"] = "test" config = Environment(keyfmt=formatter) assert "TEST" in config assert "test" == config["TEST"] del os.environ["_TEST"]
def test_str_as_default_value(): os.environ["STR"] = "1" assert getconf("STR", default="foo", loaders=[Environment()]) == "1"
def test_int_as_default_value(): os.environ["INT"] = "1" assert getconf("INT", default=0, loaders=[Environment()]) == 1
def test_boolean_as_default_value(): os.environ["BOOLEAN"] = "1" assert getconf("BOOLEAN", default=False, loaders=[Environment()]) == True
def test_none_default_value_identity_cast(): os.environ["DEFAULT"] = "1" assert getconf("DEFAULT", default=None, loaders=[Environment()]) == "1"
def test_none_as_default_value(): assert getconf("UNKNOWN", default=None, loaders=[Environment()]) is None
def test_fail_unknown_config_without_default_value(): os.environ["ENVVAR"] = "Environment Variable Value" with pytest.raises(UnknownConfiguration): getconf("UNKNOWN", loaders=[Environment()])
def test_fail_invalid_cast_type(): os.environ["INTEGER"] = "42" with pytest.raises(TypeError): getconf("INTEGER", cast="not callable", loaders=[Environment()])
def test_config_cast_value(): os.environ["INTEGER"] = "42" assert getconf("INTEGER", cast=int, loaders=[Environment()]) == 42
def test_config_default_values(): assert (getconf("DEFAULT", default="Default Value", loaders=[Environment()]) == "Default Value")
def test_fail_missing_config(): config = Environment() assert "UNKNOWN" not in config with pytest.raises(KeyError): _ = config["UNKNOWN"]