105 lines
2.9 KiB
Python
105 lines
2.9 KiB
Python
"""Tests for configuration loader."""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from src.config.config_loader import get_config, load_config
|
|
from src.core.exceptions import ConfigurationError
|
|
|
|
|
|
def test_load_config_success(temp_config_dir, monkeypatch):
|
|
"""Test successful config loading."""
|
|
from src.core import constants
|
|
original_path = constants.PATHS["config"]
|
|
constants.PATHS["config"] = temp_config_dir
|
|
|
|
config = load_config()
|
|
assert config is not None
|
|
assert "app" in config
|
|
assert config["app"]["name"] == "Test App"
|
|
|
|
constants.PATHS["config"] = original_path
|
|
|
|
|
|
def test_load_config_missing_file(temp_dir, monkeypatch):
|
|
"""Test config loading with missing file."""
|
|
from src.core import constants
|
|
original_path = constants.PATHS["config"]
|
|
constants.PATHS["config"] = temp_dir / "nonexistent"
|
|
|
|
with pytest.raises(ConfigurationError):
|
|
load_config()
|
|
|
|
constants.PATHS["config"] = original_path
|
|
|
|
|
|
def test_get_config_with_key(temp_config_dir, monkeypatch):
|
|
"""Test getting config value by key."""
|
|
from src.core import constants
|
|
original_path = constants.PATHS["config"]
|
|
constants.PATHS["config"] = temp_config_dir
|
|
|
|
value = get_config("app.name")
|
|
assert value == "Test App"
|
|
|
|
constants.PATHS["config"] = original_path
|
|
|
|
|
|
def test_get_config_with_default(temp_config_dir, monkeypatch):
|
|
"""Test getting config with default value."""
|
|
from src.core import constants
|
|
original_path = constants.PATHS["config"]
|
|
constants.PATHS["config"] = temp_config_dir
|
|
|
|
value = get_config("nonexistent.key", default="default_value")
|
|
assert value == "default_value"
|
|
|
|
constants.PATHS["config"] = original_path
|
|
|
|
|
|
def test_get_config_none(temp_config_dir, monkeypatch):
|
|
"""Test getting entire config."""
|
|
from src.core import constants
|
|
original_path = constants.PATHS["config"]
|
|
constants.PATHS["config"] = temp_config_dir
|
|
|
|
config = get_config()
|
|
assert isinstance(config, dict)
|
|
assert "app" in config
|
|
|
|
constants.PATHS["config"] = original_path
|
|
|
|
|
|
def test_env_var_substitution(temp_config_dir, monkeypatch):
|
|
"""Test environment variable substitution in config."""
|
|
from src.core import constants
|
|
original_path = constants.PATHS["config"]
|
|
constants.PATHS["config"] = temp_config_dir
|
|
|
|
# Set environment variable
|
|
os.environ["TEST_VAR"] = "test_value"
|
|
|
|
# Create config with env var
|
|
config_file = temp_config_dir / "config.yaml"
|
|
config_file.write_text(
|
|
"""
|
|
app:
|
|
name: "${TEST_VAR}"
|
|
version: "0.1.0"
|
|
"""
|
|
)
|
|
|
|
# Reset config cache
|
|
import src.config.config_loader as config_module
|
|
config_module._config = None
|
|
|
|
config = load_config()
|
|
assert config["app"]["name"] == "test_value"
|
|
|
|
# Cleanup
|
|
del os.environ["TEST_VAR"]
|
|
constants.PATHS["config"] = original_path
|
|
|