fix: linter and unit tests

This commit is contained in:
Daniel Bauer
2025-06-30 11:44:25 +02:00
parent f34d3e6e7c
commit 9901f51f06
4 changed files with 130 additions and 146 deletions

View File

@@ -42,14 +42,14 @@ class CordraClient:
def __init__(self, config: CordraConfig) -> None: def __init__(self, config: CordraConfig) -> None:
"""Initialize the Cordra client. """Initialize the Cordra client.
Args: Args:
config: Configuration settings for the Cordra connection config: Configuration settings for the Cordra connection
""" """
self.config = config self.config = config
self.session = requests.Session() self.session = requests.Session()
self.session.verify = config.verify_ssl self.session.verify = config.verify_ssl
# Set up authentication # Set up authentication
if config.username and config.password: if config.username and config.password:
self.session.auth = (config.username, config.password) self.session.auth = (config.username, config.password)
@@ -58,14 +58,14 @@ class CordraClient:
def _handle_http_error(self, response: requests.Response, context: str) -> None: def _handle_http_error(self, response: requests.Response, context: str) -> None:
"""Handle HTTP errors and raise appropriate exceptions. """Handle HTTP errors and raise appropriate exceptions.
Args: Args:
response: The HTTP response object response: The HTTP response object
context: Context description for the error message context: Context description for the error message
Raises: Raises:
CordraNotFoundError: For 404 errors CordraNotFoundError: For 404 errors
CordraAuthenticationError: For 401/403 errors CordraAuthenticationError: For 401/403 errors
CordraClientError: For other HTTP errors CordraClientError: For other HTTP errors
""" """
status_code = response.status_code status_code = response.status_code
@@ -80,13 +80,13 @@ class CordraClient:
async def get_object(self, object_id: str) -> DigitalObject: async def get_object(self, object_id: str) -> DigitalObject:
"""Retrieve a digital object by its ID. """Retrieve a digital object by its ID.
Args: Args:
object_id: The unique identifier of the object to retrieve object_id: The unique identifier of the object to retrieve
Returns: Returns:
The full digital object The full digital object
Raises: Raises:
ValueError: If object_id is empty ValueError: If object_id is empty
CordraNotFoundError: If the object is not found CordraNotFoundError: If the object is not found
@@ -95,15 +95,15 @@ class CordraClient:
""" """
url = f"{self.config.cordra_url}/objects/{object_id}" url = f"{self.config.cordra_url}/objects/{object_id}"
params = {"full": "true"} params = {"full": "true"}
try: try:
response = self.session.get(url, params=params, timeout=self.config.timeout) response = self.session.get(url, params=params, timeout=self.config.timeout)
if not response.ok: if not response.ok:
self._handle_http_error(response, f"Failed to retrieve object {object_id}") self._handle_http_error(response, f"Failed to retrieve object {object_id}")
cordra_obj = response.json() cordra_obj = response.json()
return DigitalObject( return DigitalObject(
id=object_id, id=object_id,
type=cordra_obj.get('type', ''), type=cordra_obj.get('type', ''),
@@ -112,19 +112,19 @@ class CordraClient:
acl=cordra_obj.get('acl'), acl=cordra_obj.get('acl'),
payloads=cordra_obj.get('payloads'), payloads=cordra_obj.get('payloads'),
) )
except requests.RequestException as e: except requests.RequestException as e:
raise CordraClientError(f"Failed to retrieve object {object_id}: {e}") from e raise CordraClientError(f"Failed to retrieve object {object_id}: {e}") from e
async def find(self, query: str) -> list[dict[str, Any]]: async def find(self, query: str) -> list[dict[str, Any]]:
"""Find objects using a Cordra query. """Find objects using a Cordra query.
Args: Args:
query: The query string to search for objects query: The query string to search for objects
Returns: Returns:
List of objects matching the query as dictionaries List of objects matching the query as dictionaries
Raises: Raises:
ValueError: If query is empty ValueError: If query is empty
CordraAuthenticationError: If authentication fails CordraAuthenticationError: If authentication fails
@@ -132,33 +132,33 @@ class CordraClient:
""" """
url = f"{self.config.cordra_url}/search" url = f"{self.config.cordra_url}/search"
params = {"query": query} params = {"query": query}
try: try:
response = self.session.get(url, params=params, timeout=self.config.timeout) response = self.session.get(url, params=params, timeout=self.config.timeout)
if not response.ok: if not response.ok:
self._handle_http_error(response, f"Failed to search with query '{query}'") self._handle_http_error(response, f"Failed to search with query '{query}'")
search_result = response.json() search_result = response.json()
# Extract the results array from the response # Extract the results array from the response
if isinstance(search_result, dict) and 'results' in search_result: if isinstance(search_result, dict) and 'results' in search_result:
return search_result['results'] return search_result['results']
else: else:
return [] return []
except requests.RequestException as e: except requests.RequestException as e:
raise CordraClientError(f"Failed to search with query '{query}': {e}") from e raise CordraClientError(f"Failed to search with query '{query}': {e}") from e
async def get_schema(self, schema_name: str) -> DigitalObject: async def get_schema(self, schema_name: str) -> DigitalObject:
"""Retrieve a schema definition by its name. """Retrieve a schema definition by its name.
Args: Args:
schema_name: The name of the schema to retrieve schema_name: The name of the schema to retrieve
Returns: Returns:
The schema object containing the type definition The schema object containing the type definition
Raises: Raises:
CordraNotFoundError: If the schema is not found CordraNotFoundError: If the schema is not found
CordraAuthenticationError: If authentication fails CordraAuthenticationError: If authentication fails
@@ -166,20 +166,20 @@ class CordraClient:
""" """
# Search for the specific schema by name using correct query format # Search for the specific schema by name using correct query format
query = f"type:Schema AND /name:{schema_name}" query = f"type:Schema AND /name:{schema_name}"
try: try:
schemas = await self.find(query) schemas = await self.find(query)
if not schemas: if not schemas:
raise CordraNotFoundError(f"Schema '{schema_name}' not found") raise CordraNotFoundError(f"Schema '{schema_name}' not found")
# Get the first matching schema (should be unique by name) # Get the first matching schema (should be unique by name)
schema_data = schemas[0] schema_data = schemas[0]
# Get the full schema object using its ID # Get the full schema object using its ID
return await self.get_object(schema_data['id']) return await self.get_object(schema_data['id'])
except (CordraNotFoundError, CordraAuthenticationError): except (CordraNotFoundError, CordraAuthenticationError):
raise raise
except Exception as e: except Exception as e:
raise CordraClientError(f"Failed to retrieve schema '{schema_name}': {e}") from e raise CordraClientError(f"Failed to retrieve schema '{schema_name}': {e}") from e

