Add test suite (68 tests)
This commit is contained in:
133
tests/test_config.py
Normal file
133
tests/test_config.py
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
"""Tests for configuration module."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from codexchange.config import (
|
||||||
|
ConfigSettings,
|
||||||
|
get_config_path,
|
||||||
|
load_config,
|
||||||
|
save_config,
|
||||||
|
get_config,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestConfigSettings:
|
||||||
|
"""Tests for ConfigSettings model."""
|
||||||
|
|
||||||
|
def test_default_values(self):
|
||||||
|
"""Test that ConfigSettings has correct default values."""
|
||||||
|
config = ConfigSettings()
|
||||||
|
assert config.ollama_host == "http://localhost:11434"
|
||||||
|
assert config.default_model == "codellama"
|
||||||
|
assert config.timeout == 300
|
||||||
|
assert config.verbose is False
|
||||||
|
|
||||||
|
def test_custom_values(self):
|
||||||
|
"""Test that ConfigSettings accepts custom values."""
|
||||||
|
config = ConfigSettings(
|
||||||
|
ollama_host="http://ollama:11434",
|
||||||
|
default_model="llama2",
|
||||||
|
timeout=600,
|
||||||
|
verbose=True
|
||||||
|
)
|
||||||
|
assert config.ollama_host == "http://ollama:11434"
|
||||||
|
assert config.default_model == "llama2"
|
||||||
|
assert config.timeout == 600
|
||||||
|
assert config.verbose is True
|
||||||
|
|
||||||
|
|
||||||
|
class TestConfigPath:
|
||||||
|
"""Tests for configuration file path."""
|
||||||
|
|
||||||
|
def test_get_config_path(self):
|
||||||
|
"""Test that config path is in home directory."""
|
||||||
|
config_path = get_config_path()
|
||||||
|
assert config_path.name == ".codexchange.yaml"
|
||||||
|
assert config_path.parent == Path.home()
|
||||||
|
|
||||||
|
|
||||||
|
class TestLoadConfig:
|
||||||
|
"""Tests for loading configuration."""
|
||||||
|
|
||||||
|
def test_load_default_config(self):
|
||||||
|
"""Test loading config when file doesn't exist."""
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
config_path = Path(tmpdir) / "nonexistent.yaml"
|
||||||
|
config = load_config(config_path)
|
||||||
|
|
||||||
|
assert config.ollama_host == "http://localhost:11434"
|
||||||
|
assert config.default_model == "codellama"
|
||||||
|
|
||||||
|
def test_load_config_from_file(self):
|
||||||
|
"""Test loading config from YAML file."""
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
config_path = Path(tmpdir) / ".codexchange.yaml"
|
||||||
|
|
||||||
|
yaml_content = """
|
||||||
|
ollama_host: http://custom:11434
|
||||||
|
default_model: mistral
|
||||||
|
timeout: 600
|
||||||
|
verbose: true
|
||||||
|
"""
|
||||||
|
with open(config_path, "w") as f:
|
||||||
|
f.write(yaml_content)
|
||||||
|
|
||||||
|
config = load_config(config_path)
|
||||||
|
|
||||||
|
assert config.ollama_host == "http://custom:11434"
|
||||||
|
assert config.default_model == "mistral"
|
||||||
|
assert config.timeout == 600
|
||||||
|
assert config.verbose is True
|
||||||
|
|
||||||
|
|
||||||
|
class TestSaveConfig:
|
||||||
|
"""Tests for saving configuration."""
|
||||||
|
|
||||||
|
def test_save_config(self):
|
||||||
|
"""Test saving config to YAML file."""
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
config_path = Path(tmpdir) / ".codexchange.yaml"
|
||||||
|
config = ConfigSettings(
|
||||||
|
ollama_host="http://test:11434",
|
||||||
|
default_model="testmodel",
|
||||||
|
timeout=120
|
||||||
|
)
|
||||||
|
|
||||||
|
save_config(config, config_path)
|
||||||
|
|
||||||
|
assert config_path.exists()
|
||||||
|
|
||||||
|
with open(config_path, "r") as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
|
assert "http://test:11434" in content
|
||||||
|
assert "testmodel" in content
|
||||||
|
assert "120" in content
|
||||||
|
|
||||||
|
|
||||||
|
class TestGetConfig:
|
||||||
|
"""Tests for getting configuration with env overrides."""
|
||||||
|
|
||||||
|
def test_environment_override(self, monkeypatch):
|
||||||
|
"""Test that environment variables override config."""
|
||||||
|
monkeypatch.setenv("CODEXCHANGE_OLLAMA_HOST", "http://env-host:11434")
|
||||||
|
monkeypatch.setenv("CODEXCHANGE_DEFAULT_MODEL", "env-model")
|
||||||
|
monkeypatch.setenv("CODEXCHANGE_TIMEOUT", "900")
|
||||||
|
|
||||||
|
config = get_config()
|
||||||
|
|
||||||
|
assert config.ollama_host == "http://env-host:11434"
|
||||||
|
assert config.default_model == "env-model"
|
||||||
|
assert config.timeout == 900
|
||||||
|
|
||||||
|
def test_invalid_timeout_env(self, monkeypatch):
|
||||||
|
"""Test handling of invalid timeout environment variable."""
|
||||||
|
monkeypatch.setenv("CODEXCHANGE_TIMEOUT", "invalid")
|
||||||
|
|
||||||
|
config = get_config()
|
||||||
|
|
||||||
|
assert config.timeout == 300
|
||||||
Reference in New Issue
Block a user