Skip to content

Commit 7a8a969

Browse files
committed
test: add unit tests for HaystackServiceResource
1 parent 7a5780e commit 7a8a969

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from typing import Any
2+
3+
import pytest
4+
5+
from deepset_mcp.api.haystack_service.resource import HaystackServiceResource
6+
from deepset_mcp.api.transport import TransportResponse
7+
from tests.unit.api.conftest import MockAsyncClient
8+
9+
10+
def make_component_schema_response() -> dict[str, Any]:
11+
return {
12+
"Crawler": {
13+
"type": "Crawler",
14+
"Base": "BaseComponent",
15+
"Description": "A component that crawls websites and creates Documents from them.",
16+
"Parameters": {
17+
"urls": {
18+
"type": "list[str]",
19+
"required": True,
20+
"description": "List of URLs to crawl."
21+
}
22+
}
23+
}
24+
}
25+
26+
27+
@pytest.fixture
28+
def mock_successful_response(mock_client: MockAsyncClient) -> None:
29+
"""Configure the mock client to return a successful response."""
30+
mock_client.responses.append(
31+
TransportResponse(
32+
status_code=200,
33+
json=make_component_schema_response(),
34+
text="",
35+
success=True,
36+
)
37+
)
38+
39+
40+
@pytest.fixture
41+
def mock_error_response(mock_client: MockAsyncClient) -> None:
42+
"""Configure the mock client to return an error response."""
43+
mock_client.responses.append(
44+
TransportResponse(
45+
status_code=500,
46+
json={"message": "Internal server error"},
47+
text="Internal server error",
48+
success=False,
49+
)
50+
)
51+
52+
53+
def test_initialization(mock_client: MockAsyncClient) -> None:
54+
"""Test HaystackServiceResource initialization."""
55+
resource = HaystackServiceResource(client=mock_client)
56+
assert resource._client == mock_client
57+
58+
59+
async def test_get_component_schema_success(
60+
mock_client: MockAsyncClient,
61+
mock_successful_response: None,
62+
) -> None:
63+
"""Test successful component schema retrieval."""
64+
resource = HaystackServiceResource(client=mock_client)
65+
result = await resource.get_component_schema()
66+
67+
assert result == make_component_schema_response()
68+
assert mock_client.last_request == {
69+
"method": "GET",
70+
"endpoint": "v1/haystack/components",
71+
"headers": {"accept": "application/json"},
72+
"data": {"domain": "deepset-cloud"},
73+
}
74+
75+
76+
async def test_get_component_schema_error(
77+
mock_client: MockAsyncClient,
78+
mock_error_response: None,
79+
) -> None:
80+
"""Test error handling in component schema retrieval."""
81+
resource = HaystackServiceResource(client=mock_client)
82+
with pytest.raises(Exception, match="Internal server error"):
83+
await resource.get_component_schema()

0 commit comments

Comments
 (0)