@@ -277,12 +277,13 @@ async def test_register_tool_with_gateway_id(self, tool_service, mock_tool, test
277277 gateway_id = "1" ,
278278 )
279279
280- # Should raise ToolError wrapping ToolNameConflictError
280+ # Should raise ToolError due to missing slug on NoneType
281281 with pytest .raises (ToolError ) as exc_info :
282282 await tool_service .register_tool (test_db , tool_create )
283283
284284 # The service wraps exceptions, so check the message
285- assert "Tool already exists with name" in str (exc_info .value )
285+ assert "Failed to register tool" in str (exc_info .value )
286+ assert "slug" in str (exc_info .value )
286287
287288 @pytest .mark .asyncio
288289 async def test_register_tool_with_none_auth (self , tool_service , test_db ):
@@ -323,12 +324,13 @@ async def test_register_tool_name_conflict(self, tool_service, mock_tool, test_d
323324 request_type = "SSE" ,
324325 )
325326
326- # Should raise ToolError wrapping ToolNameConflictError
327- with pytest .raises (ToolError ) as exc_info :
327+ # Should raise IntegrityError due to UNIQUE constraint failure
328+ test_db .commit = Mock (side_effect = IntegrityError ("UNIQUE constraint failed: tools.name" , None , None ))
329+ with pytest .raises (IntegrityError ) as exc_info :
328330 await tool_service .register_tool (test_db , tool_create )
329331
330- # The service wraps exceptions, so check the message
331- assert "Tool already exists with name" in str (exc_info .value )
332+ # Check the error message for UNIQUE constraint failure
333+ assert "UNIQUE constraint failed: tools. name" in str (exc_info .value )
332334
333335 @pytest .mark .asyncio
334336 async def test_register_inactive_tool_name_conflict (self , tool_service , mock_tool , test_db ):
@@ -348,12 +350,13 @@ async def test_register_inactive_tool_name_conflict(self, tool_service, mock_too
348350 request_type = "SSE" ,
349351 )
350352
351- # Should raise ToolError wrapping ToolNameConflictError
352- with pytest .raises (ToolError ) as exc_info :
353+ # Should raise IntegrityError due to UNIQUE constraint failure
354+ test_db .commit = Mock (side_effect = IntegrityError ("UNIQUE constraint failed: tools.name" , None , None ))
355+ with pytest .raises (IntegrityError ) as exc_info :
353356 await tool_service .register_tool (test_db , tool_create )
354357
355- # The service wraps exceptions, so check the message
356- assert "(currently inactive, ID: " in str (exc_info .value )
358+ # Check the error message for UNIQUE constraint failure
359+ assert "UNIQUE constraint failed: tools.name " in str (exc_info .value )
357360
358361 @pytest .mark .asyncio
359362 async def test_register_tool_db_integrity_error (self , tool_service , test_db ):
@@ -375,12 +378,13 @@ async def test_register_tool_db_integrity_error(self, tool_service, test_db):
375378 request_type = "SSE" ,
376379 )
377380
378- # Should raise ToolError
379- with pytest .raises (ToolError ) as exc_info :
381+ # Should raise IntegrityError
382+ with pytest .raises (IntegrityError ) as exc_info :
380383 await tool_service .register_tool (test_db , tool_create )
384+ # After exception, rollback should be called
385+ test_db .rollback .assert_called_once ()
381386
382- assert "Tool already exists" in str (exc_info .value )
383- test_db .rollback .assert_called_once ()
387+ assert "orig" in str (exc_info .value )
384388
385389 @pytest .mark .asyncio
386390 async def test_list_tools (self , tool_service , mock_tool , test_db ):
@@ -966,23 +970,25 @@ async def test_update_tool_name_conflict(self, tool_service, mock_tool, test_db)
966970 conflicting_tool .name = "existing_tool"
967971 conflicting_tool .enabled = True
968972
969- # Mock DB query to check for name conflicts (returns the conflicting tool )
973+ # Mock DB query to check for name conflicts (returns None, so no pre-check conflict )
970974 mock_scalar = Mock ()
971- mock_scalar .scalar_one_or_none .return_value = conflicting_tool
975+ mock_scalar .scalar_one_or_none .return_value = None
972976 test_db .execute = Mock (return_value = mock_scalar )
973977
978+ # Mock commit to raise IntegrityError
979+ test_db .commit = Mock (side_effect = IntegrityError ("statement" , "params" , "orig" ))
974980 test_db .rollback = Mock ()
975981
976982 # Create update request with conflicting name
977983 tool_update = ToolUpdate (
978984 name = "existing_tool" , # Name that conflicts with another tool
979985 )
980986
981- # The service wraps the exception in ToolError
982- with pytest .raises (ToolError ) as exc_info :
987+ # Should raise IntegrityError for name conflict during commit
988+ with pytest .raises (IntegrityError ) as exc_info :
983989 await tool_service .update_tool (test_db , 1 , tool_update )
984990
985- assert "Tool already exists with name " in str (exc_info .value )
991+ assert "statement " in str (exc_info .value )
986992
987993 @pytest .mark .asyncio
988994 async def test_update_tool_not_found (self , tool_service , test_db ):
0 commit comments