Skip to content

Commit c96e2ca

Browse files
authored
Python: Added tests for OpenAI content types + Unit test improvement (microsoft#3259)
* added tests for content types+ unit test improvement * small fixes * small fix
1 parent f882357 commit c96e2ca

File tree

3 files changed

+948
-0
lines changed

3 files changed

+948
-0
lines changed

python/packages/core/tests/openai/test_openai_assistants_client.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,81 @@ def test_parse_function_calls_from_assistants_basic(mock_async_openai: MagicMock
609609
assert contents[0].arguments == {"location": "Seattle"}
610610

611611

612+
def test_parse_run_step_with_code_interpreter_tool_call(mock_async_openai: MagicMock) -> None:
613+
"""Test _parse_run_step_tool_call with code_interpreter type creates CodeInterpreterToolCallContent."""
614+
client = create_test_openai_assistants_client(
615+
mock_async_openai,
616+
model_id="test-model",
617+
assistant_id="test-assistant",
618+
)
619+
620+
# Mock a run with required_action containing code_interpreter tool call
621+
mock_run = MagicMock()
622+
mock_run.id = "run_123"
623+
mock_run.status = "requires_action"
624+
625+
mock_tool_call = MagicMock()
626+
mock_tool_call.id = "call_code_123"
627+
mock_tool_call.type = "code_interpreter"
628+
mock_code_interpreter = MagicMock()
629+
mock_code_interpreter.input = "print('Hello, World!')"
630+
mock_tool_call.code_interpreter = mock_code_interpreter
631+
632+
mock_required_action = MagicMock()
633+
mock_required_action.submit_tool_outputs = MagicMock()
634+
mock_required_action.submit_tool_outputs.tool_calls = [mock_tool_call]
635+
mock_run.required_action = mock_required_action
636+
637+
# Parse the run step
638+
contents = client._parse_function_calls_from_assistants(mock_run, "response_123")
639+
640+
# Should have CodeInterpreterToolCallContent
641+
assert len(contents) == 1
642+
assert contents[0].type == "code_interpreter_tool_call"
643+
assert contents[0].call_id == '["response_123", "call_code_123"]'
644+
assert contents[0].inputs is not None
645+
assert len(contents[0].inputs) == 1
646+
assert contents[0].inputs[0].type == "text"
647+
assert contents[0].inputs[0].text == "print('Hello, World!')"
648+
649+
650+
def test_parse_run_step_with_mcp_tool_call(mock_async_openai: MagicMock) -> None:
651+
"""Test _parse_run_step_tool_call with mcp type creates MCPServerToolCallContent."""
652+
client = create_test_openai_assistants_client(
653+
mock_async_openai,
654+
model_id="test-model",
655+
assistant_id="test-assistant",
656+
)
657+
658+
# Mock a run with required_action containing mcp tool call
659+
mock_run = MagicMock()
660+
mock_run.id = "run_456"
661+
mock_run.status = "requires_action"
662+
663+
mock_tool_call = MagicMock()
664+
mock_tool_call.id = "call_mcp_456"
665+
mock_tool_call.type = "mcp"
666+
mock_tool_call.name = "fetch_data"
667+
mock_tool_call.server_label = "DataServer"
668+
mock_tool_call.args = {"key": "value"}
669+
670+
mock_required_action = MagicMock()
671+
mock_required_action.submit_tool_outputs = MagicMock()
672+
mock_required_action.submit_tool_outputs.tool_calls = [mock_tool_call]
673+
mock_run.required_action = mock_required_action
674+
675+
# Parse the run step
676+
contents = client._parse_function_calls_from_assistants(mock_run, "response_456")
677+
678+
# Should have MCPServerToolCallContent
679+
assert len(contents) == 1
680+
assert contents[0].type == "mcp_server_tool_call"
681+
assert contents[0].call_id == '["response_456", "call_mcp_456"]'
682+
assert contents[0].tool_name == "fetch_data"
683+
assert contents[0].server_name == "DataServer"
684+
assert contents[0].arguments == {"key": "value"}
685+
686+
612687
def test_prepare_options_basic(mock_async_openai: MagicMock) -> None:
613688
"""Test _prepare_options with basic chat options."""
614689
chat_client = create_test_openai_assistants_client(mock_async_openai)

0 commit comments

Comments
 (0)