Add pre commit (#26)
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
# tests/agent_deep_research_test.py
|
||||
import logging
|
||||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import shutil
|
||||
import tempfile
|
||||
from unittest.mock import Mock, AsyncMock, patch
|
||||
from unittest.mock import Mock, AsyncMock, patch, MagicMock
|
||||
|
||||
import pytest
|
||||
from agentscope.formatter import DashScopeChatFormatter
|
||||
@@ -12,7 +11,9 @@ from agentscope.memory import InMemoryMemory
|
||||
from agentscope.message import Msg
|
||||
from agentscope.model import DashScopeChatModel
|
||||
|
||||
from deep_research.agent_deep_research.deep_research_agent import DeepResearchAgent
|
||||
from deep_research.agent_deep_research.deep_research_agent import (
|
||||
DeepResearchAgent,
|
||||
)
|
||||
from deep_research.agent_deep_research.main import main
|
||||
|
||||
|
||||
@@ -70,12 +71,15 @@ class TestDeepResearchAgent:
|
||||
|
||||
def test_agent_initialization(
|
||||
self,
|
||||
mock_model,
|
||||
mock_tavily_client,
|
||||
temp_working_dir,
|
||||
mock_model, # pylint: disable=redefined-outer-name
|
||||
mock_tavily_client, # pylint: disable=redefined-outer-name
|
||||
temp_working_dir, # pylint: disable=redefined-outer-name
|
||||
):
|
||||
"""Test agent initialization with valid parameters"""
|
||||
with patch("asyncio.create_task"):
|
||||
mock_loop = MagicMock()
|
||||
mock_task = AsyncMock()
|
||||
mock_loop.create_task = MagicMock(return_value=mock_task)
|
||||
with patch("asyncio.get_running_loop", return_value=mock_loop):
|
||||
agent = DeepResearchAgent(
|
||||
name="Friday",
|
||||
sys_prompt="You are a helpful assistant named Friday.",
|
||||
@@ -87,17 +91,17 @@ class TestDeepResearchAgent:
|
||||
)
|
||||
|
||||
assert agent.name == "Friday"
|
||||
assert agent.sys_prompt.startswith("You are a helpful assistant named Friday.")
|
||||
assert agent.sys_prompt.startswith(
|
||||
"You are a helpful assistant named Friday.",
|
||||
)
|
||||
assert agent.tmp_file_storage_dir == temp_working_dir
|
||||
assert os.path.exists(temp_working_dir)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_main_function_success(
|
||||
self,
|
||||
mock_env_vars,
|
||||
mock_tavily_client,
|
||||
mock_model,
|
||||
temp_working_dir,
|
||||
mock_tavily_client, # pylint: disable=redefined-outer-name
|
||||
temp_working_dir, # pylint: disable=redefined-outer-name
|
||||
):
|
||||
"""Test main function with successful execution"""
|
||||
with patch(
|
||||
@@ -109,17 +113,26 @@ class TestDeepResearchAgent:
|
||||
autospec=True,
|
||||
) as mock_agent_class:
|
||||
mock_agent = AsyncMock()
|
||||
mock_agent.return_value = Msg("Friday", "Test response", "assistant")
|
||||
mock_agent.return_value = Msg(
|
||||
"Friday",
|
||||
"Test response",
|
||||
"assistant",
|
||||
)
|
||||
mock_agent_class.return_value = mock_agent
|
||||
|
||||
with patch("os.makedirs") as mock_makedirs:
|
||||
with patch.dict(os.environ, {"AGENT_OPERATION_DIR": temp_working_dir}):
|
||||
with patch.dict(
|
||||
os.environ,
|
||||
{"AGENT_OPERATION_DIR": temp_working_dir},
|
||||
):
|
||||
test_query = "Test research question"
|
||||
msg = Msg("Bob", test_query, "user")
|
||||
|
||||
await main(test_query)
|
||||
|
||||
mock_makedirs.assert_called_once_with(temp_working_dir, exist_ok=True)
|
||||
mock_makedirs.assert_called_once_with(
|
||||
temp_working_dir,
|
||||
exist_ok=True,
|
||||
)
|
||||
mock_agent_class.assert_called_once()
|
||||
|
||||
# ✅ Use assert_called_once() + manual argument check
|
||||
@@ -138,8 +151,7 @@ class TestDeepResearchAgent:
|
||||
@pytest.mark.asyncio
|
||||
async def test_agent_cleanup(
|
||||
self,
|
||||
mock_env_vars,
|
||||
mock_tavily_client,
|
||||
mock_tavily_client, # pylint: disable=redefined-outer-name
|
||||
):
|
||||
"""Test proper cleanup of resources"""
|
||||
with patch(
|
||||
@@ -151,7 +163,10 @@ class TestDeepResearchAgent:
|
||||
|
||||
mock_tavily_client.close.assert_called_once()
|
||||
|
||||
def test_working_directory_creation(self, temp_working_dir):
|
||||
def test_working_directory_creation(
|
||||
self,
|
||||
temp_working_dir, # pylint: disable=redefined-outer-name
|
||||
):
|
||||
"""Test working directory is created correctly"""
|
||||
test_dir = os.path.join(temp_working_dir, "test_subdir")
|
||||
os.makedirs(test_dir, exist_ok=True)
|
||||
@@ -161,17 +176,28 @@ class TestDeepResearchAgent:
|
||||
|
||||
class TestErrorHandling:
|
||||
"""Test suite for error handling scenarios"""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_filesystem_errors(self, mock_env_vars, mock_tavily_client):
|
||||
async def test_filesystem_errors(
|
||||
self,
|
||||
mock_tavily_client, # pylint: disable=redefined-outer-name
|
||||
):
|
||||
"""Test handling of filesystem errors"""
|
||||
with patch(
|
||||
"deep_research.agent_deep_research.main.StdIOStatefulClient",
|
||||
return_value=mock_tavily_client,
|
||||
"deep_research.agent_deep_research.main.StdIOStatefulClient",
|
||||
return_value=mock_tavily_client,
|
||||
):
|
||||
with patch.dict(os.environ, {"AGENT_OPERATION_DIR": "/invalid/path"}):
|
||||
with patch("os.makedirs", side_effect=PermissionError("Permission denied")):
|
||||
with patch.dict(
|
||||
os.environ,
|
||||
{"AGENT_OPERATION_DIR": "/invalid/path"},
|
||||
):
|
||||
with patch(
|
||||
"os.makedirs",
|
||||
side_effect=PermissionError("Permission denied"),
|
||||
):
|
||||
with pytest.raises(PermissionError):
|
||||
await main("Test query")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main(["-v", __file__])
|
||||
pytest.main(["-v", __file__])
|
||||
|
||||
Reference in New Issue
Block a user