View File

@@ -8,10 +8,10 @@ from mcp.server.fastmcp import FastMCP
from mcp.server.fastmcp.resources import FunctionResource from mcp.server.fastmcp.resources import FunctionResource
from .client import ( from .client import (
CordraAuthenticationError,
CordraClient, CordraClient,
CordraClientError, CordraClientError,
CordraNotFoundError, CordraNotFoundError,
CordraAuthenticationError,
) )
from .config import CordraConfig from .config import CordraConfig
@@ -52,8 +52,8 @@ async def get_cordra_object(prefix: str, suffix: str) -> str:
except ValueError as e: except ValueError as e:
raise RuntimeError(f"Invalid parameters: {e}") from e raise RuntimeError(f"Invalid parameters: {e}") from e
except CordraNotFoundError: except CordraNotFoundError as e:
raise RuntimeError(f"Object not found: {object_id}") raise RuntimeError(f"Object not found: {object_id}") from e
except CordraAuthenticationError as e: except CordraAuthenticationError as e:
raise RuntimeError(f"Authentication failed: {e}") from e raise RuntimeError(f"Authentication failed: {e}") from e
except CordraClientError as e: except CordraClientError as e:
@@ -66,8 +66,8 @@ async def create_schema_resource(schema_name: str) -> str:
schema_object = await cordra_client.get_schema(schema_name) schema_object = await cordra_client.get_schema(schema_name)
schema_dict = schema_object.model_dump() schema_dict = schema_object.model_dump()
return json.dumps(schema_dict, indent=2) return json.dumps(schema_dict, indent=2)
except CordraNotFoundError: except CordraNotFoundError as e:
raise RuntimeError(f"Schema not found: {schema_name}") raise RuntimeError(f"Schema not found: {schema_name}") from e
except CordraAuthenticationError as e: except CordraAuthenticationError as e:
raise RuntimeError(f"Authentication failed: {e}") from e raise RuntimeError(f"Authentication failed: {e}") from e
except CordraClientError as e: except CordraClientError as e:

