"""Unit tests for GetTablesService."""
import pytest
from mcp_sql.tools.get_tables import GetTablesService
@pytest.mark.asyncio
class TestGetTablesService:
"""Test cases for GetTablesService."""
async def test_name_property(self, tool_dependencies):
"""Test that the tool has the correct name."""
service = GetTablesService(**tool_dependencies)
assert service.name == "get_tables"
async def test_description_property(self, tool_dependencies):
"""Test that the tool has a description."""
service = GetTablesService(**tool_dependencies)
assert len(service.description) > 0
assert "table" in service.description.lower()
async def test_execute_returns_table_list(self, tool_dependencies, mock_context):
"""Test that execute returns a list of tables."""
service = GetTablesService(**tool_dependencies)
result = await service.execute(
mock_context,
database="test_db",
server_name="test_server"
)
assert isinstance(result, list)
assert len(result) == 3
assert "table1" in result
assert "table2" in result
assert "table3" in result
async def test_execute_without_database(self, tool_dependencies, mock_context, mock_credentials):
"""Test execute without database name."""
mock_credentials.database = None
service = GetTablesService(**tool_dependencies)
result = await service.execute(
mock_context,
server_name="test_server"
)
assert isinstance(result, list)
assert len(result) == 1
assert "Error: Database name is required" in result
async def test_execute_with_invalid_credentials(self, tool_dependencies_invalid, mock_context):
"""Test execute with invalid credentials."""
service = GetTablesService(**tool_dependencies_invalid)
result = await service.execute(
mock_context,
database="test_db"
)
assert isinstance(result, list)
assert len(result) == 1
assert "Error: Missing credentials" in result
async def test_execute_calls_inspector(self, tool_dependencies, mock_context):
"""Test that execute calls the database inspector."""
service = GetTablesService(**tool_dependencies)
await service.execute(
mock_context,
database="test_db",
server_name="test_server"
)
tool_dependencies["inspector"].get_tables.assert_called_once()