Skip to content

Commit 9da4062

Browse files
committed
🧪 add test case
1 parent f90c6da commit 9da4062

File tree

2 files changed

+92
-36
lines changed

2 files changed

+92
-36
lines changed

‎test/backend/agents/test_create_agent_info.py‎

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ async def test_create_agent_config_basic(self):
295295
patch('backend.agents.create_agent_info.tenant_config_manager') as mock_tenant_config, \
296296
patch('backend.agents.create_agent_info.build_memory_context') as mock_build_memory, \
297297
patch('backend.agents.create_agent_info.get_selected_knowledge_list') as mock_knowledge, \
298-
patch('backend.agents.create_agent_info.prepare_prompt_templates') as mock_prepare_templates:
298+
patch('backend.agents.create_agent_info.prepare_prompt_templates') as mock_prepare_templates, \
299+
patch('backend.agents.create_agent_info.get_model_by_model_id') as mock_get_model_by_id:
299300

300301
# Set mock return values
301302
mock_search_agent.return_value = {
@@ -305,7 +306,7 @@ async def test_create_agent_config_basic(self):
305306
"constraint_prompt": "test constraint",
306307
"few_shots_prompt": "test few shots",
307308
"max_steps": 5,
308-
"model_name": "test_model",
309+
"model_id": 123,
309310
"provide_run_summary": True
310311
}
311312
mock_query_sub.return_value = []
@@ -324,6 +325,7 @@ async def test_create_agent_config_basic(self):
324325
mock_knowledge.return_value = []
325326
mock_prepare_templates.return_value = {
326327
"system_prompt": "populated_system_prompt"}
328+
mock_get_model_by_id.return_value = {"display_name": "test_model"}
327329

328330
result = await create_agent_config("agent_1", "tenant_1", "user_1", "zh", "test query")
329331

@@ -350,7 +352,8 @@ async def test_create_agent_config_with_sub_agents(self):
350352
patch('backend.agents.create_agent_info.build_memory_context') as mock_build_memory, \
351353
patch('backend.agents.create_agent_info.search_memory_in_levels', new_callable=AsyncMock) as mock_search_memory, \
352354
patch('backend.agents.create_agent_info.get_selected_knowledge_list') as mock_knowledge, \
353-
patch('backend.agents.create_agent_info.prepare_prompt_templates') as mock_prepare_templates:
355+
patch('backend.agents.create_agent_info.prepare_prompt_templates') as mock_prepare_templates, \
356+
patch('backend.agents.create_agent_info.get_model_by_model_id') as mock_get_model_by_id:
354357

355358
# Set mock return values
356359
mock_search_agent.return_value = {
@@ -360,7 +363,7 @@ async def test_create_agent_config_with_sub_agents(self):
360363
"constraint_prompt": "test constraint",
361364
"few_shots_prompt": "test few shots",
362365
"max_steps": 5,
363-
"model_name": "test_model",
366+
"model_id": 123,
364367
"provide_run_summary": True
365368
}
366369
mock_query_sub.return_value = ["sub_agent_1"]
@@ -379,6 +382,7 @@ async def test_create_agent_config_with_sub_agents(self):
379382
mock_knowledge.return_value = []
380383
mock_prepare_templates.return_value = {
381384
"system_prompt": "populated_system_prompt"}
385+
mock_get_model_by_id.return_value = {"display_name": "test_model"}
382386

383387
# Mock sub-agent configuration
384388
mock_sub_agent_config = Mock()
@@ -415,7 +419,8 @@ async def test_create_agent_config_with_memory(self):
415419
patch('backend.agents.create_agent_info.build_memory_context') as mock_build_memory, \
416420
patch('backend.agents.create_agent_info.search_memory_in_levels', new_callable=AsyncMock) as mock_search_memory, \
417421
patch('backend.agents.create_agent_info.get_selected_knowledge_list') as mock_knowledge, \
418-
patch('backend.agents.create_agent_info.prepare_prompt_templates') as mock_prepare_templates:
422+
patch('backend.agents.create_agent_info.prepare_prompt_templates') as mock_prepare_templates, \
423+
patch('backend.agents.create_agent_info.get_model_by_model_id') as mock_get_model_by_id:
419424

420425
# Set mock return values
421426
mock_search_agent.return_value = {
@@ -425,7 +430,7 @@ async def test_create_agent_config_with_memory(self):
425430
"constraint_prompt": "test constraint",
426431
"few_shots_prompt": "test few shots",
427432
"max_steps": 5,
428-
"model_name": "test_model",
433+
"model_id": 123,
429434
"provide_run_summary": True
430435
}
431436
mock_query_sub.return_value = []
@@ -453,6 +458,7 @@ async def test_create_agent_config_with_memory(self):
453458
mock_knowledge.return_value = []
454459
mock_prepare_templates.return_value = {
455460
"system_prompt": "populated_system_prompt"}
461+
mock_get_model_by_id.return_value = {"display_name": "test_model"}
456462

