1010def mock_client ():
1111 """Create a mock API client."""
1212 client = AsyncMock ()
13- client .list_items = AsyncMock (return_value = [
14- {"id" : "1" , "name" : "Item 1" },
15- {"id" : "2" , "name" : "Item 2" },
16- ])
17- client .get_item = AsyncMock (return_value = {
18- "id" : "1" ,
19- "name" : "Item 1" ,
20- "description" : "Test item" ,
21- })
13+ client .list_items = AsyncMock (
14+ return_value = [
15+ {"id" : "1" , "name" : "Item 1" },
16+ {"id" : "2" , "name" : "Item 2" },
17+ ]
18+ )
19+ client .get_item = AsyncMock (
20+ return_value = {
21+ "id" : "1" ,
22+ "name" : "Item 1" ,
23+ "description" : "Test item" ,
24+ }
25+ )
2226 return client
2327
2428
@@ -27,6 +31,7 @@ async def test_list_items(mock_client):
2731 """Test list_items tool."""
2832 with patch ("mcp_example.server.get_client" , return_value = mock_client ):
2933 from mcp_example .server import list_items
34+
3035 result = await list_items (limit = 10 )
3136 assert len (result ) == 2
3237 mock_client .list_items .assert_called_once_with (limit = 10 )
@@ -37,6 +42,7 @@ async def test_get_item(mock_client):
3742 """Test get_item tool."""
3843 with patch ("mcp_example.server.get_client" , return_value = mock_client ):
3944 from mcp_example .server import get_item
45+
4046 result = await get_item (item_id = "1" )
4147 assert result ["id" ] == "1"
4248 mock_client .get_item .assert_called_once_with ("1" )
@@ -45,10 +51,9 @@ async def test_get_item(mock_client):
4551@pytest .mark .asyncio
4652async def test_list_items_api_error (mock_client ):
4753 """Test list_items handles API errors."""
48- mock_client .list_items = AsyncMock (
49- side_effect = ExampleAPIError (401 , "Unauthorized" )
50- )
54+ mock_client .list_items = AsyncMock (side_effect = ExampleAPIError (401 , "Unauthorized" ))
5155 with patch ("mcp_example.server.get_client" , return_value = mock_client ):
5256 from mcp_example .server import list_items
57+
5358 with pytest .raises (ExampleAPIError ):
5459 await list_items ()
0 commit comments