import pytest import importlib import yaml @pytest.fixture def tmp_config_file(tmp_path): """Creates a temporary config.yaml file and returns its path.""" config_content = { "application": {"project_name": "Test Project from YAML"}, "llm_providers": {"deepseek_model_name": "deepseek-from-yaml"} } config_path = tmp_path / "test_config.yaml" with open(config_path, 'w') as f: yaml.dump(config_content, f) return str(config_path) def test_yaml_overrides_env_var(monkeypatch, tmp_config_file): """ Tests that a value from the YAML file has priority over an environment variable. """ # Arrange: Set BOTH a YAML value and an environment variable monkeypatch.setenv("CONFIG_PATH", tmp_config_file) monkeypatch.setenv("DEEPSEEK_MODEL_NAME", "deepseek-from-env") # This should be ignored # Act from app import config importlib.reload(config) # Assert: The value from the YAML file should be used assert config.settings.DEEPSEEK_MODEL_NAME == "deepseek-from-yaml" def test_env_var_overrides_default(monkeypatch): """ Tests that an environment variable overrides the hardcoded default when the YAML file is missing or doesn't contain the key. """ # Arrange: Point to a non-existent file and set an environment variable monkeypatch.setenv("CONFIG_PATH", "/path/that/does/not/exist.yaml") monkeypatch.setenv("DEEPSEEK_MODEL_NAME", "deepseek-from-env") # Act from app import config importlib.reload(config) # Assert: The value from the environment variable should be used assert config.settings.DEEPSEEK_MODEL_NAME == "deepseek-from-env" def test_hardcoded_default_is_used_last(monkeypatch): """ Tests that the hardcoded default is used when the YAML is missing and the environment variable is not set. """ # Arrange monkeypatch.setenv("CONFIG_PATH", "/path/that/does/not/exist.yaml") monkeypatch.delenv("DEEPSEEK_MODEL_NAME", raising=False) # Act from app import config importlib.reload(config) # Assert: The hardcoded default value should be used assert config.settings.DEEPSEEK_MODEL_NAME == "deepseek-chat"