test_tools.py•2.75 kB
"""
Unit tests for tools.
"""
import pytest
from mcp_template.tools.calculator import calculator_handler
from mcp_template.tools.search import search_handler
@pytest.mark.asyncio
async def test_calculator_add():
"""Test calculator addition."""
result = await calculator_handler({
"operation": "add",
"a": 5,
"b": 3
})
assert "5.0 add 3.0 = 8.0" in result
@pytest.mark.asyncio
async def test_calculator_subtract():
"""Test calculator subtraction."""
result = await calculator_handler({
"operation": "subtract",
"a": 10,
"b": 4
})
assert "10.0 subtract 4.0 = 6.0" in result
@pytest.mark.asyncio
async def test_calculator_multiply():
"""Test calculator multiplication."""
result = await calculator_handler({
"operation": "multiply",
"a": 6,
"b": 7
})
assert "6.0 multiply 7.0 = 42.0" in result
@pytest.mark.asyncio
async def test_calculator_divide():
"""Test calculator division."""
result = await calculator_handler({
"operation": "divide",
"a": 15,
"b": 3
})
assert "15.0 divide 3.0 = 5.0" in result
@pytest.mark.asyncio
async def test_calculator_divide_by_zero():
"""Test calculator division by zero."""
result = await calculator_handler({
"operation": "divide",
"a": 10,
"b": 0
})
assert "Error: Division by zero" in result
@pytest.mark.asyncio
async def test_calculator_invalid_operation():
"""Test calculator with invalid operation."""
result = await calculator_handler({
"operation": "invalid",
"a": 5,
"b": 3
})
assert "Error: Unknown operation" in result
@pytest.mark.asyncio
async def test_search_with_results():
"""Test search with matching results."""
result = await search_handler({
"query": "python",
"limit": 10
})
assert "Found" in result
assert "Python Programming" in result
@pytest.mark.asyncio
async def test_search_no_results():
"""Test search with no matching results."""
result = await search_handler({
"query": "nonexistent",
"limit": 10
})
assert "No results found" in result
@pytest.mark.asyncio
async def test_search_with_limit():
"""Test search with result limit."""
result = await search_handler({
"query": "programming",
"limit": 1
})
# Should only return 1 result
assert result.count("[") == 1 # Count result items
@pytest.mark.asyncio
async def test_search_empty_query():
"""Test search with empty query."""
result = await search_handler({
"query": "",
"limit": 10
})
assert "Error: Query cannot be empty" in result