1
- # tests/test_server.py
2
1
import pytest
3
- from mysql_mcp_server .server import app
2
+ from mysql_mcp_server .server import app , list_tools , list_resources , read_resource , call_tool
3
+ from pydantic import AnyUrl
4
4
5
5
def test_server_initialization ():
6
6
"""Test that the server initializes correctly."""
@@ -9,6 +9,38 @@ def test_server_initialization():
9
9
@pytest .mark .asyncio
10
10
async def test_list_tools ():
11
11
"""Test that list_tools returns expected tools."""
12
- tools = await app . list_tools ()
12
+ tools = await list_tools ()
13
13
assert len (tools ) == 1
14
- assert tools [0 ].name == "execute_sql"
14
+ assert tools [0 ].name == "execute_sql"
15
+ assert "query" in tools [0 ].inputSchema ["properties" ]
16
+
17
+ @pytest .mark .asyncio
18
+ async def test_call_tool_invalid_name ():
19
+ """Test calling a tool with an invalid name."""
20
+ with pytest .raises (ValueError , match = "Unknown tool" ):
21
+ await call_tool ("invalid_tool" , {})
22
+
23
+ @pytest .mark .asyncio
24
+ async def test_call_tool_missing_query ():
25
+ """Test calling execute_sql without a query."""
26
+ with pytest .raises (ValueError , match = "Query is required" ):
27
+ await call_tool ("execute_sql" , {})
28
+
29
+ # Skip database-dependent tests if no database connection
30
+ @pytest .mark .asyncio
31
+ @pytest .mark .skipif (
32
+ not all ([
33
+ pytest .importorskip ("mysql.connector" ),
34
+ pytest .importorskip ("mysql_mcp_server" )
35
+ ]),
36
+ reason = "MySQL connection not available"
37
+ )
38
+ async def test_list_resources ():
39
+ """Test listing resources (requires database connection)."""
40
+ try :
41
+ resources = await list_resources ()
42
+ assert isinstance (resources , list )
43
+ except ValueError as e :
44
+ if "Missing required database configuration" in str (e ):
45
+ pytest .skip ("Database configuration not available" )
46
+ raise
0 commit comments