From f35305568ce6554aea1fcd7aadebe03409f7e79a Mon Sep 17 00:00:00 2001 From: Daniel Bauer Date: Wed, 9 Jul 2025 11:09:17 +0200 Subject: [PATCH] fix: typing errors in tests --- pyproject.toml | 1 + tests/test_client.py | 47 ++++++++++++------------- tests/test_server.py | 81 +++++++++++++++++++++++++------------------- 3 files changed, 72 insertions(+), 57 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 522ced7..09e5465 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -61,6 +61,7 @@ ignore = ["E501"] python_version = "3.11" strict = true warn_return_any = true +files = ["src", "tests"] [[tool.mypy.overrides]] module = ["cordra.*"] diff --git a/tests/test_client.py b/tests/test_client.py index 19caf82..0336e9e 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,5 +1,6 @@ """Unit tests for the Cordra client.""" +from typing import Any from unittest.mock import patch import pytest @@ -15,7 +16,7 @@ from cordra_mcp.config import CordraConfig @pytest.fixture -def config(): +def config() -> CordraConfig: """Create a test configuration.""" return CordraConfig( base_url="https://test.example.com", @@ -26,13 +27,13 @@ def config(): @pytest.fixture -def client(config): +def client(config: CordraConfig) -> CordraClient: """Create a test client.""" return CordraClient(config) @pytest.fixture -def mock_cordra_object(): +def mock_cordra_object() -> dict[str, Any]: """Create a mock CordraObject response (dictionary).""" return { "type": "TestType", @@ -59,7 +60,7 @@ def mock_cordra_object(): class TestDigitalObject: """Test the DigitalObject model.""" - def test_digital_object_creation(self): + def test_digital_object_creation(self) -> None: """Test creating a DigitalObject.""" obj = DigitalObject( id="test/123", @@ -89,7 +90,7 @@ class TestDigitalObject: assert payload["size"] == 1024 assert payload["filename"] == "file1.txt" - def test_digital_object_optional_fields(self): + def test_digital_object_optional_fields(self) -> None: """Test DigitalObject with only required fields.""" obj = DigitalObject(id="test/123", type="TestType", content={"title": "Test"}) @@ -104,13 +105,13 @@ class TestDigitalObject: class TestCordraClient: """Test the CordraClient class.""" - def test_client_initialization(self, config): + def test_client_initialization(self, config: CordraConfig) -> None: """Test client initialization.""" client = CordraClient(config) assert client.config == config @patch("cordra_mcp.client.requests.Session.get") - async def test_get_object_success(self, mock_get, client, mock_cordra_object): + async def test_get_object_success(self, mock_get: Any, client: CordraClient, mock_cordra_object: dict[str, Any]) -> None: """Test successful object retrieval.""" mock_response = mock_get.return_value mock_response.status_code = 200 @@ -137,7 +138,7 @@ class TestCordraClient: ) @patch("cordra_mcp.client.requests.Session.get") - async def test_get_object_not_found(self, mock_get, client): + async def test_get_object_not_found(self, mock_get: Any, client: CordraClient) -> None: """Test object not found exception.""" mock_response = mock_get.return_value mock_response.status_code = 404 @@ -149,7 +150,7 @@ class TestCordraClient: assert "Resource not found" in str(exc_info.value) @patch("cordra_mcp.client.requests.Session.get") - async def test_get_object_general_error(self, mock_get, client): + async def test_get_object_general_error(self, mock_get: Any, client: CordraClient) -> None: """Test general error handling.""" from requests import RequestException @@ -161,7 +162,7 @@ class TestCordraClient: assert "Failed to retrieve object test/123" in str(exc_info.value) @patch("cordra_mcp.client.requests.Session.get") - async def test_find_success(self, mock_get, client): + async def test_find_success(self, mock_get: Any, client: CordraClient) -> None: """Test successful find operation.""" mock_response_data = { "results": [ @@ -196,7 +197,7 @@ class TestCordraClient: ) @patch("cordra_mcp.client.requests.Session.get") - async def test_find_empty_results(self, mock_get, client): + async def test_find_empty_results(self, mock_get: Any, client: CordraClient) -> None: """Test find with empty results.""" mock_response_data = {"results": [], "size": 0, "pageNum": 0, "pageSize": 20} mock_response = mock_get.return_value @@ -218,7 +219,7 @@ class TestCordraClient: ) @patch("cordra_mcp.client.requests.Session.get") - async def test_find_error(self, mock_get, client): + async def test_find_error(self, mock_get: Any, client: CordraClient) -> None: """Test find error handling.""" from requests import RequestException @@ -231,7 +232,7 @@ class TestCordraClient: assert "Search failed" in str(exc_info.value) @patch("cordra_mcp.client.requests.Session.get") - async def test_find_with_type_filter(self, mock_get, client): + async def test_find_with_type_filter(self, mock_get: Any, client: CordraClient) -> None: """Test find operation with type filter constructs correct query.""" mock_response_data = {"results": [], "size": 0, "pageNum": 0, "pageSize": 20} mock_response = mock_get.return_value @@ -248,7 +249,7 @@ class TestCordraClient: ) @patch("cordra_mcp.client.requests.Session.get") - async def test_find_with_page_size(self, mock_get, client): + async def test_find_with_page_size(self, mock_get: Any, client: CordraClient) -> None: """Test find operation with custom page size.""" mock_response_data = {"results": [], "size": 0, "pageNum": 0, "pageSize": 50} mock_response = mock_get.return_value @@ -265,7 +266,7 @@ class TestCordraClient: ) @patch("cordra_mcp.client.requests.Session.get") - async def test_find_with_type_and_page_size(self, mock_get, client): + async def test_find_with_type_and_page_size(self, mock_get: Any, client: CordraClient) -> None: """Test find operation with both type filter and page size.""" mock_response_data = {"results": [], "size": 0, "pageNum": 0, "pageSize": 25} mock_response = mock_get.return_value @@ -282,7 +283,7 @@ class TestCordraClient: ) @patch("cordra_mcp.client.requests.Session.get") - async def test_find_default_params(self, mock_get, client): + async def test_find_default_params(self, mock_get: Any, client: CordraClient) -> None: """Test find operation with default parameters.""" mock_response_data = {"results": [], "size": 0, "pageNum": 0, "pageSize": 20} mock_response = mock_get.return_value @@ -299,7 +300,7 @@ class TestCordraClient: ) @patch("cordra_mcp.client.requests.Session.get") - async def test_find_with_page_num(self, mock_get, client): + async def test_find_with_page_num(self, mock_get: Any, client: CordraClient) -> None: """Test find operation with specific page number.""" mock_response_data = {"results": [], "size": 100, "pageNum": 2, "pageSize": 20} mock_response = mock_get.return_value @@ -319,7 +320,7 @@ class TestCordraClient: ) @patch("cordra_mcp.client.requests.Session.get") - async def test_find_with_custom_page_size_and_num(self, mock_get, client): + async def test_find_with_custom_page_size_and_num(self, mock_get: Any, client: CordraClient) -> None: """Test find operation with custom page size and page number.""" mock_response_data = {"results": [], "size": 500, "pageNum": 5, "pageSize": 10} mock_response = mock_get.return_value @@ -339,7 +340,7 @@ class TestCordraClient: ) @patch("cordra_mcp.client.requests.Session.get") - async def test_get_design_success(self, mock_get, client): + async def test_get_design_success(self, mock_get: Any, client: CordraClient) -> None: """Test successful design object retrieval.""" mock_design_data = { "type": "CordraDesign", @@ -368,7 +369,7 @@ class TestCordraClient: ) @patch("cordra_mcp.client.requests.Session.get") - async def test_get_design_authentication_error(self, mock_get, client): + async def test_get_design_authentication_error(self, mock_get: Any, client: CordraClient) -> None: """Test design object retrieval with authentication error.""" mock_response = mock_get.return_value mock_response.status_code = 403 @@ -380,7 +381,7 @@ class TestCordraClient: assert "Authentication failed" in str(exc_info.value) @patch("cordra_mcp.client.requests.Session.get") - async def test_get_design_not_found(self, mock_get, client): + async def test_get_design_not_found(self, mock_get: Any, client: CordraClient) -> None: """Test design object retrieval with not found error.""" mock_response = mock_get.return_value mock_response.status_code = 404 @@ -392,7 +393,7 @@ class TestCordraClient: assert "Resource not found" in str(exc_info.value) @patch("cordra_mcp.client.requests.Session.get") - async def test_get_design_request_error(self, mock_get, client): + async def test_get_design_request_error(self, mock_get: Any, client: CordraClient) -> None: """Test design object retrieval with request error.""" from requests import RequestException @@ -407,7 +408,7 @@ class TestCordraClient: class TestCordraConfig: """Test the CordraConfig class.""" - def test_default_config(self): + def test_default_config(self) -> None: """Test default configuration values.""" config = CordraConfig() assert config.base_url == "https://localhost:8443" diff --git a/tests/test_server.py b/tests/test_server.py index d9e2732..dc9472e 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -1,6 +1,7 @@ """Unit tests for the MCP server.""" import json +from typing import Any from unittest.mock import AsyncMock, patch import pytest @@ -20,7 +21,7 @@ from cordra_mcp.server import ( @pytest.fixture -def sample_digital_object(): +def sample_digital_object() -> DigitalObject: """Create a sample DigitalObject for testing.""" return DigitalObject( id="people/john-doe-123", @@ -47,7 +48,9 @@ class TestGetCordraObject: """Test the get_cordra_object resource handler.""" @patch("cordra_mcp.server.cordra_client") - async def test_get_object_success(self, mock_client, sample_digital_object): + async def test_get_object_success( + self, mock_client: Any, sample_digital_object: DigitalObject + ) -> None: """Test successful object retrieval.""" mock_client.get_object = AsyncMock(return_value=sample_digital_object) @@ -67,7 +70,7 @@ class TestGetCordraObject: mock_client.get_object.assert_called_once_with("people/john-doe-123") @patch("cordra_mcp.server.cordra_client") - async def test_get_object_not_found(self, mock_client): + async def test_get_object_not_found(self, mock_client: Any) -> None: """Test object not found exception.""" mock_client.get_object = AsyncMock( side_effect=CordraNotFoundError("Object not found: people/nonexistent") @@ -80,7 +83,7 @@ class TestGetCordraObject: mock_client.get_object.assert_called_once_with("people/nonexistent") @patch("cordra_mcp.server.cordra_client") - async def test_get_object_client_error(self, mock_client): + async def test_get_object_client_error(self, mock_client: Any) -> None: """Test general client error handling.""" mock_client.get_object = AsyncMock( side_effect=CordraClientError("Connection failed") @@ -94,7 +97,9 @@ class TestGetCordraObject: mock_client.get_object.assert_called_once_with("people/john-doe-123") @patch("cordra_mcp.server.cordra_client") - async def test_object_id_construction(self, mock_client, sample_digital_object): + async def test_object_id_construction( + self, mock_client: Any, sample_digital_object: DigitalObject + ) -> None: """Test that object ID is correctly constructed from prefix and suffix.""" mock_client.get_object = AsyncMock(return_value=sample_digital_object) @@ -110,7 +115,9 @@ class TestGetCordraObject: mock_client.get_object.assert_called_with(expected_id) @patch("cordra_mcp.server.cordra_client") - async def test_json_formatting(self, mock_client, sample_digital_object): + async def test_json_formatting( + self, mock_client: Any, sample_digital_object: DigitalObject + ) -> None: """Test that the returned JSON is properly formatted.""" mock_client.get_object = AsyncMock(return_value=sample_digital_object) @@ -132,7 +139,7 @@ class TestGetCordraObject: assert "payloads" in parsed_result @patch("cordra_mcp.server.cordra_client") - async def test_minimal_object(self, mock_client): + async def test_minimal_object(self, mock_client: Any) -> None: """Test handling of object with minimal data.""" minimal_object = DigitalObject( id="test/minimal", @@ -159,7 +166,7 @@ class TestSchemaResourceFunctions: """Test the schema resource functions.""" @patch("cordra_mcp.server.cordra_client") - async def test_create_schema_resource_success(self, mock_client): + async def test_create_schema_resource_success(self, mock_client: Any) -> None: """Test successful schema resource creation.""" mock_schema = DigitalObject( id="test/user-schema", @@ -182,7 +189,7 @@ class TestSchemaResourceFunctions: mock_client.get_schema.assert_called_once_with("User") @patch("cordra_mcp.server.cordra_client") - async def test_create_schema_resource_not_found(self, mock_client): + async def test_create_schema_resource_not_found(self, mock_client: Any) -> None: """Test schema resource creation with schema not found.""" mock_client.get_schema = AsyncMock( side_effect=CordraNotFoundError("Schema not found") @@ -197,7 +204,7 @@ class TestSchemaResourceFunctions: mock_client.get_schema.assert_called_once_with("NonExistent") @patch("cordra_mcp.server.cordra_client") - async def test_register_schema_resources_success(self, mock_client): + async def test_register_schema_resources_success(self, mock_client: Any) -> None: """Test successful schema resource registration.""" mock_search_result = { "results": [ @@ -226,7 +233,9 @@ class TestSchemaResourceFunctions: assert mock_mcp.add_resource.call_count == 3 @patch("cordra_mcp.server.cordra_client") - async def test_register_schema_resources_missing_name(self, mock_client): + async def test_register_schema_resources_missing_name( + self, mock_client: Any + ) -> None: """Test schema resource registration with objects missing name field.""" mock_search_result = { "results": [ @@ -249,7 +258,9 @@ class TestSchemaResourceFunctions: assert mock_mcp.add_resource.call_count == 2 @patch("cordra_mcp.server.cordra_client") - async def test_register_schema_resources_client_error(self, mock_client): + async def test_register_schema_resources_client_error( + self, mock_client: Any + ) -> None: """Test schema resource registration with client error.""" mock_client.find = AsyncMock(side_effect=CordraClientError("Search failed")) @@ -263,7 +274,7 @@ class TestSchemaResourceFunctions: ) @patch("cordra_mcp.server.cordra_client") - async def test_register_schema_resources_pagination(self, mock_client): + async def test_register_schema_resources_pagination(self, mock_client: Any) -> None: """Test schema resource registration with pagination.""" # Mock multiple pages of results # First page with full 20 results (simulating more schemas) @@ -309,7 +320,7 @@ class TestSearchObjects: """Test the search_objects tool.""" @patch("cordra_mcp.server.cordra_client") - async def test_search_objects_success(self, mock_client): + async def test_search_objects_success(self, mock_client: Any) -> None: """Test successful object search.""" mock_search_result = { "results": [ @@ -345,7 +356,7 @@ class TestSearchObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_search_objects_with_type_filter(self, mock_client): + async def test_search_objects_with_type_filter(self, mock_client: Any) -> None: """Test object search with type filter.""" mock_search_result = { "results": [ @@ -374,7 +385,7 @@ class TestSearchObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_search_objects_with_limit(self, mock_client): + async def test_search_objects_with_limit(self, mock_client: Any) -> None: """Test object search with custom limit.""" mock_search_result = { "results": [ @@ -403,7 +414,7 @@ class TestSearchObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_search_objects_with_all_parameters(self, mock_client): + async def test_search_objects_with_all_parameters(self, mock_client: Any) -> None: """Test object search with all parameters.""" mock_search_result = { "results": [ @@ -432,7 +443,7 @@ class TestSearchObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_search_objects_empty_results(self, mock_client): + async def test_search_objects_empty_results(self, mock_client: Any) -> None: """Test object search with no results.""" mock_search_result = { "results": [], @@ -454,7 +465,7 @@ class TestSearchObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_search_objects_client_error(self, mock_client): + async def test_search_objects_client_error(self, mock_client: Any) -> None: """Test object search with client error.""" mock_client.find = AsyncMock(side_effect=CordraClientError("Search failed")) @@ -467,7 +478,7 @@ class TestSearchObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_search_objects_value_error(self, mock_client): + async def test_search_objects_value_error(self, mock_client: Any) -> None: """Test object search with value error.""" mock_client.find = AsyncMock(side_effect=ValueError("Invalid query")) @@ -480,7 +491,7 @@ class TestSearchObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_search_objects_json_formatting(self, mock_client): + async def test_search_objects_json_formatting(self, mock_client: Any) -> None: """Test that search results are properly formatted as JSON.""" mock_search_result = { "results": [ @@ -506,7 +517,7 @@ class TestSearchObjects: assert parsed_result["total_count"] == 1 @patch("cordra_mcp.server.cordra_client") - async def test_search_objects_with_page_num(self, mock_client): + async def test_search_objects_with_page_num(self, mock_client: Any) -> None: """Test object search with page number parameter.""" mock_search_result = { "results": [ @@ -530,7 +541,9 @@ class TestSearchObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_search_objects_with_all_pagination_params(self, mock_client): + async def test_search_objects_with_all_pagination_params( + self, mock_client: Any + ) -> None: """Test object search with all pagination parameters.""" mock_search_result = { "results": [ @@ -558,7 +571,7 @@ class TestGetCordraDesign: """Test the get_cordra_design resource handler.""" @patch("cordra_mcp.server.cordra_client") - async def test_get_design_success(self, mock_client): + async def test_get_design_success(self, mock_client: Any) -> None: """Test successful design object retrieval.""" mock_design = DigitalObject( id="design", @@ -586,7 +599,7 @@ class TestGetCordraDesign: mock_client.get_design.assert_called_once() @patch("cordra_mcp.server.cordra_client") - async def test_get_design_not_found(self, mock_client): + async def test_get_design_not_found(self, mock_client: Any) -> None: """Test design object not found exception.""" mock_client.get_design = AsyncMock( side_effect=CordraNotFoundError("Design object not found") @@ -599,7 +612,7 @@ class TestGetCordraDesign: mock_client.get_design.assert_called_once() @patch("cordra_mcp.server.cordra_client") - async def test_get_design_authentication_error(self, mock_client): + async def test_get_design_authentication_error(self, mock_client: Any) -> None: """Test design object authentication error.""" mock_client.get_design = AsyncMock( side_effect=CordraAuthenticationError("Authentication failed") @@ -612,7 +625,7 @@ class TestGetCordraDesign: mock_client.get_design.assert_called_once() @patch("cordra_mcp.server.cordra_client") - async def test_get_design_client_error(self, mock_client): + async def test_get_design_client_error(self, mock_client: Any) -> None: """Test design object general client error.""" mock_client.get_design = AsyncMock( side_effect=CordraClientError("Connection failed") @@ -626,7 +639,7 @@ class TestGetCordraDesign: mock_client.get_design.assert_called_once() @patch("cordra_mcp.server.cordra_client") - async def test_get_design_json_formatting(self, mock_client): + async def test_get_design_json_formatting(self, mock_client: Any) -> None: """Test that the design object is properly formatted as JSON.""" mock_design = DigitalObject( id="design", @@ -656,7 +669,7 @@ class TestCountObjects: """Test the count_objects tool.""" @patch("cordra_mcp.server.cordra_client") - async def test_count_objects_success(self, mock_client): + async def test_count_objects_success(self, mock_client: Any) -> None: """Test successful object count.""" mock_search_result = { "results": [{"id": "people/john-doe", "type": "Person"}], @@ -677,7 +690,7 @@ class TestCountObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_count_objects_with_type_filter(self, mock_client): + async def test_count_objects_with_type_filter(self, mock_client: Any) -> None: """Test object count with type filter.""" mock_search_result = { "results": [{"id": "people/john-doe", "type": "Person"}], @@ -698,7 +711,7 @@ class TestCountObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_count_objects_zero_results(self, mock_client): + async def test_count_objects_zero_results(self, mock_client: Any) -> None: """Test object count with zero results.""" mock_search_result = { "results": [], @@ -718,7 +731,7 @@ class TestCountObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_count_objects_client_error(self, mock_client): + async def test_count_objects_client_error(self, mock_client: Any) -> None: """Test object count with client error.""" mock_client.find = AsyncMock(side_effect=CordraClientError("Search failed")) @@ -731,7 +744,7 @@ class TestCountObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_count_objects_value_error(self, mock_client): + async def test_count_objects_value_error(self, mock_client: Any) -> None: """Test object count with value error.""" mock_client.find = AsyncMock(side_effect=ValueError("Invalid query")) @@ -744,7 +757,7 @@ class TestCountObjects: ) @patch("cordra_mcp.server.cordra_client") - async def test_count_objects_authentication_error(self, mock_client): + async def test_count_objects_authentication_error(self, mock_client: Any) -> None: """Test object count with authentication error.""" mock_client.find = AsyncMock( side_effect=CordraAuthenticationError("Authentication failed")