Spaces:
Sleeping
Sleeping
| """Unit tests for tools module.""" | |
| import pytest | |
| from unittest.mock import AsyncMock, patch, MagicMock | |
| from src.tools.base import Tool, ToolParameter, ToolResult, ToolRegistry | |
| from src.tools.calculator import CalculatorTool | |
| from src.tools.datetime_tool import DateTimeTool | |
| class TestToolBase: | |
| """Tests for base tool classes.""" | |
| def test_tool_parameter_creation(self): | |
| """Test ToolParameter creation.""" | |
| param = ToolParameter( | |
| name="query", | |
| type="string", | |
| description="Search query", | |
| required=True, | |
| ) | |
| assert param.name == "query" | |
| assert param.type == "string" | |
| assert param.required is True | |
| def test_tool_result_success(self): | |
| """Test successful ToolResult.""" | |
| result = ToolResult( | |
| success=True, | |
| data={"key": "value"}, | |
| error=None, | |
| ) | |
| assert result.success is True | |
| assert result.data == {"key": "value"} | |
| assert result.error is None | |
| def test_tool_result_failure(self): | |
| """Test failed ToolResult.""" | |
| result = ToolResult( | |
| success=False, | |
| data=None, | |
| error="Something went wrong", | |
| ) | |
| assert result.success is False | |
| assert result.error == "Something went wrong" | |
| class TestToolRegistry: | |
| """Tests for ToolRegistry.""" | |
| def test_register_tool(self): | |
| """Test tool registration.""" | |
| registry = ToolRegistry() | |
| class MockTool(Tool): | |
| name = "mock_tool" | |
| description = "A mock tool" | |
| parameters = [] | |
| async def execute(self, **kwargs): | |
| return ToolResult(success=True, data="mock") | |
| tool = MockTool() | |
| registry.register(tool) | |
| assert "mock_tool" in registry.list_tools() | |
| assert registry.get("mock_tool") == tool | |
| def test_unregister_tool(self): | |
| """Test tool unregistration.""" | |
| registry = ToolRegistry() | |
| class MockTool(Tool): | |
| name = "mock_tool" | |
| description = "A mock tool" | |
| parameters = [] | |
| async def execute(self, **kwargs): | |
| return ToolResult(success=True, data="mock") | |
| tool = MockTool() | |
| registry.register(tool) | |
| registry.unregister("mock_tool") | |
| assert "mock_tool" not in registry.list_tools() | |
| class TestCalculatorTool: | |
| """Tests for CalculatorTool.""" | |
| async def test_simple_addition(self): | |
| """Test simple addition.""" | |
| calc = CalculatorTool() | |
| result = await calc.execute(expression="2 + 2") | |
| assert result.success is True | |
| assert result.data == 4 | |
| async def test_complex_expression(self): | |
| """Test complex mathematical expression.""" | |
| calc = CalculatorTool() | |
| result = await calc.execute(expression="(10 + 5) * 2") | |
| assert result.success is True | |
| assert result.data == 30 | |
| async def test_invalid_expression(self): | |
| """Test invalid expression handling.""" | |
| calc = CalculatorTool() | |
| result = await calc.execute(expression="import os") | |
| assert result.success is False | |
| assert result.error is not None | |
| class TestDateTimeTool: | |
| """Tests for DateTimeTool.""" | |
| async def test_current_time(self): | |
| """Test getting current time.""" | |
| dt_tool = DateTimeTool() | |
| result = await dt_tool.execute(action="now") | |
| assert result.success is True | |
| assert "datetime" in result.data | |
| async def test_format_date(self): | |
| """Test date formatting.""" | |
| dt_tool = DateTimeTool() | |
| result = await dt_tool.execute( | |
| action="format", | |
| date="2024-01-15", | |
| format="%B %d, %Y", | |
| ) | |
| assert result.success is True | |
| assert "January 15, 2024" in result.data["formatted"] | |