Spaces:
Running
Running
File size: 5,664 Bytes
9024ad9 2aa2b79 9024ad9 2aa2b79 |
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
"""
Tests for configuration management.
"""
import pytest
import os
from app.core.config import Settings, settings
class TestSettings:
"""Test configuration settings."""
def test_default_settings(self):
"""Test default configuration values."""
test_settings = Settings()
assert test_settings.ollama_model == "llama3.1:8b"
assert test_settings.ollama_host == "http://127.0.0.1:11434"
assert test_settings.ollama_timeout == 30
assert test_settings.server_host == "127.0.0.1"
assert test_settings.server_port == 8000
assert test_settings.log_level == "INFO"
assert test_settings.api_key_enabled is False
assert test_settings.rate_limit_enabled is False
assert test_settings.max_text_length == 32000
assert test_settings.max_tokens_default == 256
def test_environment_override(self, test_env_vars):
"""Test that environment variables override defaults."""
test_settings = Settings()
assert test_settings.ollama_model == "llama3.1:8b"
assert test_settings.ollama_host == "http://127.0.0.1:11434"
assert test_settings.ollama_timeout == 30
assert test_settings.server_host == "127.0.0.1"
assert test_settings.server_port == 8000
assert test_settings.log_level == "INFO"
def test_global_settings_instance(self):
"""Test that global settings instance exists."""
assert settings is not None
assert isinstance(settings, Settings)
def test_custom_environment_variables(self, monkeypatch):
"""Test custom environment variable values."""
monkeypatch.setenv("OLLAMA_MODEL", "custom-model:7b")
monkeypatch.setenv("OLLAMA_HOST", "http://custom-host:9999")
monkeypatch.setenv("OLLAMA_TIMEOUT", "60")
monkeypatch.setenv("SERVER_HOST", "0.0.0.0")
monkeypatch.setenv("SERVER_PORT", "9000")
monkeypatch.setenv("LOG_LEVEL", "DEBUG")
monkeypatch.setenv("API_KEY_ENABLED", "true")
monkeypatch.setenv("API_KEY", "test-secret-key")
monkeypatch.setenv("RATE_LIMIT_ENABLED", "true")
monkeypatch.setenv("RATE_LIMIT_REQUESTS", "100")
monkeypatch.setenv("RATE_LIMIT_WINDOW", "120")
monkeypatch.setenv("MAX_TEXT_LENGTH", "64000")
monkeypatch.setenv("MAX_TOKENS_DEFAULT", "512")
test_settings = Settings()
assert test_settings.ollama_model == "custom-model:7b"
assert test_settings.ollama_host == "http://custom-host:9999"
assert test_settings.ollama_timeout == 60
assert test_settings.server_host == "0.0.0.0"
assert test_settings.server_port == 9000
assert test_settings.log_level == "DEBUG"
assert test_settings.api_key_enabled is True
assert test_settings.api_key == "test-secret-key"
assert test_settings.rate_limit_enabled is True
assert test_settings.rate_limit_requests == 100
assert test_settings.rate_limit_window == 120
assert test_settings.max_text_length == 64000
assert test_settings.max_tokens_default == 512
def test_invalid_boolean_environment_variables(self, monkeypatch):
"""Test that invalid boolean values raise validation errors."""
monkeypatch.setenv("API_KEY_ENABLED", "invalid")
monkeypatch.setenv("RATE_LIMIT_ENABLED", "maybe")
with pytest.raises(Exception): # Pydantic validation error
Settings()
def test_invalid_integer_environment_variables(self, monkeypatch):
"""Test that invalid integer values raise validation errors."""
monkeypatch.setenv("OLLAMA_TIMEOUT", "invalid")
monkeypatch.setenv("SERVER_PORT", "not-a-number")
monkeypatch.setenv("MAX_TEXT_LENGTH", "abc")
with pytest.raises(Exception): # Pydantic validation error
Settings()
def test_negative_integer_environment_variables(self, monkeypatch):
"""Test that negative integer values raise validation errors."""
monkeypatch.setenv("OLLAMA_TIMEOUT", "-10")
monkeypatch.setenv("SERVER_PORT", "-1")
monkeypatch.setenv("MAX_TEXT_LENGTH", "-1000")
with pytest.raises(Exception): # Pydantic validation error
Settings()
def test_settings_validation(self):
"""Test that settings validation works correctly."""
test_settings = Settings()
# Test that all required attributes exist
assert hasattr(test_settings, 'ollama_model')
assert hasattr(test_settings, 'ollama_host')
assert hasattr(test_settings, 'ollama_timeout')
assert hasattr(test_settings, 'server_host')
assert hasattr(test_settings, 'server_port')
assert hasattr(test_settings, 'log_level')
assert hasattr(test_settings, 'api_key_enabled')
assert hasattr(test_settings, 'rate_limit_enabled')
assert hasattr(test_settings, 'max_text_length')
assert hasattr(test_settings, 'max_tokens_default')
def test_log_level_validation(self, monkeypatch):
"""Test that log level validation works."""
# Test valid log levels
for level in ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]:
monkeypatch.setenv("LOG_LEVEL", level)
test_settings = Settings()
assert test_settings.log_level == level
# Test invalid log level defaults to INFO
monkeypatch.setenv("LOG_LEVEL", "INVALID")
test_settings = Settings()
assert test_settings.log_level == "INFO"
|