Skip to content

Commit 4dcf72a

Browse files
committed
refactor: reformat test fixtures in data_juicer_agent_test.py
1 parent 3816343 commit 4dcf72a

File tree

1 file changed

+69
-76
lines changed

1 file changed

+69
-76
lines changed

tests/data_juicer_agent_test.py

Lines changed: 69 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
root_path = Path(__file__).parent.parent
88
sys.path.insert(0, str(root_path))
9-
sys.path.insert(0, str(Path(root_path)/"data_juicer_agent"))
9+
sys.path.insert(0, str(Path(root_path) / "data_juicer_agent"))
1010

1111
import pytest
1212
from unittest.mock import AsyncMock, Mock, patch
@@ -39,44 +39,68 @@
3939
)
4040

4141

42+
@pytest.fixture
43+
def mock_toolkit():
44+
"""Create a mocked Toolkit instance"""
45+
return Mock(spec=Toolkit)
46+
47+
48+
@pytest.fixture
49+
def mock_model():
50+
"""Create a mocked DashScopeChatModel"""
51+
model = Mock(spec=DashScopeChatModel)
52+
model.call = AsyncMock(
53+
return_value=Msg("assistant", "test response", role="assistant"),
54+
)
55+
return model
56+
57+
58+
@pytest.fixture
59+
def mock_formatter():
60+
"""Create a mocked DashScopeChatFormatter"""
61+
return Mock(spec=DashScopeChatFormatter)
62+
63+
64+
@pytest.fixture
65+
def mock_memory():
66+
"""Create a mocked InMemoryMemory"""
67+
return Mock(spec=InMemoryMemory)
68+
69+
70+
@pytest.fixture
71+
def mock_mcp_client():
72+
"""Create a mocked MCP client"""
73+
mock_client = Mock()
74+
mock_client.name = "DJ_recipe_flow"
75+
mock_client.connect = AsyncMock()
76+
mock_client.close = AsyncMock()
77+
mock_client.get_callable_function = AsyncMock()
78+
mock_client.list_tools = AsyncMock()
79+
return mock_client
80+
81+
82+
@pytest.fixture
83+
def mock_agent(
84+
mock_model,
85+
mock_formatter,
86+
mock_toolkit,
87+
mock_memory,
88+
):
89+
"""Create a mocked ReActAgent instance"""
90+
agent = Mock(spec=ReActAgent)
91+
agent.model = mock_model
92+
agent.formatter = mock_formatter
93+
agent.toolkit = mock_toolkit
94+
agent.memory = mock_memory
95+
agent.__call__ = AsyncMock(
96+
return_value=Msg("assistant", "test response", role="assistant"),
97+
)
98+
return agent
99+
100+
42101
class TestDataJuicerAgent:
43102
"""Test suite for the data_juicer_agent functionality"""
44103

45-
@pytest.fixture
46-
def mock_toolkit(self):
47-
"""Create a mocked Toolkit instance"""
48-
return Mock(spec=Toolkit)
49-
50-
@pytest.fixture
51-
def mock_model(self):
52-
"""Create a mocked DashScopeChatModel"""
53-
model = Mock(spec=DashScopeChatModel)
54-
model.call = AsyncMock(
55-
return_value=Msg("assistant", "test response", role="assistant"),
56-
)
57-
return model
58-
59-
@pytest.fixture
60-
def mock_formatter(self):
61-
"""Create a mocked DashScopeChatFormatter"""
62-
return Mock(spec=DashScopeChatFormatter)
63-
64-
@pytest.fixture
65-
def mock_memory(self):
66-
"""Create a mocked InMemoryMemory"""
67-
return Mock(spec=InMemoryMemory)
68-
69-
@pytest.fixture
70-
def mock_mcp_client(self):
71-
"""Create a mocked MCP client"""
72-
mock_client = Mock()
73-
mock_client.name = "DJ_recipe_flow"
74-
mock_client.connect = AsyncMock()
75-
mock_client.close = AsyncMock()
76-
mock_client.get_callable_function = AsyncMock()
77-
mock_client.list_tools = AsyncMock()
78-
return mock_client
79-
80104
def create_named_mock_agent(self, name, mock_agent, *args, **kwargs):
81105
"""Create a named mock agent for testing"""
82106
agent_instance = Mock(spec=ReActAgent)
@@ -88,24 +112,8 @@ def create_named_mock_agent(self, name, mock_agent, *args, **kwargs):
88112
agent_instance.name = name
89113
return agent_instance
90114

