|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from stackone_ai.models import ( |
| 6 | + ExecuteConfig, |
| 7 | + StackOneTool, |
| 8 | + ToolParameters, |
| 9 | + Tools, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture |
| 14 | +def mock_tool() -> StackOneTool: |
| 15 | + """Create a mock tool for testing""" |
| 16 | + return StackOneTool( |
| 17 | + description="Test HRIS tool for getting employee data", |
| 18 | + parameters=ToolParameters( |
| 19 | + type="object", |
| 20 | + properties={ |
| 21 | + "id": {"type": "string", "description": "Employee ID"}, |
| 22 | + "include_personal": {"type": "boolean", "description": "Include personal information"}, |
| 23 | + }, |
| 24 | + ), |
| 25 | + _execute_config=ExecuteConfig( |
| 26 | + headers={}, |
| 27 | + method="GET", |
| 28 | + url="https://api.stackone.com/unified/hris/employees/{id}", |
| 29 | + name="hris_get_employee", |
| 30 | + parameter_locations={"id": "path"}, |
| 31 | + ), |
| 32 | + _api_key="test_key", |
| 33 | + _account_id="test_account", |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def tools_collection(mock_tool: StackOneTool) -> Tools: |
| 39 | + """Create a Tools collection with mock tools""" |
| 40 | + return Tools([mock_tool]) |
| 41 | + |
| 42 | + |
| 43 | +class TestAgnoIntegration: |
| 44 | + """Test Agno integration functionality""" |
| 45 | + |
| 46 | + def test_to_agno_without_agno_installed(self, mock_tool: StackOneTool) -> None: |
| 47 | + """Test that proper error is raised when Agno is not installed""" |
| 48 | + with patch.dict('sys.modules', {'agno': None, 'agno.tools': None}): |
| 49 | + with pytest.raises(ImportError) as exc_info: |
| 50 | + mock_tool.to_agno() |
| 51 | + |
| 52 | + assert "Agno is not installed" in str(exc_info.value) |
| 53 | + assert "pip install agno>=1.7.0" in str(exc_info.value) |
| 54 | + |
| 55 | + def test_to_agno_with_mocked_agno(self, mock_tool: StackOneTool) -> None: |
| 56 | + """Test Agno conversion with mocked Agno classes""" |
| 57 | + # Mock the Agno Tool class |
| 58 | + mock_agno_base_tool = MagicMock() |
| 59 | + mock_agno_module = MagicMock() |
| 60 | + mock_agno_module.Tool = mock_agno_base_tool |
| 61 | + |
| 62 | + with patch.dict('sys.modules', {'agno.tools': mock_agno_module}): |
| 63 | + agno_tool = mock_tool.to_agno() |
| 64 | + |
| 65 | + # Verify an Agno tool instance was created |
| 66 | + assert agno_tool is not None |
| 67 | + |
| 68 | + def test_to_agno_tool_execution(self, mock_tool: StackOneTool) -> None: |
| 69 | + """Test that the Agno tool can execute the underlying StackOne tool""" |
| 70 | + mock_agno_base_tool = MagicMock() |
| 71 | + mock_agno_module = MagicMock() |
| 72 | + mock_agno_module.Tool = mock_agno_base_tool |
| 73 | + |
| 74 | + with patch.dict('sys.modules', {'agno.tools': mock_agno_module}): |
| 75 | + agno_tool = mock_tool.to_agno() |
| 76 | + |
| 77 | + # Verify the tool was created (basic functionality test) |
| 78 | + assert agno_tool is not None |
| 79 | + assert hasattr(agno_tool, 'run') |
| 80 | + |
| 81 | + def test_tools_to_agno(self, tools_collection: Tools) -> None: |
| 82 | + """Test converting Tools collection to Agno format""" |
| 83 | + mock_agno_base_tool = MagicMock() |
| 84 | + mock_agno_module = MagicMock() |
| 85 | + mock_agno_module.Tool = mock_agno_base_tool |
| 86 | + |
| 87 | + with patch.dict('sys.modules', {'agno.tools': mock_agno_module}): |
| 88 | + agno_tools = tools_collection.to_agno() |
| 89 | + |
| 90 | + # Verify we got the expected number of tools |
| 91 | + assert len(agno_tools) == 1 |
| 92 | + assert agno_tools[0] is not None |
| 93 | + |
| 94 | + def test_tools_to_agno_multiple_tools(self) -> None: |
| 95 | + """Test converting multiple tools to Agno format""" |
| 96 | + # Create multiple mock tools |
| 97 | + tool1 = StackOneTool( |
| 98 | + description="Test tool 1", |
| 99 | + parameters=ToolParameters(type="object", properties={"id": {"type": "string"}}), |
| 100 | + _execute_config=ExecuteConfig( |
| 101 | + headers={}, method="GET", url="https://api.example.com/test1/{id}", name="test_tool_1" |
| 102 | + ), |
| 103 | + _api_key="test_key", |
| 104 | + ) |
| 105 | + tool2 = StackOneTool( |
| 106 | + description="Test tool 2", |
| 107 | + parameters=ToolParameters(type="object", properties={"name": {"type": "string"}}), |
| 108 | + _execute_config=ExecuteConfig( |
| 109 | + headers={}, method="POST", url="https://api.example.com/test2", name="test_tool_2" |
| 110 | + ), |
| 111 | + _api_key="test_key", |
| 112 | + ) |
| 113 | + |
| 114 | + tools = Tools([tool1, tool2]) |
| 115 | + |
| 116 | + mock_agno_base_tool = MagicMock() |
| 117 | + mock_agno_module = MagicMock() |
| 118 | + mock_agno_module.Tool = mock_agno_base_tool |
| 119 | + |
| 120 | + with patch.dict('sys.modules', {'agno.tools': mock_agno_module}): |
| 121 | + agno_tools = tools.to_agno() |
| 122 | + |
| 123 | + assert len(agno_tools) == 2 |
| 124 | + assert all(tool is not None for tool in agno_tools) |
| 125 | + |
| 126 | + def test_agno_tool_preserves_metadata(self, mock_tool: StackOneTool) -> None: |
| 127 | + """Test that Agno tool conversion preserves important metadata""" |
| 128 | + mock_agno_base_tool = MagicMock() |
| 129 | + mock_agno_module = MagicMock() |
| 130 | + mock_agno_module.Tool = mock_agno_base_tool |
| 131 | + |
| 132 | + with patch.dict('sys.modules', {'agno.tools': mock_agno_module}): |
| 133 | + agno_tool = mock_tool.to_agno() |
| 134 | + |
| 135 | + # Verify the tool was created with expected attributes |
| 136 | + assert agno_tool is not None |
| 137 | + # For real integration, name and description would be set by the Agno base class |
| 138 | + assert hasattr(agno_tool, 'name') |
| 139 | + assert hasattr(agno_tool, 'description') |
| 140 | + |
| 141 | + |
| 142 | +class TestAgnoIntegrationErrors: |
| 143 | + """Test error handling in Agno integration""" |
| 144 | + |
| 145 | + def test_agno_import_error_message(self, mock_tool: StackOneTool) -> None: |
| 146 | + """Test that ImportError contains helpful installation instructions""" |
| 147 | + with patch.dict('sys.modules', {'agno': None, 'agno.tools': None}): |
| 148 | + with pytest.raises(ImportError) as exc_info: |
| 149 | + mock_tool.to_agno() |
| 150 | + |
| 151 | + error_msg = str(exc_info.value) |
| 152 | + assert "Agno is not installed" in error_msg |
| 153 | + assert "pip install agno>=1.7.0" in error_msg |
| 154 | + assert "requirements" in error_msg |
| 155 | + |
| 156 | + def test_tools_to_agno_with_failed_conversion(self) -> None: |
| 157 | + """Test Tools.to_agno() when individual tool conversion fails""" |
| 158 | + mock_tool = MagicMock() |
| 159 | + mock_tool.to_agno.side_effect = ImportError("Agno not available") |
| 160 | + |
| 161 | + tools = Tools([mock_tool]) |
| 162 | + |
| 163 | + with pytest.raises(ImportError): |
| 164 | + tools.to_agno() |
0 commit comments