Files
ai_novel/tests/test_config.py

92 lines
2.7 KiB
Python

"""Tests for configuration management."""
import pytest
import tempfile
import yaml
from pathlib import Path
from ai_novel.config import Config
def test_default_config():
"""Test default configuration loading."""
config = Config()
assert config.get("project_dir") == "."
assert config.get("novelist_llm.type") == "openai"
assert config.get("novelist_llm.model") == "gpt-3.5-turbo"
assert config.get("summarizer_llm.type") == "openai"
def test_config_from_file():
"""Test loading configuration from file."""
test_config = {
"project_dir": "test_novel",
"novelist_llm": {
"type": "ollama",
"model": "llama3.1",
"temperature": 0.8
}
}
with tempfile.NamedTemporaryFile(mode='w', suffix='.yaml', delete=False) as f:
yaml.dump(test_config, f)
config_path = f.name
try:
config = Config(config_path)
assert config.get("project_dir") == "test_novel"
assert config.get("novelist_llm.type") == "ollama"
assert config.get("novelist_llm.model") == "llama3.1"
assert config.get("novelist_llm.temperature") == 0.8
# Should still have default values for unspecified keys
assert config.get("summarizer_llm.type") == "openai"
finally:
Path(config_path).unlink()
def test_config_get_with_default():
"""Test getting configuration values with defaults."""
config = Config()
assert config.get("nonexistent.key", "default") == "default"
assert config.get("novelist_llm.nonexistent", "default") == "default"
def test_create_example_config():
"""Test creating example configuration file."""
with tempfile.NamedTemporaryFile(suffix='.yaml', delete=False) as f:
config_path = f.name
try:
Config.create_example_config(config_path)
assert Path(config_path).exists()
with open(config_path, 'r') as f:
example_config = yaml.safe_load(f)
assert "project_dir" in example_config
assert "novelist_llm" in example_config
assert "summarizer_llm" in example_config
finally:
Path(config_path).unlink()
def test_config_save_to_file():
"""Test saving configuration to file."""
config = Config()
with tempfile.NamedTemporaryFile(suffix='.yaml', delete=False) as f:
config_path = f.name
try:
config.save_to_file(config_path)
assert Path(config_path).exists()
# Load and verify
new_config = Config(config_path)
assert new_config.get("novelist_llm.type") == config.get("novelist_llm.type")
assert new_config.get("novelist_llm.model") == config.get("novelist_llm.model")
finally:
Path(config_path).unlink()