Building AI agents, atomically
fix(tests) get tests passing & update requirements.txt
2dogz232f85cb499e
diff --git a/atomic-forge/tools/tavily_search/requirements.txt b/atomic-forge/tools/tavily_search/requirements.txtindex ee7b57c..e7586f3 100644--- a/atomic-forge/tools/tavily_search/requirements.txt+++ b/atomic-forge/tools/tavily_search/requirements.txt@@ -1,3 +1,4 @@atomic-agents>=1.0.0,<2.0.0pydantic>=2.8.2,<3.0.0-sympy>=1.12,<2.0.0\ No newline at end of file+sympy>=1.12,<2.0.0+aiohttp>=3.9.0,<4.0.0\ No newline at end of filediff --git a/atomic-forge/tools/tavily_search/tests/test_tavily_seach.py b/atomic-forge/tools/tavily_search/tests/test_tavily_seach.pyindex c76f8f1..89287f0 100644--- a/atomic-forge/tools/tavily_search/tests/test_tavily_seach.py+++ b/atomic-forge/tools/tavily_search/tests/test_tavily_seach.py@@ -2,7 +2,7 @@ import osimport sysimport pytestfrom unittest.mock import patch, AsyncMock, MagicMock-from aiohttp import ClientSession+from types import SimpleNamespacesys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))@@ -16,34 +16,45 @@ from tool.tavily_search import ( # noqa: E402@pytest.fixturedef mock_aiohttp_session():# Patch the ClientSession in the module where it's used- with patch("aiohttp.ClientSession") as mock_session_class:- mock_session = MagicMock(spec=ClientSession)- # Mock the async context manager __aenter__ to return the mock session- mock_session_class.return_value.__aenter__ = AsyncMock(return_value=mock_session)+ with patch("tool.tavily_search.aiohttp.ClientSession") as mock_session_class:+ # Create a mock session+ mock_session = MagicMock()++ # Mock `post` method with a response+ mock_response = SimpleNamespace(+ status=200,+ json=AsyncMock(+ return_value={+ "results": [+ {"title": "Mock Title", "url": "https://example.com", "content": "Mock Content", "score": 1.0},+ ]+ }+ ),+ )+ mock_session.post.return_value.__aenter__.return_value = mock_response++ # Mock the session returned by `ClientSession`+ mock_session_class.return_value.__aenter__.return_value = mock_sessionyield mock_session@pytest.mark.asyncio-async def test_searxng_search_tool_missing_fields(mock_aiohttp_session):+async def test_tavily_search_tool_missing_fields(mock_aiohttp_session):mock_api_key = "KEY"mock_query = "query with missing fields"mock_response_data = {"results": [- {"title": "Result Missing Content", "url": "https://example.com/1", "query": mock_query},- {"content": "Result Missing Title", "url": "https://example.com/2", "query": mock_query},- {"title": "Result Missing URL", "content": "Some content", "query": mock_query},- {"title": "Result Missing Query", "url": "https://example.com/4", "content": "Some content"},- {"title": "Valid Result", "url": "https://example.com/5", "content": "Valid content", "query": mock_query},+ {"title": "Result Missing Content", "url": "https://example.com/1", "score": 0.8},+ {"content": "Result Missing Title", "url": "https://example.com/2", "score": 0.8},+ {"title": "Result Missing URL", "content": "Some content", "score": 0.8},+ {"title": "Result Missing Score", "url": "https://example.com/4", "content": "Some content"},+ {"title": "Result Missing Query", "url": "https://example.com/4", "content": "Some content", "score": 0.7},+ {"title": "Valid Result", "url": "https://example.com/2", "content": "Valid content", "score": 1.0},]}- # Create a mock response object- mock_response = AsyncMock()- mock_response.status = 200- mock_response.json.return_value = mock_response_data-- # Configure the mock- mock_aiohttp_session.get.return_value.__aenter__.return_value = mock_response+ # Set up the mocked response for post+ mock_aiohttp_session.post.return_value.__aenter__.return_value.json.return_value = mock_response_data# Initialize the tooltavily_tool = TavilySearchTool(TavilySearchToolConfig(api_key=mock_api_key))@@ -58,7 +69,7 @@ async def test_searxng_search_tool_missing_fields(mock_aiohttp_session):assert result.results[1].title == "Valid Result"-def test_searxng_search_tool_sync_run_method(mock_aiohttp_session):+def test_tavily_search_tool_sync_run_method(mock_aiohttp_session):mock_api_key = "KEY"mock_query = "sync query"mock_response_data = {@@ -68,6 +79,7 @@ def test_searxng_search_tool_sync_run_method(mock_aiohttp_session):"url": "https://example.com/sync","content": "Sync content","query": mock_query,+ "score": 1.0,}]}@@ -78,7 +90,7 @@ def test_searxng_search_tool_sync_run_method(mock_aiohttp_session):mock_response.json.return_value = mock_response_data# Configure the mock- mock_aiohttp_session.get.return_value.__aenter__.return_value = mock_response+ mock_aiohttp_session.post.return_value.__aenter__.return_value = mock_response# Initialize the tooltavily_tool = TavilySearchTool(TavilySearchToolConfig(api_key=mock_api_key))@@ -93,12 +105,18 @@ def test_searxng_search_tool_sync_run_method(mock_aiohttp_session):@pytest.mark.asyncio-async def test_searxng_search_tool_with_max_results(mock_aiohttp_session):+async def test_tavily_search_tool_with_max_results(mock_aiohttp_session):mock_api_key = "KEY"mock_query = "test query with max results"mock_response_data = {"results": [- {"title": f"Result {i}", "url": f"https://example.com/{i}", "content": f"Content {i}", "query": mock_query}+ {+ "title": f"Result {i}",+ "url": f"https://example.com/{i}",+ "content": f"Content {i}",+ "query": mock_query,+ "score": 0.5,+ }for i in range(10)]}@@ -109,7 +127,7 @@ async def test_searxng_search_tool_with_max_results(mock_aiohttp_session):mock_response.json.return_value = mock_response_data# Configure the mock- mock_aiohttp_session.get.return_value.__aenter__.return_value = mock_response+ mock_aiohttp_session.post.return_value.__aenter__.return_value = mock_response# Initialize the tooltavily_tool = TavilySearchTool(TavilySearchToolConfig(api_key=mock_api_key, max_results=10))@@ -123,7 +141,7 @@ async def test_searxng_search_tool_with_max_results(mock_aiohttp_session):@pytest.mark.asyncio-async def test_searxng_search_tool_no_results(mock_aiohttp_session):+async def test_tavily_search_tool_no_results(mock_aiohttp_session):# Existing test case remains the samemock_api_key = "KEY"mock_query = "no results query"@@ -135,7 +153,7 @@ async def test_searxng_search_tool_no_results(mock_aiohttp_session):mock_response.json.return_value = mock_response_data# Configure the mock- mock_aiohttp_session.get.return_value.__aenter__.return_value = mock_response+ mock_aiohttp_session.post.return_value.__aenter__.return_value = mock_response# Initialize the tooltavily_tool = TavilySearchTool(TavilySearchToolConfig(api_key=mock_api_key))@@ -149,7 +167,7 @@ async def test_searxng_search_tool_no_results(mock_aiohttp_session):@pytest.mark.asyncio-async def test_searxng_search_tool_error(mock_aiohttp_session):+async def test_tavily_search_tool_error(mock_aiohttp_session):# Existing test case remains the samemock_api_key = "KEY"mock_query = "error query"@@ -160,7 +178,7 @@ async def test_searxng_search_tool_error(mock_aiohttp_session):mock_response.reason = "Internal Server Error"# Configure the mock- mock_aiohttp_session.get.return_value.__aenter__.return_value = mock_response+ mock_aiohttp_session.post.return_value.__aenter__.return_value = mock_response# Initialize the tooltavily_tool = TavilySearchTool(TavilySearchToolConfig(api_key=mock_api_key))@@ -174,5 +192,39 @@ async def test_searxng_search_tool_error(mock_aiohttp_session):assert "Failed to fetch search results" in str(excinfo.value)+async def test_tavily_search_tool_include_answer(mock_aiohttp_session):+ mock_api_key = "KEY"+ mock_query = "test query with answer"+ mock_answer = "This is a test answer."+ mock_response_data = {+ "results": [+ {"title": "Result 1", "url": "https://example.com/1", "content": "Content 1", "score": 1.0},+ {"title": "Result 2", "url": "https://example.com/2", "content": "Content 2", "score": 0.8},+ ],+ "answer": mock_answer,+ }++ # Set up the mocked response for post+ mock_aiohttp_session.post.return_value.__aenter__.return_value.json.return_value = mock_response_data++ # Initialize the tool with `include_answer=True`+ tavily_tool = TavilySearchTool(TavilySearchToolConfig(api_key=mock_api_key))+ tavily_tool.include_answer = True # Enable the include_answer feature+ input_schema = TavilySearchToolInputSchema(queries=[mock_query])++ # Run the tool+ result = await tavily_tool.run_async(input_schema)++ # Assertions+ assert len(result.results) == 2+ assert result.results[0].title == "Result 1"+ assert result.results[1].title == "Result 2"++ # Validate that the answer is included in each result+ assert result.results[0].answer == mock_answer+ assert result.results[1].answer == mock_answer++if __name__ == "__main__":pytest.main([__file__])