94 lines
2.9 KiB
Python
94 lines
2.9 KiB
Python
"""Tests for ShellGenius configuration module."""
|
|
|
|
import os
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
from shellgenius.config import Config, get_config
|
|
|
|
|
|
class TestConfig:
|
|
"""Test cases for configuration."""
|
|
|
|
def test_default_config(self):
|
|
"""Test default configuration values."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
config_path = Path(tmpdir) / "nonexistent.yaml"
|
|
config = Config(str(config_path))
|
|
|
|
assert config.ollama_host == "localhost:11434"
|
|
assert config.ollama_model == "codellama"
|
|
assert config.safety_level == "moderate"
|
|
|
|
def test_custom_config(self):
|
|
"""Test loading custom configuration."""
|
|
custom_config = {
|
|
"ollama": {
|
|
"host": "custom:9999",
|
|
"model": "mistral",
|
|
"timeout": 60,
|
|
},
|
|
"safety": {
|
|
"level": "strict",
|
|
},
|
|
}
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
config_path = Path(tmpdir) / "config.yaml"
|
|
with open(config_path, "w") as f:
|
|
yaml.dump(custom_config, f)
|
|
|
|
config = Config(str(config_path))
|
|
|
|
assert config.ollama_host == "custom:9999"
|
|
assert config.ollama_model == "mistral"
|
|
assert config.safety_level == "strict"
|
|
|
|
def test_env_override(self):
|
|
"""Test environment variable overrides."""
|
|
os.environ["OLLAMA_HOST"] = "env-host:1234"
|
|
os.environ["OLLAMA_MODEL"] = "env-model"
|
|
|
|
try:
|
|
config = Config()
|
|
assert config.ollama_host == "env-host:1234"
|
|
assert config.ollama_model == "env-model"
|
|
finally:
|
|
del os.environ["OLLAMA_HOST"]
|
|
del os.environ["OLLAMA_MODEL"]
|
|
|
|
def test_get_nested_value(self):
|
|
"""Test getting nested configuration values."""
|
|
config = Config()
|
|
timeout = config.get("ollama.timeout")
|
|
assert timeout == 120 or isinstance(timeout, (int, type(None)))
|
|
|
|
def test_get_missing_key(self):
|
|
"""Test getting missing key returns default."""
|
|
config = Config()
|
|
value = config.get("nonexistent.key", "default")
|
|
assert value == "default"
|
|
|
|
def test_merge_configs(self):
|
|
"""Test merging user config with defaults."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
config_path = Path(tmpdir) / "config.yaml"
|
|
with open(config_path, "w") as f:
|
|
yaml.dump({"ollama": {"model": "llama2"}}, f)
|
|
|
|
config = Config(str(config_path))
|
|
|
|
assert config.ollama_model == "llama2"
|
|
assert config.ollama_host == "localhost:11434"
|
|
|
|
|
|
class TestGetConfig:
|
|
"""Test get_config convenience function."""
|
|
|
|
def test_get_config_singleton(self):
|
|
"""Test get_config returns Config instance."""
|
|
config = get_config()
|
|
assert isinstance(config, Config)
|