""" Integration tests for Data Cleaning MCP Server. Tests server initialization and basic functionality. """ import asyncio import pytest from unittest.mock import AsyncMock, patch from mcp_servers.data_cleaning.server import DataCleaningMCPServer from shared.models import UserContext class TestDataCleaningIntegration: """Integration test suite for Data Cleaning MCP Server.""" @pytest.mark.asyncio async def test_server_startup_and_tool_registration(self): """Test that server can start up and register tools properly.""" server = DataCleaningMCPServer() # Mock database initialization with patch.object(server, 'initialize_database') as mock_init_db: mock_init_db.return_value = None # Register tools await server.register_server_tools() # Verify all expected tools are registered expected_tools = [ "upload_dataset", "analyze_quality", "clean_dataset", "get_cleaning_summary" ] for tool_name in expected_tools: assert tool_name in server.tools, f"Tool {tool_name} not registered" assert tool_name in server.tool_schemas, f"Tool schema {tool_name} not registered" # Verify tool schemas have required structure for tool_name, schema in server.tool_schemas.items(): assert hasattr(schema, 'name') assert hasattr(schema, 'description') assert hasattr(schema, 'inputSchema') assert 'user_id' in schema.inputSchema['properties'] @pytest.mark.asyncio async def test_health_check(self): """Test server health check functionality.""" server = DataCleaningMCPServer() # Mock database mock_db = AsyncMock() mock_db.health_check.return_value = True server.db = mock_db # Register at least one tool await server.register_server_tools() # Perform health check health_status = await server.health_check() # Verify health status assert health_status.server_name == "data_cleaning" assert health_status.is_healthy == True assert health_status.response_time_ms >= 0 assert health_status.last_check is not None @pytest.mark.asyncio async def test_server_info(self): """Test server information retrieval.""" server = DataCleaningMCPServer() # Register tools await server.register_server_tools() # Get server info info = server.get_server_info() # Verify server info assert info['server_name'] == "data_cleaning" assert info['port'] == 3001 # Default port from config assert info['tools_count'] == 4 # Number of registered tools assert len(info['tools']) == 4 assert 'upload_dataset' in info['tools'] assert 'analyze_quality' in info['tools'] assert 'clean_dataset' in info['tools'] assert 'get_cleaning_summary' in info['tools'] @pytest.mark.asyncio async def test_user_context_extraction(self): """Test user context extraction from arguments.""" server = DataCleaningMCPServer() # Test valid user context arguments = { "user_id": "test_user_123", "user_email": "test@example.com", "user_permissions": ["read", "write"] } user_context = server._extract_user_context(arguments) assert user_context.user_id == "test_user_123" assert user_context.email == "test@example.com" assert user_context.permissions == ["read", "write"] @pytest.mark.asyncio async def test_user_context_extraction_missing_user_id(self): """Test user context extraction with missing user_id.""" server = DataCleaningMCPServer() # Test missing user_id arguments = { "user_email": "test@example.com" } with pytest.raises(Exception, match="user_id is required"): server._extract_user_context(arguments) @pytest.mark.asyncio async def test_parameter_validation(self): """Test parameter validation functionality.""" server = DataCleaningMCPServer() # Test valid parameters arguments = { "param1": "value1", "param2": "value2", "param3": "value3" } # Should not raise exception server.validate_required_params(arguments, ["param1", "param2"]) # Test missing parameters with pytest.raises(Exception, match="Missing required parameters"): server.validate_required_params(arguments, ["param1", "param4"]) @pytest.mark.asyncio async def test_input_sanitization(self): """Test input sanitization functionality.""" server = DataCleaningMCPServer() # Test string sanitization assert server.sanitize_input(" hello world ", "string") == "hello world" # Test integer sanitization assert server.sanitize_input("42", "integer") == 42 # Test float sanitization assert server.sanitize_input("3.14", "float") == 3.14 # Test boolean sanitization assert server.sanitize_input(1, "boolean") == True assert server.sanitize_input(0, "boolean") == False # Test None handling assert server.sanitize_input(None, "string") is None if __name__ == "__main__": pytest.main([__file__])