91-
@pytest.fixture
92-
def mock_agent(
93-
self,
94-
mock_model,
95-
mock_formatter,
96-
mock_toolkit,
97-
mock_memory,
98-
):
99-
"""Create a mocked ReActAgent instance"""
100-
agent = Mock(spec=ReActAgent)
101-
agent.model = mock_model
102-
agent.formatter = mock_formatter
103-
agent.toolkit = mock_toolkit
104-
agent.memory = mock_memory
105-
agent.__call__ = AsyncMock(
106-
return_value=Msg("assistant", "test response", role="assistant"),
107-
)
108-
return agent
115+
async def mock_user_func(self, msg=None):
116+
return Msg("user", "exit", role="user")
109117

110118
def test_dj_toolkit_initialization(self):
111119
"""Test DJ toolkit initialization and tool registration"""
@@ -149,12 +157,12 @@ def test_dj_dev_toolkit_initialization(self):
149157
async def test_mcp_tools_list(self, mock_mcp_client):
150158
"""Test MCP tools list contains expected tools and MCP client binding"""
151159
with patch(
152-
"agentscope.mcp.HttpStatefulClient",
153-
return_value=mock_mcp_client,
154-
) as mock_client_cls:
160+
"agentscope.mcp.HttpStatefulClient",
161+
return_value=mock_mcp_client,
162+
) as mock_client_cls:
155163
await get_mcp_toolkit()
156164
assert mock_client_cls.assert_called_once
157-
165+
158166
expected_tools = [view_text_file, write_text_file]
159167
assert len(mcp_tools) == len(expected_tools)
160168
for tool in expected_tools:
@@ -174,20 +182,20 @@ async def test_agent_initialization(
174182
name="DataJuicer",
175183
sys_prompt="You are {name}, a agent.",
176184
toolkit=mock_toolkit,
185+
description="test description",
177186
model=mock_model,
178187
formatter=mock_formatter,
179188
memory=mock_memory,
180189
)
181190

182191
assert agent.name == "DataJuicer"
183192
assert "DataJuicer" in agent.sys_prompt
193+
assert "test" in agent.__doc__
184194
assert agent.model == mock_model
185195
assert agent.formatter == mock_formatter
186196
assert agent.toolkit == mock_toolkit
187197
assert agent.memory == mock_memory
188-
189-
async def mock_user_func(self, msg=None):
190-
return Msg("user", "exit", role="user")
198+
assert isinstance(agent, ReActAgent)
191199

192200
@pytest.mark.asyncio
193201
async def test_main_with_multiple_agents_loading(self, mock_agent, mock_mcp_client):
@@ -218,21 +226,6 @@ async def test_main_with_multiple_agents_loading(self, mock_agent, mock_mcp_clie
218226
# Validate multiple agents are correctly created (dj, dj_dev, dj_mcp, and router)
219227
assert mock_create_agent.call_count == 4
220228

221-
# Validate router agent is created
222-
create_calls = mock_create_agent.call_args_list
223-
router_agent_created = any(
224-
call[0][0] == "Router"
225-
for call in create_calls # First parameter is name
226-
)
227-
assert router_agent_created, "Router agent should be created"
228-
229-
# Validate dj_mcp agent is created
230-
mcp_agent_created = any(
231-
call[0][0] == "mcp_datajuicer_agent"
232-
for call in create_calls # First parameter is name
233-
)
234-
assert mcp_agent_created, "MCP agent should be created"
235-
236229

237230
if __name__ == "__main__":
238231
pytest.main(["-v", __file__])

0 commit comments

Comments
 (0)