457463
result = await create_agent_config("agent_1", "tenant_1", "user_1", "zh", "test query")
458464

@@ -487,6 +493,9 @@ async def test_create_agent_config_memory_disabled_no_search(self):
487493
patch(
488494
"backend.agents.create_agent_info.build_memory_context"
489495
) as mock_build_memory,
496+
patch(
497+
"backend.agents.create_agent_info.get_model_by_model_id"
498+
) as mock_get_model_by_id,
490499
patch(
491500
"backend.agents.create_agent_info.search_memory_in_levels",
492501
new_callable=AsyncMock,
@@ -505,7 +514,7 @@ async def test_create_agent_config_memory_disabled_no_search(self):
505514
"constraint_prompt": "test constraint",
506515
"few_shots_prompt": "test few shots",
507516
"max_steps": 5,
508-
"model_name": "test_model",
517+
"model_id": 123,
509518
"provide_run_summary": True,
510519
}
511520
mock_query_sub.return_value = []
@@ -536,6 +545,7 @@ async def test_create_agent_config_memory_disabled_no_search(self):
536545
mock_prepare_templates.return_value = {
537546
"system_prompt": "populated_system_prompt"
538547
}
548+
mock_get_model_by_id.return_value = {"display_name": "test_model"}
539549

540550
await create_agent_config(
541551
"agent_1",
@@ -548,6 +558,62 @@ async def test_create_agent_config_memory_disabled_no_search(self):
548558

549559
mock_search_memory.assert_not_called()
550560

561+
@pytest.mark.asyncio
562+
async def test_create_agent_config_model_id_none(self):
563+
"""Test case for creating agent configuration when model_id is None"""
564+
with patch('backend.agents.create_agent_info.search_agent_info_by_agent_id') as mock_search_agent, \
565+
patch('backend.agents.create_agent_info.query_sub_agents_id_list') as mock_query_sub, \
566+
patch('backend.agents.create_agent_info.create_tool_config_list') as mock_create_tools, \
567+
patch('backend.agents.create_agent_info.get_agent_prompt_template') as mock_get_template, \
568+
patch('backend.agents.create_agent_info.tenant_config_manager') as mock_tenant_config, \
569+
patch('backend.agents.create_agent_info.build_memory_context') as mock_build_memory, \
570+
patch('backend.agents.create_agent_info.get_selected_knowledge_list') as mock_knowledge, \
571+
patch('backend.agents.create_agent_info.prepare_prompt_templates') as mock_prepare_templates, \
572+
patch('backend.agents.create_agent_info.get_model_by_model_id') as mock_get_model_by_id:
573+
574+
# Set mock return values
575+
mock_search_agent.return_value = {
576+
"name": "test_agent",
577+
"description": "test description",
578+
"duty_prompt": "test duty",
579+
"constraint_prompt": "test constraint",
580+
"few_shots_prompt": "test few shots",
581+
"max_steps": 5,
582+
"model_id": None, # Test None case
583+
"provide_run_summary": True
584+
}
585+
mock_query_sub.return_value = []
586+
mock_create_tools.return_value = []
587+
mock_get_template.return_value = {
588+
"system_prompt": "{{duty}} {{constraint}} {{few_shots}}"}
589+
mock_tenant_config.get_app_config.side_effect = [
590+
"TestApp", "Test Description"]
591+
mock_build_memory.return_value = Mock(
592+
user_config=Mock(memory_switch=False),
593+
memory_config={},
594+
tenant_id="tenant_1",
595+
user_id="user_1",
596+
agent_id="agent_1"
597+
)
598+
mock_knowledge.return_value = []
599+
mock_prepare_templates.return_value = {
600+
"system_prompt": "populated_system_prompt"}
601+
mock_get_model_by_id.return_value = None # Model not found
602+
603+
result = await create_agent_config("agent_1", "tenant_1", "user_1", "zh", "test query")
604+
605+
# Verify that AgentConfig was called with "main_model" as fallback
606+
mock_agent_config.assert_called_with(
607+
name="test_agent",
608+
description="test description",
609+
prompt_templates={"system_prompt": "populated_system_prompt"},
610+
tools=[],
611+
max_steps=5,
612+
model_name="main_model", # Should fallback to "main_model"
613+
provide_run_summary=True,
614+
managed_agents=[]
615+
)
616+
551617
@pytest.mark.asyncio
552618
async def test_create_agent_config_memory_exception(self):
553619
"""raise when search_memory_in_levels raises an exception"""
@@ -588,7 +654,7 @@ async def test_create_agent_config_memory_exception(self):
588654
"constraint_prompt": "test constraint",
589655
"few_shots_prompt": "test few shots",
590656
"max_steps": 5,
591-
"model_name": "test_model",
657+
"model_id": 123,
592658
"provide_run_summary": True,
593659
}
594660
mock_query_sub.return_value = []

0 commit comments

Comments
 (0)