Skip to content

Commit d2e3902

Browse files
Merge pull request #854 from microsoft/bugfix/codeQL-suggestions
test: enhance mock specifications in various test files
2 parents 6b39c90 + 0ac4bd7 commit d2e3902

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/tests/backend/test_app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
import pytest
1414
import sys
1515
import os
16-
from unittest.mock import Mock, AsyncMock, patch, MagicMock
17-
from types import ModuleType
16+
from unittest.mock import Mock, AsyncMock, patch, NonCallableMock
1817

1918
# Environment variables are set by conftest.py, but ensure they're available
2019
os.environ.setdefault("APPLICATIONINSIGHTS_CONNECTION_STRING", "InstrumentationKey=test-key-12345")
@@ -36,7 +35,8 @@
3635
os.environ.setdefault("AZURE_OPENAI_RAI_DEPLOYMENT_NAME", "test-rai-deployment")
3736

3837
# Check if v4 has been mocked by another test file (prevents import errors)
39-
_v4_is_mocked = 'v4' in sys.modules and isinstance(sys.modules['v4'], (Mock, MagicMock))
38+
# Use NonCallableMock to catch all mock subclasses (Mock, MagicMock, etc.)
39+
_v4_is_mocked = 'v4' in sys.modules and isinstance(sys.modules['v4'], NonCallableMock)
4040
if _v4_is_mocked:
4141
# Skip this module - v4 has been mocked by another test file
4242
pytest.skip(

src/tests/backend/v4/orchestration/helper/test_plan_to_mplan_converter.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111

1212
import unittest
1313
import sys
14-
from unittest.mock import Mock, MagicMock
14+
from unittest.mock import NonCallableMock
1515

1616
import pytest
1717

1818
# Check if v4 has been mocked by another test file (prevents import errors)
19-
_v4_is_mocked = 'v4' in sys.modules and isinstance(sys.modules['v4'], (Mock, MagicMock))
20-
_v4_models_is_mocked = 'v4.models' in sys.modules and isinstance(sys.modules['v4.models'], (Mock, MagicMock))
19+
# Use NonCallableMock to catch all mock subclasses (Mock, MagicMock, etc.)
20+
_v4_is_mocked = 'v4' in sys.modules and isinstance(sys.modules['v4'], NonCallableMock)
21+
_v4_models_is_mocked = 'v4.models' in sys.modules and isinstance(sys.modules['v4.models'], NonCallableMock)
2122
if _v4_is_mocked or _v4_models_is_mocked:
2223
pytest.skip(
2324
"Skipping test_plan_to_mplan_converter.py: v4 module has been mocked by another test file. "

src/tests/backend/v4/orchestration/test_orchestration_manager.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -917,30 +917,30 @@ def test_extract_response_text_object_with_text_attr(self):
917917
def test_extract_response_text_object_with_empty_text(self):
918918
"""Test extracting text from object with empty text attribute."""
919919
# Use spec to ensure only specified attributes exist
920-
obj = Mock(spec=['text'])
920+
obj = Mock(spec_set=['text'])
921921
obj.text = ""
922922
result = self.manager._extract_response_text(obj)
923923
self.assertEqual(result, "")
924924

925925
def test_extract_response_text_agent_executor_response_with_agent_response(self):
926926
"""Test extracting text from AgentExecutorResponse with agent_response.text."""
927-
agent_resp = Mock()
927+
agent_resp = Mock(spec_set=['text'])
928928
agent_resp.text = "Agent executor response"
929929

930-
executor_resp = Mock(spec=['agent_response'])
930+
executor_resp = Mock(spec_set=['agent_response'])
931931
executor_resp.agent_response = agent_resp
932932

933933
result = self.manager._extract_response_text(executor_resp)
934934
self.assertEqual(result, "Agent executor response")
935935

936936
def test_extract_response_text_agent_executor_response_fallback_to_conversation(self):
937937
"""Test extracting text from AgentExecutorResponse falling back to full_conversation."""
938-
agent_resp = Mock()
938+
agent_resp = Mock(spec_set=['text'])
939939
agent_resp.text = None
940940

941941
last_msg = MockChatMessage("Last conversation message")
942942

943-
executor_resp = Mock(spec=['agent_response', 'full_conversation'])
943+
executor_resp = Mock(spec_set=['agent_response', 'full_conversation'])
944944
executor_resp.agent_response = agent_resp
945945
executor_resp.full_conversation = [MockChatMessage("First"), last_msg]
946946

@@ -949,10 +949,10 @@ def test_extract_response_text_agent_executor_response_fallback_to_conversation(
949949

950950
def test_extract_response_text_agent_executor_response_empty_conversation(self):
951951
"""Test extracting text from AgentExecutorResponse with empty conversation."""
952-
agent_resp = Mock()
952+
agent_resp = Mock(spec_set=['text'])
953953
agent_resp.text = None
954954

955-
executor_resp = Mock(spec=['agent_response', 'full_conversation'])
955+
executor_resp = Mock(spec_set=['agent_response', 'full_conversation'])
956956
executor_resp.agent_response = agent_resp
957957
executor_resp.full_conversation = []
958958

@@ -1091,7 +1091,7 @@ async def test_workflow_output_with_object_with_text(self):
10911091
mock_workflow = Mock()
10921092

10931093
# Create object with text attribute
1094-
obj_with_text = Mock(spec=['text'])
1094+
obj_with_text = Mock(spec_set=['text'])
10951095
obj_with_text.text = "Object response"
10961096
output_event = MockWorkflowOutputEvent(obj_with_text)
10971097

0 commit comments

Comments
 (0)