View File

@@ -8,7 +8,6 @@ from mcp_cordra.client import (
CordraClient, CordraClient,
CordraClientError, CordraClientError,
CordraNotFoundError, CordraNotFoundError,
CordraAuthenticationError,
DigitalObject, DigitalObject,
) )
from mcp_cordra.config import CordraConfig from mcp_cordra.config import CordraConfig
@@ -47,7 +46,7 @@ def mock_cordra_object():
"mediaType": "text/plain" "mediaType": "text/plain"
}, },
{ {
"name": "file2.pdf", "name": "file2.pdf",
"filename": "file2.pdf", "filename": "file2.pdf",
"size": 2048, "size": 2048,
"mediaType": "application/pdf" "mediaType": "application/pdf"
@@ -175,14 +174,14 @@ class TestCordraClient:
mock_response.status_code = 200 mock_response.status_code = 200
mock_response.json.return_value = mock_response_data mock_response.json.return_value = mock_response_data
mock_response.raise_for_status.return_value = None mock_response.raise_for_status.return_value = None
result = await client.find("type:Schema") result = await client.find("type:Schema")
assert len(result) == 3 assert len(result) == 3
assert result[0]["name"] == "User" assert result[0]["name"] == "User"
assert result[1]["name"] == "Project" assert result[1]["name"] == "Project"
assert result[2]["name"] == "Document" assert result[2]["name"] == "Document"
mock_get.assert_called_once_with( mock_get.assert_called_once_with(
"https://test.example.com/search", "https://test.example.com/search",
params={"query": "type:Schema"}, params={"query": "type:Schema"},
@@ -197,9 +196,9 @@ class TestCordraClient:
mock_response.status_code = 200 mock_response.status_code = 200
mock_response.json.return_value = mock_response_data mock_response.json.return_value = mock_response_data
mock_response.raise_for_status.return_value = None mock_response.raise_for_status.return_value = None
result = await client.find("type:NonExistent") result = await client.find("type:NonExistent")
assert result == [] assert result == []
mock_get.assert_called_once_with( mock_get.assert_called_once_with(
"https://test.example.com/search", "https://test.example.com/search",
@@ -215,9 +214,9 @@ class TestCordraClient:
mock_response.status_code = 200 mock_response.status_code = 200
mock_response.json.return_value = mock_response_data mock_response.json.return_value = mock_response_data
mock_response.raise_for_status.return_value = None mock_response.raise_for_status.return_value = None
result = await client.find("type:Schema") result = await client.find("type:Schema")
assert result == [] assert result == []
@patch('mcp_cordra.client.requests.Session.get') @patch('mcp_cordra.client.requests.Session.get')
@@ -225,10 +224,10 @@ class TestCordraClient:
"""Test find error handling.""" """Test find error handling."""
from requests import RequestException from requests import RequestException
mock_get.side_effect = RequestException("Search failed") mock_get.side_effect = RequestException("Search failed")
with pytest.raises(CordraClientError) as exc_info: with pytest.raises(CordraClientError) as exc_info:
await client.find("invalid:query") await client.find("invalid:query")
assert "Failed to search with query 'invalid:query'" in str(exc_info.value) assert "Failed to search with query 'invalid:query'" in str(exc_info.value)
assert "Search failed" in str(exc_info.value) assert "Search failed" in str(exc_info.value)

View File

@@ -40,9 +40,9 @@ class TestGetCordraObject:
async def test_get_object_success(self, mock_client, sample_digital_object): async def test_get_object_success(self, mock_client, sample_digital_object):
"""Test successful object retrieval.""" """Test successful object retrieval."""
mock_client.get_object = AsyncMock(return_value=sample_digital_object) mock_client.get_object = AsyncMock(return_value=sample_digital_object)
result = await get_cordra_object("people", "john-doe-123") result = await get_cordra_object("people", "john-doe-123")
# Verify the result is valid JSON # Verify the result is valid JSON
parsed_result = json.loads(result) parsed_result = json.loads(result)
assert parsed_result["id"] == "people/john-doe-123" assert parsed_result["id"] == "people/john-doe-123"
@@ -52,7 +52,7 @@ class TestGetCordraObject:
assert parsed_result["metadata"]["created"] == "2023-01-01" assert parsed_result["metadata"]["created"] == "2023-01-01"
assert len(parsed_result["payloads"]) == 1 assert len(parsed_result["payloads"]) == 1
assert parsed_result["payloads"][0]["name"] == "profile_photo" assert parsed_result["payloads"][0]["name"] == "profile_photo"
# Verify the client was called with the correct object ID # Verify the client was called with the correct object ID
mock_client.get_object.assert_called_once_with("people/john-doe-123") mock_client.get_object.assert_called_once_with("people/john-doe-123")
@@ -62,10 +62,10 @@ class TestGetCordraObject:
mock_client.get_object = AsyncMock( mock_client.get_object = AsyncMock(
side_effect=CordraNotFoundError("Object not found: people/nonexistent") side_effect=CordraNotFoundError("Object not found: people/nonexistent")
) )
with pytest.raises(RuntimeError) as exc_info: with pytest.raises(RuntimeError) as exc_info:
await get_cordra_object("people", "nonexistent") await get_cordra_object("people", "nonexistent")
assert "Object not found: people/nonexistent" in str(exc_info.value) assert "Object not found: people/nonexistent" in str(exc_info.value)
mock_client.get_object.assert_called_once_with("people/nonexistent") mock_client.get_object.assert_called_once_with("people/nonexistent")
@@ -75,10 +75,10 @@ class TestGetCordraObject:
mock_client.get_object = AsyncMock( mock_client.get_object = AsyncMock(
side_effect=CordraClientError("Connection failed") side_effect=CordraClientError("Connection failed")
) )
with pytest.raises(RuntimeError) as exc_info: with pytest.raises(RuntimeError) as exc_info:
await get_cordra_object("people", "john-doe-123") await get_cordra_object("people", "john-doe-123")
assert "Failed to retrieve object people/john-doe-123" in str(exc_info.value) assert "Failed to retrieve object people/john-doe-123" in str(exc_info.value)
assert "Connection failed" in str(exc_info.value) assert "Connection failed" in str(exc_info.value)
mock_client.get_object.assert_called_once_with("people/john-doe-123") mock_client.get_object.assert_called_once_with("people/john-doe-123")
@@ -87,14 +87,14 @@ class TestGetCordraObject:
async def test_object_id_construction(self, mock_client, sample_digital_object): async def test_object_id_construction(self, mock_client, sample_digital_object):
"""Test that object ID is correctly constructed from prefix and suffix.""" """Test that object ID is correctly constructed from prefix and suffix."""
mock_client.get_object = AsyncMock(return_value=sample_digital_object) mock_client.get_object = AsyncMock(return_value=sample_digital_object)
# Test various prefix/suffix combinations # Test various prefix/suffix combinations
test_cases = [ test_cases = [
("people", "john-doe-123", "people/john-doe-123"), ("people", "john-doe-123", "people/john-doe-123"),
("documents", "report-2023", "documents/report-2023"), ("documents", "report-2023", "documents/report-2023"),
("items", "item_with_underscores", "items/item_with_underscores"), ("items", "item_with_underscores", "items/item_with_underscores"),
] ]
for prefix, suffix, expected_id in test_cases: for prefix, suffix, expected_id in test_cases:
await get_cordra_object(prefix, suffix) await get_cordra_object(prefix, suffix)
mock_client.get_object.assert_called_with(expected_id) mock_client.get_object.assert_called_with(expected_id)
@@ -103,16 +103,16 @@ class TestGetCordraObject:
async def test_json_formatting(self, mock_client, sample_digital_object): async def test_json_formatting(self, mock_client, sample_digital_object):
"""Test that the returned JSON is properly formatted.""" """Test that the returned JSON is properly formatted."""
mock_client.get_object = AsyncMock(return_value=sample_digital_object) mock_client.get_object = AsyncMock(return_value=sample_digital_object)
result = await get_cordra_object("people", "john-doe-123") result = await get_cordra_object("people", "john-doe-123")
# Verify it's valid JSON with proper indentation # Verify it's valid JSON with proper indentation
parsed_result = json.loads(result) parsed_result = json.loads(result)
assert isinstance(parsed_result, dict) assert isinstance(parsed_result, dict)
# Check that the result contains indentation (pretty-printed) # Check that the result contains indentation (pretty-printed)
assert " " in result # Should have 2-space indentation assert " " in result # Should have 2-space indentation
# Verify all expected fields are present # Verify all expected fields are present
assert "id" in parsed_result assert "id" in parsed_result
assert "type" in parsed_result assert "type" in parsed_result
@@ -133,10 +133,10 @@ class TestGetCordraObject:
payloads=None payloads=None
) )
mock_client.get_object = AsyncMock(return_value=minimal_object) mock_client.get_object = AsyncMock(return_value=minimal_object)
result = await get_cordra_object("test", "minimal") result = await get_cordra_object("test", "minimal")
parsed_result = json.loads(result) parsed_result = json.loads(result)
assert parsed_result["id"] == "test/minimal" assert parsed_result["id"] == "test/minimal"
assert parsed_result["type"] == "" assert parsed_result["type"] == ""
assert parsed_result["content"]["id"] == "test/minimal" assert parsed_result["content"]["id"] == "test/minimal"
@@ -145,103 +145,88 @@ class TestGetCordraObject:
assert parsed_result["payloads"] is None assert parsed_result["payloads"] is None
class TestListCordraSchemas: class TestSchemaResourceFunctions:
"""Test the list_cordra_schemas resource handler.""" """Test the schema resource functions."""
@patch('mcp_cordra.server.cordra_client') @patch('mcp_cordra.server.cordra_client')
async def test_list_schemas_success(self, mock_client): async def test_create_schema_resource_success(self, mock_client):
"""Test successful schema listing.""" """Test successful schema resource creation."""
mock_schemas = [ mock_schema = DigitalObject(
{"content": {"name": "User"}, "identifier": "test/user-schema"}, id="test/user-schema",
{"content": {"name": "Project"}, "identifier": "test/project-schema"}, type="Schema",
{"content": {"name": "Document"}, "identifier": "test/doc-schema"}, content={"name": "User", "type": "object", "properties": {}}
{"content": {"name": "CaptureEvent"}, "identifier": "test/capture-schema"} )
] mock_client.get_schema = AsyncMock(return_value=mock_schema)
mock_client.find = AsyncMock(return_value=mock_schemas)
from mcp_cordra.server import create_schema_resource
from mcp_cordra.server import list_cordra_schemas result = await create_schema_resource("User")
result = await list_cordra_schemas()
# Verify the result is valid JSON # Verify the result is valid JSON
parsed_result = json.loads(result) parsed_result = json.loads(result)
assert "schemas" in parsed_result assert parsed_result["id"] == "test/user-schema"
assert "count" in parsed_result assert parsed_result["type"] == "Schema"
assert parsed_result["count"] == 4 assert parsed_result["content"]["name"] == "User"
assert "User" in parsed_result["schemas"]
assert "Project" in parsed_result["schemas"] # Verify the client was called with correct schema name
assert "Document" in parsed_result["schemas"] mock_client.get_schema.assert_called_once_with("User")
assert "CaptureEvent" in parsed_result["schemas"]
@patch('mcp_cordra.server.cordra_client')
async def test_create_schema_resource_not_found(self, mock_client):
"""Test schema resource creation with schema not found."""
mock_client.get_schema = AsyncMock(side_effect=CordraNotFoundError("Schema not found"))
from mcp_cordra.server import create_schema_resource
with pytest.raises(RuntimeError) as exc_info:
await create_schema_resource("NonExistent")
assert "Schema not found: NonExistent" in str(exc_info.value)
mock_client.get_schema.assert_called_once_with("NonExistent")
@patch('mcp_cordra.server.cordra_client')
async def test_register_schema_resources_success(self, mock_client):
"""Test successful schema resource registration."""
mock_schemas = [
{"content": {"name": "User"}, "id": "test/user-schema"},
{"content": {"name": "Project"}, "id": "test/project-schema"},
{"content": {"name": "Document"}, "id": "test/doc-schema"}
]
mock_client.find = AsyncMock(return_value=mock_schemas)
# Mock the mcp.add_resource method
with patch('mcp_cordra.server.mcp') as mock_mcp:
from mcp_cordra.server import register_schema_resources
await register_schema_resources()
# Verify the client was called with correct query # Verify the client was called with correct query
mock_client.find.assert_called_once_with("type:Schema") mock_client.find.assert_called_once_with("type:Schema")
# Verify add_resource was called for each schema
assert mock_mcp.add_resource.call_count == 3
@patch('mcp_cordra.server.cordra_client') @patch('mcp_cordra.server.cordra_client')
async def test_list_schemas_empty(self, mock_client): async def test_register_schema_resources_missing_name(self, mock_client):
"""Test schema listing with no results.""" """Test schema resource registration with objects missing name field."""
mock_client.find = AsyncMock(return_value=[])
from mcp_cordra.server import list_cordra_schemas
result = await list_cordra_schemas()
parsed_result = json.loads(result)
assert parsed_result["schemas"] == []
assert parsed_result["count"] == 0
mock_client.find.assert_called_once_with("type:Schema")
@patch('mcp_cordra.server.cordra_client')
async def test_list_schemas_missing_name_field(self, mock_client):
"""Test schema listing with objects missing name field."""
mock_schemas = [ mock_schemas = [
{"content": {"name": "User"}, "identifier": "test/user-schema"}, {"content": {"name": "User"}, "id": "test/user-schema"},
{"content": {}, "identifier": "test/no-name-schema"}, # Missing name field {"content": {}, "id": "test/no-name-schema"}, # Missing name field
{"content": {"name": "Project"}, "identifier": "test/project-schema"}, {"content": {"name": "Project"}, "id": "test/project-schema"}
{"content": {"other": "field"}} # No name or identifier
] ]
mock_client.find = AsyncMock(return_value=mock_schemas) mock_client.find = AsyncMock(return_value=mock_schemas)
from mcp_cordra.server import list_cordra_schemas with patch('mcp_cordra.server.mcp') as mock_mcp:
result = await list_cordra_schemas() from mcp_cordra.server import register_schema_resources
await register_schema_resources()
parsed_result = json.loads(result)
assert parsed_result["count"] == 2 # Only objects with name field # Only 2 schemas should be registered (those with name field)
assert "User" in parsed_result["schemas"] assert mock_mcp.add_resource.call_count == 2
assert "Project" in parsed_result["schemas"]
assert len(parsed_result["schemas"]) == 2
@patch('mcp_cordra.server.cordra_client') @patch('mcp_cordra.server.cordra_client')
async def test_list_schemas_client_error(self, mock_client): async def test_register_schema_resources_client_error(self, mock_client):
"""Test schema listing with client error.""" """Test schema resource registration with client error."""
from mcp_cordra.client import CordraClientError
mock_client.find = AsyncMock(side_effect=CordraClientError("Search failed")) mock_client.find = AsyncMock(side_effect=CordraClientError("Search failed"))
from mcp_cordra.server import list_cordra_schemas
with pytest.raises(RuntimeError) as exc_info:
await list_cordra_schemas()
assert "Failed to list schemas" in str(exc_info.value)
assert "Search failed" in str(exc_info.value)
@patch('mcp_cordra.server.cordra_client') # Should not raise an exception, just log a warning
async def test_list_schemas_json_format(self, mock_client): from mcp_cordra.server import register_schema_resources
"""Test that the returned JSON is properly formatted.""" await register_schema_resources() # Should complete without raising
mock_schemas = [
{"content": {"name": "TestSchema"}, "identifier": "test/schema"} mock_client.find.assert_called_once_with("type:Schema")
]
mock_client.find = AsyncMock(return_value=mock_schemas)
from mcp_cordra.server import list_cordra_schemas
result = await list_cordra_schemas()
# Verify it's valid JSON with proper indentation
parsed_result = json.loads(result)
assert isinstance(parsed_result, dict)
# Check that the result contains indentation (pretty-printed)
assert " " in result # Should have 2-space indentation
# Verify expected structure
assert "schemas" in parsed_result
assert "count" in parsed_result
assert isinstance(parsed_result["schemas"], list)
assert isinstance(parsed_result["count"], int)