Files
temp/tests/unit/core/test_exceptions.py

126 lines
4.3 KiB
Python

"""
Unit tests for core/exceptions.py
Tests all custom exception classes.
"""
import pytest
from cleveragents.core.exceptions import (
CleverAgentsException,
ConfigurationError,
UnsafeConfigurationError,
AgentCreationError,
RoutingError,
TemplateError,
ExecutionError,
ApplicationError,
StreamRoutingError,
)
class TestExceptions:
"""Test suite for all exception classes."""
def test_clever_agents_exception(self):
"""Test CleverAgentsException base class."""
exc = CleverAgentsException("test message")
assert str(exc) == "test message"
assert isinstance(exc, Exception)
def test_configuration_error(self):
"""Test ConfigurationError exception."""
exc = ConfigurationError("config error")
assert str(exc) == "config error"
assert isinstance(exc, CleverAgentsException)
assert isinstance(exc, Exception)
def test_unsafe_configuration_error(self):
"""Test UnsafeConfigurationError exception."""
exc = UnsafeConfigurationError("unsafe config")
assert str(exc) == "unsafe config"
assert isinstance(exc, ConfigurationError)
assert isinstance(exc, CleverAgentsException)
def test_agent_creation_error(self):
"""Test AgentCreationError exception."""
exc = AgentCreationError("agent creation failed")
assert str(exc) == "agent creation failed"
assert isinstance(exc, CleverAgentsException)
def test_routing_error(self):
"""Test RoutingError exception."""
exc = RoutingError("routing failed")
assert str(exc) == "routing failed"
assert isinstance(exc, CleverAgentsException)
def test_template_error(self):
"""Test TemplateError exception."""
exc = TemplateError("template error")
assert str(exc) == "template error"
assert isinstance(exc, CleverAgentsException)
def test_execution_error(self):
"""Test ExecutionError exception."""
exc = ExecutionError("execution failed")
assert str(exc) == "execution failed"
assert isinstance(exc, CleverAgentsException)
def test_application_error(self):
"""Test ApplicationError exception."""
exc = ApplicationError("application error")
assert str(exc) == "application error"
assert isinstance(exc, CleverAgentsException)
def test_stream_routing_error(self):
"""Test StreamRoutingError exception."""
exc = StreamRoutingError("stream routing error")
assert str(exc) == "stream routing error"
assert isinstance(exc, CleverAgentsException)
def test_exceptions_can_be_raised_and_caught(self):
"""Test that exceptions can be raised and caught."""
with pytest.raises(ConfigurationError) as exc_info:
raise ConfigurationError("test error")
assert "test error" in str(exc_info.value)
def test_inheritance_hierarchy(self):
"""Test exception inheritance hierarchy."""
# UnsafeConfigurationError should be caught as ConfigurationError
with pytest.raises(ConfigurationError):
raise UnsafeConfigurationError("unsafe")
# All should be caught as CleverAgentsException
with pytest.raises(CleverAgentsException):
raise RoutingError("routing")
with pytest.raises(CleverAgentsException):
raise TemplateError("template")
def test_exceptions_with_no_message(self):
"""Test that exceptions can be created without messages."""
exc = CleverAgentsException()
assert isinstance(exc, Exception)
exc = ConfigurationError()
assert isinstance(exc, CleverAgentsException)
def test_exceptions_with_complex_messages(self):
"""Test exceptions with complex error messages."""
complex_msg = "Error in agent 'test_agent': failed to load model 'gpt-4' with config {'temp': 0.7}"
exc = AgentCreationError(complex_msg)
assert complex_msg in str(exc)
def test_unsafe_configuration_error_docstring(self):
"""Test that UnsafeConfigurationError has appropriate documentation."""
assert UnsafeConfigurationError.__doc__ is not None
assert "unsafe" in UnsafeConfigurationError.__doc__.lower()
if __name__ == "__main__":
pytest.main([__file__, "-v"])