def test_konfik(tmp_path, toml_str): """Unit test different parts of the Konfik class.""" test_toml_real_path = make_config_path(tmp_path, toml_str, "toml") test_toml_fake_path = "some/fake/path/config.toml" # Test if konfik raises `TypeError` when an argument is missing. with pytest.raises(TypeError): konfik = Konfik() # Test if konfik raises `MissingConfigError` when the config is missing. with pytest.raises(MissingConfigError): konfik = Konfik(config_path=test_toml_fake_path) konfik = Konfik(config_path=test_toml_real_path) # Test if a nested key can be resolved by `get_by_path` (used in the CLI). assert konfik.get_by_path({"hello": {"world": 2}}, ["hello", "world"]) == 2 # Test if konfik can find the file path. assert konfik._config_path == test_toml_real_path # Check raw config type. assert isinstance(konfik._config_raw, dict) is True # Check transformed config type. assert isinstance(konfik.config, DotMap) # Test if konfik can parse file extension from path. assert konfik._config_ext == "toml"
def test_konfik_env(tmp_path, dotenv_str): """Test the Konfik class for dotenv file.""" test_env_path = make_config_path(tmp_path, dotenv_str, "env") # Load toml from the test toml path. konfik = Konfik(config_path=test_env_path) # Make sure the serializer works. konfik.show_config() # Test variable access with dot notation. config = konfik.config assert config.TITLE == "DOTENV_EXAMPLE" assert config.NAME == "TOM" assert config.DOB == "1994-03-24T07:32:00-08:00" assert config.SERVER == "192.168.1.1" assert config.PORT == "8001" assert config.CONNECTION_MAX == "5000" assert config.ENABLED == "True" assert config.IP == "10.0.0.1" assert config.DC == "eqdc10" with pytest.raises(MissingVariableError): config.fakekey
def test_konfik_json(tmp_path, json_str, capsys): """Test the Konfik class for json.""" test_json_path = make_config_path(tmp_path, json_str, "json") # Load toml from the test toml path. konfik = Konfik(config_path=test_json_path) # Make sure show_config works. konfik.show_config() # Make sure show_config_literal works. konfik.show_config_literal() # Make sure show_var works. konfik.show_config_var("title") out, err = capsys.readouterr() assert err == "" assert "JSON Example" in out # Test variable access with dot notation. config = konfik.config assert config.title == "JSON Example" assert config.owner.name == "Tom Preston-Werner" assert config.database == { "server": "192.168.1.1", "ports": [8001, 8001, 8002], "connection_max": 5000, "enabled": True, } assert config.database.server == "192.168.1.1" assert config.database.ports == [8001, 8001, 8002] assert config.database.connection_max == 5000 assert config.database.enabled is True assert config.servers.alpha == {"ip": "10.0.0.1", "dc": "eqdc10"} assert config.servers.beta.dc == "eqdc10" assert config.clients.data == [["gamma", "delta"], [1, 2]] with pytest.raises(MissingVariableError): config.fakekey
from pathlib import Path from konfik import Konfik # Define the config paths BASE_DIR = Path(__file__).parent CONFIG_PATH_ENV = BASE_DIR / "config.env" # Initialize the Konfik class konfik = Konfik(config_path=CONFIG_PATH_ENV) # Serialize and print the dotenv config file konfik.show_config() # Access the variables in the config files via dot notation config = konfik.config # Access and print the variables in env file print(config.TITLE) print(config.NAME) print(config.DOB) print(config.SERVER) print(config.PORT)
from pathlib import Path from konfik import Konfik # Env file directory BASE_DIR = Path(__file__).resolve(strict=True).parent.parent ENV_FILE = BASE_DIR / ".env" # Read .env konfik = Konfik(ENV_FILE) config = konfik.config
from pathlib import Path from konfik import Konfik # Define the config path BASE_DIR = Path(__file__).parent CONFIG_PATH_YAML = BASE_DIR / "config.yaml" # Initialize the konfik class konfik = Konfik(config_path=CONFIG_PATH_YAML) # Serialize and print the confile file konfik.show_config() # Get the configuration dictionary from the konfik class config = konfik.config # Access and print the variables print(config.title) print(config.owner) print(config.owner.dob) print(config.database.ports) print(config.servers.alpha.ip) print(config.clients)
from pathlib import Path from konfik import Konfik # Define the config path BASE_DIR = Path(__file__).parent CONFIG_PATH_JSON = BASE_DIR / "config.json" # Initialize the Konfik class konfik = Konfik(config_path=CONFIG_PATH_JSON) # Serialize and print the confile file konfik.show_config() # Get the configuration dictionary from the konfik class config = konfik.config # Access and print the variables in toml file print(config.title) print(config.owner) print(config.owner.dob) print(config.database.ports) print(config.servers.alpha.ip) print(config.clients)