mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: Achieve 85%+ unit test coverage for azurefunctions package (#3866)
* Initial plan * Initial analysis: azurefunctions package at 80% coverage, need 85% Co-authored-by: larohra <41490930+larohra@users.noreply.github.com> * Add comprehensive unit tests to achieve 86% coverage for azurefunctions package Co-authored-by: larohra <41490930+larohra@users.noreply.github.com> * Add comprehensive coverage report documentation for azurefunctions package Co-authored-by: larohra <41490930+larohra@users.noreply.github.com> * Fix linting errors: combine nested with statements in test_entities.py Co-authored-by: larohra <41490930+larohra@users.noreply.github.com> * Remove COVERAGE_REPORT.md and coverage.json files as requested Co-authored-by: larohra <41490930+larohra@users.noreply.github.com> * Address PR review feedback: fix unused variables, remove line numbers from docstrings, improve test clarity Co-authored-by: larohra <41490930+larohra@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: larohra <41490930+larohra@users.noreply.github.com> Co-authored-by: Laveesh Rohra <larohra@microsoft.com> Co-authored-by: Tao Chen <taochen@microsoft.com>
This commit is contained in:
co-authored by
larohra
copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Laveesh Rohra
Tao Chen
parent
e563849be3
commit
cd1e3110aa
@@ -198,6 +198,114 @@ class TestCreateAgentEntity:
|
||||
persisted_state = mock_context.set_state.call_args[0][0]
|
||||
assert persisted_state["data"]["conversationHistory"] == []
|
||||
|
||||
def test_entity_function_handles_string_input(self) -> None:
|
||||
"""Test that the entity function handles non-dict input by converting to string."""
|
||||
mock_agent = Mock()
|
||||
mock_agent.run = AsyncMock(return_value=_agent_response("String response"))
|
||||
|
||||
entity_function = create_agent_entity(mock_agent)
|
||||
|
||||
# Mock context with non-dict input (like a number)
|
||||
mock_context = Mock()
|
||||
mock_context.operation_name = "run"
|
||||
mock_context.entity_key = "conv-456"
|
||||
# Use a number to test the str() conversion path
|
||||
mock_context.get_input.return_value = 12345
|
||||
mock_context.get_state.return_value = None
|
||||
|
||||
# Execute - entity will convert non-dict input to string
|
||||
entity_function(mock_context)
|
||||
|
||||
# Verify the result was set
|
||||
assert mock_context.set_result.called
|
||||
|
||||
def test_entity_function_handles_none_input(self) -> None:
|
||||
"""Test that the entity function handles None input by converting to empty string."""
|
||||
mock_agent = Mock()
|
||||
mock_agent.run = AsyncMock(return_value=_agent_response("Empty response"))
|
||||
|
||||
entity_function = create_agent_entity(mock_agent)
|
||||
|
||||
# Mock context with None input
|
||||
mock_context = Mock()
|
||||
mock_context.operation_name = "run"
|
||||
mock_context.entity_key = "conv-789"
|
||||
mock_context.get_input.return_value = None
|
||||
mock_context.get_state.return_value = None
|
||||
|
||||
# Execute - should hit error path since entity expects dict or valid JSON string
|
||||
entity_function(mock_context)
|
||||
|
||||
# Verify the result was set (likely error result)
|
||||
assert mock_context.set_result.called
|
||||
|
||||
def test_entity_function_handles_event_loop_runtime_error(self) -> None:
|
||||
"""Test that the entity function handles RuntimeError from get_event_loop by creating a new loop."""
|
||||
from unittest.mock import patch
|
||||
|
||||
mock_agent = Mock()
|
||||
mock_agent.run = AsyncMock(return_value=_agent_response("Response"))
|
||||
|
||||
entity_function = create_agent_entity(mock_agent)
|
||||
|
||||
mock_context = Mock()
|
||||
mock_context.operation_name = "run"
|
||||
mock_context.entity_key = "conv-loop-test"
|
||||
mock_context.get_input.return_value = {"message": "Test"}
|
||||
mock_context.get_state.return_value = None
|
||||
|
||||
# Simulate RuntimeError when getting event loop
|
||||
with (
|
||||
patch("asyncio.get_event_loop", side_effect=RuntimeError("No event loop")),
|
||||
patch("asyncio.new_event_loop") as mock_new_loop,
|
||||
patch("asyncio.set_event_loop") as mock_set_loop,
|
||||
):
|
||||
mock_loop = Mock()
|
||||
mock_loop.is_running.return_value = False
|
||||
mock_loop.run_until_complete = Mock()
|
||||
mock_new_loop.return_value = mock_loop
|
||||
|
||||
# Execute
|
||||
entity_function(mock_context)
|
||||
|
||||
# Verify new event loop was created
|
||||
mock_new_loop.assert_called_once()
|
||||
mock_set_loop.assert_called_once_with(mock_loop)
|
||||
|
||||
def test_entity_function_handles_running_event_loop(self) -> None:
|
||||
"""Test that the entity function handles a running event loop by creating a temporary loop."""
|
||||
from unittest.mock import patch
|
||||
|
||||
mock_agent = Mock()
|
||||
mock_agent.run = AsyncMock(return_value=_agent_response("Response"))
|
||||
|
||||
entity_function = create_agent_entity(mock_agent)
|
||||
|
||||
mock_context = Mock()
|
||||
mock_context.operation_name = "run"
|
||||
mock_context.entity_key = "conv-running-loop"
|
||||
mock_context.get_input.return_value = {"message": "Test"}
|
||||
mock_context.get_state.return_value = None
|
||||
|
||||
# Simulate a running event loop
|
||||
mock_existing_loop = Mock()
|
||||
mock_existing_loop.is_running.return_value = True
|
||||
|
||||
mock_temp_loop = Mock()
|
||||
mock_temp_loop.run_until_complete = Mock()
|
||||
mock_temp_loop.close = Mock()
|
||||
|
||||
with (
|
||||
patch("asyncio.get_event_loop", return_value=mock_existing_loop),
|
||||
patch("asyncio.new_event_loop", return_value=mock_temp_loop),
|
||||
):
|
||||
# Execute
|
||||
entity_function(mock_context)
|
||||
|
||||
# Verify temporary loop was created and closed
|
||||
mock_temp_loop.run_until_complete.assert_called_once()
|
||||
mock_temp_loop.close.assert_called_once()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v", "--tb=short"])
|
||||
|
||||
Reference in New Issue
Block a user