Skip to content

Commit 00a4888

Browse files
committed
🧪 add test case
1 parent 166619a commit 00a4888

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

‎test/backend/services/test_agent_service.py‎

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,199 @@ async def test_get_agent_info_impl_with_tool_error(mock_search_agent_info, mock_
694694
mock_search_agent_info.assert_called_once_with(123, "test_tenant")
695695

696696

697+
@patch('backend.services.agent_service.get_model_by_model_id')
698+
@patch('backend.services.agent_service.query_sub_agents_id_list')
699+
@patch('backend.services.agent_service.search_tools_for_sub_agent')
700+
@patch('backend.services.agent_service.search_agent_info_by_agent_id')
701+
@pytest.mark.asyncio
702+
async def test_get_agent_info_impl_sub_agent_error(mock_search_agent_info, mock_search_tools, mock_query_sub_agents_id, mock_get_model_by_model_id):
703+
"""
704+
Test get_agent_info_impl with an error in retrieving sub agent id list.
705+
706+
This test verifies that:
707+
1. The function correctly gets the agent information
708+
2. When an error occurs retrieving sub agent id list
709+
3. The function returns the agent information with an empty sub_agent_id_list
710+
"""
711+
# Setup
712+
mock_agent_info = {
713+
"agent_id": 123,
714+
"model_id": None,
715+
"business_description": "Test agent"
716+
}
717+
mock_search_agent_info.return_value = mock_agent_info
718+
719+
mock_tools = [{"tool_id": 1, "name": "Tool 1"}]
720+
mock_search_tools.return_value = mock_tools
721+
722+
# Mock query_sub_agents_id_list to raise an exception
723+
mock_query_sub_agents_id.side_effect = Exception("Sub agent query error")
724+
mock_get_model_by_model_id.return_value = None
725+
726+
# Execute
727+
result = await get_agent_info_impl(agent_id=123, tenant_id="test_tenant")
728+
729+
# Assert
730+
assert result["agent_id"] == 123
731+
assert result["tools"] == mock_tools
732+
assert result["sub_agent_id_list"] == []
733+
assert result["model_name"] is None
734+
mock_search_agent_info.assert_called_once_with(123, "test_tenant")
735+
mock_search_tools.assert_called_once_with(
736+
agent_id=123, tenant_id="test_tenant")
737+
mock_query_sub_agents_id.assert_called_once_with(
738+
main_agent_id=123, tenant_id="test_tenant")
739+
740+
741+
@patch('backend.services.agent_service.get_model_by_model_id')
742+
@patch('backend.services.agent_service.query_sub_agents_id_list')
743+
@patch('backend.services.agent_service.search_tools_for_sub_agent')
744+
@patch('backend.services.agent_service.search_agent_info_by_agent_id')
745+
@pytest.mark.asyncio
746+
async def test_get_agent_info_impl_with_model_id_success(mock_search_agent_info, mock_search_tools, mock_query_sub_agents_id, mock_get_model_by_model_id):
747+
"""
748+
Test get_agent_info_impl with a valid model_id.
749+
750+
This test verifies that:
751+
1. The function correctly retrieves model information when model_id is not None
752+
2. It sets model_name from the model's display_name
753+
3. It handles the case when model_info is None
754+
"""
755+
# Setup
756+
mock_agent_info = {
757+
"agent_id": 123,
758+
"model_id": 456,
759+
"business_description": "Test agent"
760+
}
761+
mock_search_agent_info.return_value = mock_agent_info
762+
763+
mock_tools = [{"tool_id": 1, "name": "Tool 1"}]
764+
mock_search_tools.return_value = mock_tools
765+
766+
mock_sub_agent_ids = [789]
767+
mock_query_sub_agents_id.return_value = mock_sub_agent_ids
768+
769+
# Mock model info with display_name
770+
mock_model_info = {
771+
"model_id": 456,
772+
"display_name": "GPT-4",
773+
"provider": "openai"
774+
}
775+
mock_get_model_by_model_id.return_value = mock_model_info
776+
777+
# Execute
778+
result = await get_agent_info_impl(agent_id=123, tenant_id="test_tenant")
779+
780+
# Assert
781+
expected_result = {
782+
"agent_id": 123,
783+
"model_id": 456,
784+
"business_description": "Test agent",
785+
"tools": mock_tools,
786+
"sub_agent_id_list": mock_sub_agent_ids,
787+
"model_name": "GPT-4"
788+
}
789+
assert result == expected_result
790+
mock_get_model_by_model_id.assert_called_once_with(456)
791+
792+
793+
@patch('backend.services.agent_service.get_model_by_model_id')
794+
@patch('backend.services.agent_service.query_sub_agents_id_list')
795+
@patch('backend.services.agent_service.search_tools_for_sub_agent')
796+
@patch('backend.services.agent_service.search_agent_info_by_agent_id')
797+
@pytest.mark.asyncio
798+
async def test_get_agent_info_impl_with_model_id_no_display_name(mock_search_agent_info, mock_search_tools, mock_query_sub_agents_id, mock_get_model_by_model_id):
799+
"""
800+
Test get_agent_info_impl with model_id but model has no display_name.
801+
802+
This test verifies that:
803+
1. The function correctly retrieves model information when model_id is not None
804+
2. It sets model_name to None when model_info exists but has no display_name
805+
"""
806+
# Setup
807+
mock_agent_info = {
808+
"agent_id": 123,
809+
"model_id": 456,
810+
"business_description": "Test agent"
811+
}
812+
mock_search_agent_info.return_value = mock_agent_info
813+
814+
mock_tools = [{"tool_id": 1, "name": "Tool 1"}]
815+
mock_search_tools.return_value = mock_tools
816+
817+
mock_sub_agent_ids = [789]
818+
mock_query_sub_agents_id.return_value = mock_sub_agent_ids
819+
820+
# Mock model info without display_name
821+
mock_model_info = {
822+
"model_id": 456,
823+
"provider": "openai"
824+
# No display_name field
825+
}
826+
mock_get_model_by_model_id.return_value = mock_model_info
827+
828+
# Execute
829+
result = await get_agent_info_impl(agent_id=123, tenant_id="test_tenant")
830+
831+
# Assert
832+
expected_result = {
833+
"agent_id": 123,
834+
"model_id": 456,
835+
"business_description": "Test agent",
836+
"tools": mock_tools,
837+
"sub_agent_id_list": mock_sub_agent_ids,
838+
"model_name": None
839+
}
840+
assert result == expected_result
841+
mock_get_model_by_model_id.assert_called_once_with(456)
842+
843+
844+
@patch('backend.services.agent_service.get_model_by_model_id')
845+
@patch('backend.services.agent_service.query_sub_agents_id_list')
846+
@patch('backend.services.agent_service.search_tools_for_sub_agent')
847+
@patch('backend.services.agent_service.search_agent_info_by_agent_id')
848+
@pytest.mark.asyncio
849+
async def test_get_agent_info_impl_with_model_id_none_model_info(mock_search_agent_info, mock_search_tools, mock_query_sub_agents_id, mock_get_model_by_model_id):
850+
"""
851+
Test get_agent_info_impl with model_id but get_model_by_model_id returns None.
852+
853+
This test verifies that:
854+
1. The function correctly handles when model_id is not None but get_model_by_model_id returns None
855+
2. It sets model_name to None when model_info is None
856+
"""
857+
# Setup
858+
mock_agent_info = {
859+
"agent_id": 123,
860+
"model_id": 456,
861+
"business_description": "Test agent"
862+
}
863+
mock_search_agent_info.return_value = mock_agent_info
864+
865+
mock_tools = [{"tool_id": 1, "name": "Tool 1"}]
866+
mock_search_tools.return_value = mock_tools
867+
868+
mock_sub_agent_ids = [789]
869+
mock_query_sub_agents_id.return_value = mock_sub_agent_ids
870+
871+
# Mock get_model_by_model_id to return None
872+
mock_get_model_by_model_id.return_value = None
873+
874+
# Execute
875+
result = await get_agent_info_impl(agent_id=123, tenant_id="test_tenant")
876+
877+
# Assert
878+
expected_result = {
879+
"agent_id": 123,
880+
"model_id": 456,
881+
"business_description": "Test agent",
882+
"tools": mock_tools,
883+
"sub_agent_id_list": mock_sub_agent_ids,
884+
"model_name": None
885+
}
886+
assert result == expected_result
887+
mock_get_model_by_model_id.assert_called_once_with(456)
888+
889+
697890
async def test_list_all_agent_info_impl_success():
698891
"""
699892
Test successful retrieval of all agent information.

0 commit comments

Comments
 (0)