Skip to content

Commit 41ef5c2

Browse files
committed
test: add tests for get_component_definition function
1 parent 1b6fa50 commit 41ef5c2

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

test/unit/tools/test_haystack_service.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,82 @@ def haystack_service(self) -> FakeHaystackServiceResource:
3131
return self._resource
3232

3333

34+
@pytest.mark.asyncio
35+
async def test_get_component_definition_success() -> None:
36+
# Sample component definition similar to the example provided
37+
component_type = "haystack.components.converters.xlsx.XLSXToDocument"
38+
response = {
39+
"component_schema": {
40+
"definitions": {
41+
"Components": {
42+
"XLSXToDocument": {
43+
"title": "XLSXToDocument",
44+
"description": "Converts XLSX files into Documents.",
45+
"properties": {
46+
"type": {
47+
"const": component_type,
48+
"family": "converters",
49+
"family_description": "Convert data into a format your pipeline can query."
50+
},
51+
"init_parameters": {
52+
"properties": {
53+
"sheet_name": {
54+
"_annotation": "typing.Union[str, int, list, None]",
55+
"description": "The name of the sheet to read.",
56+
"default": None
57+
},
58+
"table_format": {
59+
"_annotation": "str",
60+
"description": "The format to convert the Excel file to.",
61+
"default": "csv"
62+
}
63+
}
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
72+
resource = FakeHaystackServiceResource(get_component_schemas_response=response)
73+
client = FakeClient(resource)
74+
result = await get_component_definition(client, component_type)
75+
76+
# Check that all required information is present
77+
assert component_type in result
78+
assert "XLSXToDocument" in result
79+
assert "converters" in result
80+
assert "Convert data into a format" in result
81+
assert "sheet_name" in result
82+
assert "table_format" in result
83+
assert "default: csv" in result
84+
85+
86+
@pytest.mark.asyncio
87+
async def test_get_component_definition_not_found() -> None:
88+
response = {
89+
"component_schema": {
90+
"definitions": {
91+
"Components": {}
92+
}
93+
}
94+
}
95+
resource = FakeHaystackServiceResource(get_component_schemas_response=response)
96+
client = FakeClient(resource)
97+
result = await get_component_definition(client, "nonexistent.component")
98+
assert "Component not found" in result
99+
100+
101+
@pytest.mark.asyncio
102+
async def test_get_component_definition_api_error() -> None:
103+
resource = FakeHaystackServiceResource(exception=UnexpectedAPIError(status_code=500, message="API Error"))
104+
client = FakeClient(resource)
105+
result = await get_component_definition(client, "some.component")
106+
assert "Failed to retrieve component definition" in result
107+
assert "API Error" in result
108+
109+
34110
@pytest.mark.asyncio
35111
async def test_list_component_families_no_families() -> None:
36112
response: dict[str, Any] = {"component_schema": {"definitions": {"Components": {}}}}

0 commit comments

Comments
 (0)