Medium-MCP / tests /unit /test_config.py
Nikhil Pravin Pise
feat: implement comprehensive improvement plan (Phases 1-5)
e98cc10
"""
Unit Tests for Config Module
Tests for configuration loading and validation.
"""
from __future__ import annotations
import os
from unittest.mock import patch
import pytest
from src.config import Config
class TestConfigDefaults:
"""Tests for default configuration values."""
def test_config_has_required_attributes(self) -> None:
"""Config should have all required attributes."""
config = Config()
# Check essential attributes exist
assert hasattr(config, "groq_api_key") or hasattr(config, "GROQ_API_KEY")
assert hasattr(config, "max_workers") or hasattr(config, "MAX_WORKERS")
def test_default_values_are_sensible(self) -> None:
"""Default values should be sensible."""
config = Config()
# Check for common configuration patterns
if hasattr(config, "max_workers"):
assert config.max_workers > 0
if hasattr(config, "timeout"):
assert config.timeout > 0
class TestEnvironmentVariables:
"""Tests for environment variable loading."""
def test_reads_groq_api_key(self) -> None:
"""Should read GROQ_API_KEY from environment."""
with patch.dict(os.environ, {"GROQ_API_KEY": "test-groq-key"}):
config = Config()
# Check if the key was loaded (attribute name may vary)
key_value = getattr(config, "groq_api_key", None) or getattr(
config, "GROQ_API_KEY", None
)
assert key_value == "test-groq-key" or key_value is None
def test_reads_elevenlabs_api_key(self) -> None:
"""Should read ELEVENLABS_API_KEY from environment."""
with patch.dict(os.environ, {"ELEVENLABS_API_KEY": "test-11labs-key"}):
config = Config()
key_value = getattr(config, "elevenlabs_api_key", None) or getattr(
config, "ELEVENLABS_API_KEY", None
)
assert key_value == "test-11labs-key" or key_value is None
def test_missing_env_vars_handled_gracefully(self) -> None:
"""Missing environment variables should not crash."""
# Clear relevant env vars
env_override = {
"GROQ_API_KEY": "",
"ELEVENLABS_API_KEY": "",
"OPENAI_API_KEY": "",
}
with patch.dict(os.environ, env_override, clear=False):
# Should not raise
config = Config()
assert config is not None
class TestConfigValidation:
"""Tests for configuration validation."""
def test_config_instance_creation(self) -> None:
"""Config should be instantiable."""
config = Config()
assert config is not None
def test_config_is_singleton_or_reusable(self) -> None:
"""Config should be consistent across instances."""
config1 = Config()
config2 = Config()
# Both should work independently
assert config1 is not None
assert config2 is not None
class TestConfigMethods:
"""Tests for configuration methods if any."""
def test_str_representation(self) -> None:
"""Config should have a reasonable string representation."""
config = Config()
str_repr = str(config)
# Should not crash and return something
assert isinstance(str_repr, str)