def test_env_var_is_set_then_gets_removed_without_default_raises_exception(self): os.environ["FOO"] = "BAR" config = Config() config.from_envar("FOO") del os.environ["FOO"] with pytest.raises(KeyError): config["FOO"]
def test_from_env_var_with_default_value(self): config = Config() os.environ["FOO"] = "BAR" config.from_envar("FOO", default="BAZ") assert config["FOO"] == "BAR" del os.environ["FOO"] assert config["FOO"] == "BAZ"
def test_from_envar_that_doesnt_exist_raises_warning(self): config = Config() with pytest.warns(RuntimeWarning) as warn: config.from_envar("BAR") assert len(warn) == 1 assert "not found" in warn[0].message.args[0]
def test_env_var_is_set_then_gets_removed_with_default_value_set(): config = Config() config.from_object(DevConfig) config.from_envar("FOO") assert config["FOO"] == "BAR" del os.environ["FOO"] assert config["FOO"] == "BAZ"
def test_from_envar_and_change_after_setting_value(self): os.environ["FOO"] = "BAR" config = Config() config.from_envar("FOO") assert config["FOO"] == "BAR" os.environ["FOO"] = "BAZ" assert config["FOO"] == "BAZ" del os.environ["FOO"]
def test_from_env_var_with_default_value_and_alias(self): config = Config() os.environ["FOO"] = "BAR" config.from_envar("FOO", rename="FOOBAR", default="BAZ") assert config["FOOBAR"] == "BAR" del os.environ["FOO"] assert config["FOOBAR"] == "BAZ" with pytest.raises(KeyError): config["FOO"] # should not be part of config
def test_env_var_can_be_renamed(self): try: del os.environ["FOO_DEBUG"] except KeyError: pass os.environ["FOO_DEBUG"] = "true" config = Config() config.from_envar("FOO_DEBUG", rename="DEBUG") assert config["DEBUG"] is True
def test_user_can_change_value_manually_but_env_value_takes_precedence(self): os.environ["FOO"] = "expected" config = Config() config["FOO"] = "not expected" config.from_envar("FOO") assert config["FOO"] == "expected"
def test_from_envar_that_doesnt_exist_but_exists_in_config_object(self): config = Config() config["BAR"] = "BAZ" config.from_envar("BAR") assert "BAR" in config assert config["BAR"] == "BAZ"
def test_from_env_var_that_equals_not_found(self): config = Config() os.environ["FOO"] = "not found" config.from_envar("FOO") assert config["FOO"] == "not found"
# -*- coding: utf-8 -*- # alternatively, you can load config values from a .env file path # note: you can choose which file is more important, .env or from object # by choosing which order to load the files in from pathlib import Path from configureme import Config from .config import DevConfig # create your config object to be used in your application config = Config() # load your configuration from a default object config.from_object(DevConfig) path = Path("examples/basic") / ".env" config.from_dotenv(str(path)) # you can also watch environment variables # note: environment variables will always take precedence # over any other value config.from_envar("TESTING")