mirror of
https://github.com/dnlbauer/cordra-mcp.git
synced 2026-09-10 21:55:30 +00:00
feat: add schema listing functionality
- Add find method to CordraClient for querying objects - Implement cordra://schemas resource for listing available type schemas - Add comprehensive unit tests for both client find method and server schema listing - Handle CordraPy response format with results array extraction - Support schema name extraction from returned objects
This commit is contained in:
@@ -156,6 +156,66 @@ class TestCordraClient:
|
||||
|
||||
assert "Failed to retrieve object test/123" in str(exc_info.value)
|
||||
|
||||
@patch('mcp_cordra.client.cordra.CordraObject.find')
|
||||
async def test_find_success(self, mock_find, client):
|
||||
"""Test successful find operation."""
|
||||
mock_response = {
|
||||
"results": [
|
||||
{"name": "User", "identifier": "test/user-schema"},
|
||||
{"name": "Project", "identifier": "test/project-schema"},
|
||||
{"name": "Document", "identifier": "test/doc-schema"}
|
||||
],
|
||||
"size": 3
|
||||
}
|
||||
mock_find.return_value = mock_response
|
||||
|
||||
result = await client.find("type:Schema")
|
||||
|
||||
assert len(result) == 3
|
||||
assert result[0]["name"] == "User"
|
||||
assert result[1]["name"] == "Project"
|
||||
assert result[2]["name"] == "Document"
|
||||
|
||||
mock_find.assert_called_once_with(
|
||||
client.config.cordra_url,
|
||||
"type:Schema"
|
||||
)
|
||||
|
||||
@patch('mcp_cordra.client.cordra.CordraObject.find')
|
||||
async def test_find_empty_results(self, mock_find, client):
|
||||
"""Test find with empty results."""
|
||||
mock_response = {"results": [], "size": 0}
|
||||
mock_find.return_value = mock_response
|
||||
|
||||
result = await client.find("type:NonExistent")
|
||||
|
||||
assert result == []
|
||||
mock_find.assert_called_once_with(
|
||||
client.config.cordra_url,
|
||||
"type:NonExistent"
|
||||
)
|
||||
|
||||
@patch('mcp_cordra.client.cordra.CordraObject.find')
|
||||
async def test_find_no_results_key(self, mock_find, client):
|
||||
"""Test find with response missing results key."""
|
||||
mock_response = {"size": 0} # No results key
|
||||
mock_find.return_value = mock_response
|
||||
|
||||
result = await client.find("type:Schema")
|
||||
|
||||
assert result == []
|
||||
|
||||
@patch('mcp_cordra.client.cordra.CordraObject.find')
|
||||
async def test_find_error(self, mock_find, client):
|
||||
"""Test find error handling."""
|
||||
mock_find.side_effect = Exception("Search failed")
|
||||
|
||||
with pytest.raises(CordraClientError) as exc_info:
|
||||
await client.find("invalid:query")
|
||||
|
||||
assert "Failed to search with query 'invalid:query'" in str(exc_info.value)
|
||||
assert "Search failed" in str(exc_info.value)
|
||||
|
||||
|
||||
class TestCordraConfig:
|
||||
"""Test the CordraConfig class."""
|
||||
|
||||
@@ -142,4 +142,106 @@ class TestGetCordraObject:
|
||||
assert parsed_result["content"]["id"] == "test/minimal"
|
||||
assert parsed_result["metadata"] is None
|
||||
assert parsed_result["acl"] is None
|
||||
assert parsed_result["payloads"] is None
|
||||
assert parsed_result["payloads"] is None
|
||||
|
||||
|
||||
class TestListCordraSchemas:
|
||||
"""Test the list_cordra_schemas resource handler."""
|
||||
|
||||
@patch('mcp_cordra.server.cordra_client')
|
||||
async def test_list_schemas_success(self, mock_client):
|
||||
"""Test successful schema listing."""
|
||||
mock_schemas = [
|
||||
{"name": "User", "identifier": "test/user-schema"},
|
||||
{"name": "Project", "identifier": "test/project-schema"},
|
||||
{"name": "Document", "identifier": "test/doc-schema"},
|
||||
{"name": "CaptureEvent", "identifier": "test/capture-schema"}
|
||||
]
|
||||
mock_client.find = AsyncMock(return_value=mock_schemas)
|
||||
|
||||
from mcp_cordra.server import list_cordra_schemas
|
||||
result = await list_cordra_schemas()
|
||||
|
||||
# Verify the result is valid JSON
|
||||
parsed_result = json.loads(result)
|
||||
assert "schemas" in parsed_result
|
||||
assert "count" in parsed_result
|
||||
assert parsed_result["count"] == 4
|
||||
assert "User" in parsed_result["schemas"]
|
||||
assert "Project" in parsed_result["schemas"]
|
||||
assert "Document" in parsed_result["schemas"]
|
||||
assert "CaptureEvent" in parsed_result["schemas"]
|
||||
|
||||
# Verify the client was called with correct query
|
||||
mock_client.find.assert_called_once_with("type:Schema")
|
||||
|
||||
@patch('mcp_cordra.server.cordra_client')
|
||||
async def test_list_schemas_empty(self, mock_client):
|
||||
"""Test schema listing with no results."""
|
||||
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 = [
|
||||
{"name": "User", "identifier": "test/user-schema"},
|
||||
{"identifier": "test/no-name-schema"}, # Missing name field
|
||||
{"name": "Project", "identifier": "test/project-schema"},
|
||||
{"other": "field"} # No name or identifier
|
||||
]
|
||||
mock_client.find = AsyncMock(return_value=mock_schemas)
|
||||
|
||||
from mcp_cordra.server import list_cordra_schemas
|
||||
result = await list_cordra_schemas()
|
||||
|
||||
parsed_result = json.loads(result)
|
||||
assert parsed_result["count"] == 2 # Only objects with name field
|
||||
assert "User" in parsed_result["schemas"]
|
||||
assert "Project" in parsed_result["schemas"]
|
||||
assert len(parsed_result["schemas"]) == 2
|
||||
|
||||
@patch('mcp_cordra.server.cordra_client')
|
||||
async def test_list_schemas_client_error(self, mock_client):
|
||||
"""Test schema listing with client error."""
|
||||
from mcp_cordra.client import CordraClientError
|
||||
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')
|
||||
async def test_list_schemas_json_format(self, mock_client):
|
||||
"""Test that the returned JSON is properly formatted."""
|
||||
mock_schemas = [
|
||||
{"name": "TestSchema", "identifier": "test/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)
|
||||
Reference in New Issue
Block a user