Spaces:
Running
Running
File size: 2,537 Bytes
069f0a0 4732667 069f0a0 4732667 069f0a0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
"""Unit tests for configuration loading."""
import os
from unittest.mock import patch
import pytest
from pydantic import ValidationError
from src.utils.config import Settings
from src.utils.exceptions import ConfigurationError
class TestSettings:
"""Tests for Settings class."""
def test_default_max_iterations(self):
"""Settings should have default max_iterations of 10."""
with patch.dict(os.environ, {}, clear=True):
settings = Settings()
assert settings.max_iterations == 10 # noqa: PLR2004
def test_max_iterations_from_env(self):
"""Settings should read MAX_ITERATIONS from env."""
with patch.dict(os.environ, {"MAX_ITERATIONS": "25"}):
settings = Settings()
assert settings.max_iterations == 25 # noqa: PLR2004
def test_invalid_max_iterations_raises(self):
"""Settings should reject invalid max_iterations."""
with patch.dict(os.environ, {"MAX_ITERATIONS": "100"}):
with pytest.raises(ValidationError):
Settings() # 100 > 50 (max)
def test_get_api_key_openai(self):
"""get_api_key should return OpenAI key when provider is openai."""
with patch.dict(os.environ, {"LLM_PROVIDER": "openai", "OPENAI_API_KEY": "sk-test-key"}):
settings = Settings()
assert settings.get_api_key() == "sk-test-key"
def test_get_api_key_openai_missing_raises(self):
"""get_api_key should raise ConfigurationError when OpenAI key is not set."""
with patch.dict(os.environ, {"LLM_PROVIDER": "openai"}, clear=True):
settings = Settings(_env_file=None)
with pytest.raises(ConfigurationError, match="OPENAI_API_KEY not set"):
settings.get_api_key()
def test_get_api_key_anthropic(self):
"""get_api_key should return Anthropic key when provider is anthropic."""
with patch.dict(
os.environ, {"LLM_PROVIDER": "anthropic", "ANTHROPIC_API_KEY": "sk-ant-test-key"}
):
settings = Settings()
assert settings.get_api_key() == "sk-ant-test-key"
def test_get_api_key_anthropic_missing_raises(self):
"""get_api_key should raise ConfigurationError when Anthropic key is not set."""
with patch.dict(os.environ, {"LLM_PROVIDER": "anthropic"}, clear=True):
settings = Settings(_env_file=None)
with pytest.raises(ConfigurationError, match="ANTHROPIC_API_KEY not set"):
settings.get_api_key()
|