@@ -71,15 +71,14 @@ async def test_initialize_success(self):
7171 mock_nim_client = AsyncMock ()
7272 mock_db_service = AsyncMock ()
7373
74- # Import the module first to ensure it's available for patching
75- try :
76- import src .api .services .document
77- except ImportError :
78- pass # Module might not be importable in test environment
79-
80- # Patch the module import - need to patch where it's imported from
81- with patch ("src.api.agents.document.action_tools.get_nim_client" , return_value = mock_nim_client ), \
82- patch ("src.api.services.document.get_document_db_service" , return_value = mock_db_service ), \
74+ # Create a mock module for src.api.services.document
75+ mock_document_module = MagicMock ()
76+ mock_document_module .get_document_db_service = AsyncMock (return_value = mock_db_service )
77+
78+ # Patch sys.modules to include the mock module
79+ import sys
80+ with patch .dict (sys .modules , {"src.api.services.document" : mock_document_module }), \
81+ patch ("src.api.agents.document.action_tools.get_nim_client" , return_value = mock_nim_client ), \
8382 patch .object (tools , "_load_status_data" ):
8483
8584 await tools .initialize ()
@@ -95,15 +94,14 @@ async def test_initialize_without_database_fallback(self):
9594
9695 mock_nim_client = AsyncMock ()
9796
98- # Import the module first to ensure it's available for patching
99- try :
100- import src .api .services .document
101- except ImportError :
102- pass # Module might not be importable in test environment
97+ # Create a mock module that raises an exception
98+ mock_document_module = MagicMock ()
99+ mock_document_module .get_document_db_service = AsyncMock (side_effect = Exception ("DB unavailable" ))
103100
104- # Patch the module import - need to patch where it's imported from
105- with patch ("src.api.agents.document.action_tools.get_nim_client" , return_value = mock_nim_client ), \
106- patch ("src.api.services.document.get_document_db_service" , side_effect = Exception ("DB unavailable" )), \
101+ # Patch sys.modules to include the mock module
102+ import sys
103+ with patch .dict (sys .modules , {"src.api.services.document" : mock_document_module }), \
104+ patch ("src.api.agents.document.action_tools.get_nim_client" , return_value = mock_nim_client ), \
107105 patch .object (tools , "_load_status_data" ):
108106
109107 await tools .initialize ()
0 commit comments