diff options
| author | Dawid Rycerz <dawid@rycerz.xyz> | 2025-03-28 20:53:41 +0100 |
|---|---|---|
| committer | Dawid Rycerz <dawid@rycerz.xyz> | 2025-03-28 20:53:41 +0100 |
| commit | 1745749cd2745c94c3f852e9c02dfde19d8d9c20 (patch) | |
| tree | 8ed13f3de5fac78d804124e27fbcf1b678e83253 /servers/hello_world/tests/test_handlers.py | |
Fix ruff errors and warnings in hello_world service
Diffstat (limited to 'servers/hello_world/tests/test_handlers.py')
| -rw-r--r-- | servers/hello_world/tests/test_handlers.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/servers/hello_world/tests/test_handlers.py b/servers/hello_world/tests/test_handlers.py new file mode 100644 index 0000000..0c0a45d --- /dev/null +++ b/servers/hello_world/tests/test_handlers.py @@ -0,0 +1,65 @@ +import pytest +import asyncio +import sys +from unittest.mock import AsyncMock, MagicMock, patch +from pydantic import AnyUrl +import mcp.types as types +from mcp_server_hello_world.server import HelloWorldServer, create_server + +class TestHandlers: + """Test the MCP server handlers.""" + + @pytest.mark.asyncio + async def test_resource_handler(self): + """Test the resource handler.""" + # Create a server + mcp = create_server() + + # Get the resource URI + uri = "hello://welcome" + + # Call the resource handler + result = await mcp.read_resource(uri) + + # Verify the result + assert len(result) == 1 + assert result[0].content == "Welcome to the Hello World MCP Server! This is a simple static resource." + + @pytest.mark.asyncio + async def test_tool_handler(self): + """Test the tool handler.""" + # Create a server + mcp = create_server() + + # Call the tool handler + result = await mcp.call_tool("hello", {"name": "Test User"}) + + # Verify the result + assert len(result) == 1 + assert result[0].text == "Hello, Test User! Welcome to the Hello World MCP Server." + + # Test with missing name argument + with pytest.raises(Exception) as excinfo: + await mcp.call_tool("hello", {}) + assert "Error executing tool hello" in str(excinfo.value) + assert "Field required" in str(excinfo.value) + + # Test with unknown tool + with pytest.raises(Exception) as excinfo: + await mcp.call_tool("unknown", {}) + assert "Unknown tool: unknown" in str(excinfo.value) + + @pytest.mark.asyncio + async def test_prompt_handler(self): + """Test the prompt handler.""" + # Create a server + mcp = create_server() + + # Call the prompt handler + result = await mcp.get_prompt("greeting", {"name": "Test User"}) + + # Verify the result + assert len(result.messages) > 0 + assert result.messages[0].role == "user" + assert result.messages[0].content.type == "text" + assert "Hello there! You've chosen to use the greeting prompt with the name: Test User." in result.messages[0].content.text |
