"""Unit tests for DescribeTableService."""
import pytest
from mcp_sql.tools.describe_table import DescribeTableService
@pytest.mark.asyncio
class TestDescribeTableService:
"""Test cases for DescribeTableService."""
async def test_name_property(self, tool_dependencies):
"""Test that the tool has the correct name."""
service = DescribeTableService(**tool_dependencies)
assert service.name == "describe_table"
async def test_description_property(self, tool_dependencies):
"""Test that the tool has a description."""
service = DescribeTableService(**tool_dependencies)
assert len(service.description) > 0
assert "table" in service.description.lower()
async def test_execute_returns_table_description(self, tool_dependencies, mock_context):
"""Test that execute returns table description."""
service = DescribeTableService(**tool_dependencies)
result = await service.execute(
mock_context,
table="test_table",
database="test_db",
server_name="test_server"
)
assert isinstance(result, dict)
assert "table_name" in result
assert "columns" in result
assert "primary_key" in result
assert "indexes" in result
assert "row_count" in result
assert result["table_name"] == "test_table"
assert len(result["columns"]) == 3
async def test_execute_without_database(self, tool_dependencies, mock_context, mock_credentials):
"""Test execute without database name."""
mock_credentials.database = None
service = DescribeTableService(**tool_dependencies)
result = await service.execute(
mock_context,
table="test_table",
server_name="test_server"
)
assert isinstance(result, dict)
assert "error" in result
assert "Database and table names are required" in result["error"]
async def test_execute_without_table(self, tool_dependencies, mock_context):
"""Test execute without table name."""
service = DescribeTableService(**tool_dependencies)
result = await service.execute(
mock_context,
table="",
database="test_db",
server_name="test_server"
)
assert isinstance(result, dict)
assert "error" in result
async def test_execute_with_invalid_credentials(self, tool_dependencies_invalid, mock_context):
"""Test execute with invalid credentials."""
service = DescribeTableService(**tool_dependencies_invalid)
result = await service.execute(
mock_context,
table="test_table",
database="test_db"
)
assert isinstance(result, dict)
assert "error" in result
assert "Missing credentials" in result["error"]
async def test_execute_calls_inspector(self, tool_dependencies, mock_context):
"""Test that execute calls the database inspector."""
service = DescribeTableService(**tool_dependencies)
await service.execute(
mock_context,
table="test_table",
database="test_db",
server_name="test_server"
)
tool_dependencies["inspector"].describe_table.assert_called_once()