Skip to content

Commit

Permalink
Add edge case unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
vblagoje committed Feb 7, 2025
1 parent e61d0b6 commit 5aedea5
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions test/components/connectors/test_openapi_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from openapi3 import OpenAPI

from haystack.components.connectors import OpenAPIServiceConnector
from haystack.components.connectors.openapi_service import patch_request
from haystack.dataclasses import ChatMessage, ToolCall


Expand Down Expand Up @@ -155,6 +156,46 @@ def test_run_with_missing_required_parameter(self, openapi_mock):
with pytest.raises(ValueError, match="Missing parameter: 'name' required for the 'greet' operation"):
connector.run(messages=[message], service_openapi_spec={})

@patch("haystack.components.connectors.openapi_service.OpenAPI")
def test_run_with_missing_required_parameters_in_request_body(self, openapi_mock):
"""
Test that the connector raises a ValueError when the request body is missing required parameters.
"""
connector = OpenAPIServiceConnector()
tool_call = ToolCall(
tool_name="post_message",
arguments={"recipient": "John"}, # only providing URL parameter, no request body data
)
message = ChatMessage.from_assistant(tool_calls=[tool_call])

# Mock the OpenAPI service
call_post_message = Mock()
call_post_message.operation.__self__ = Mock()
call_post_message.operation.__self__.raw_element = {
"parameters": [{"name": "recipient"}],
"requestBody": {
"required": True,
"content": {
"application/json": {
"schema": {
"required": ["message"], # Mark message as required in schema
"properties": {"message": {"type": "string"}},
}
}
},
},
}
mock_service = Mock(call_post_message=call_post_message, raw_element={})
openapi_mock.return_value = mock_service

with pytest.raises(
ValueError, match="Missing requestBody parameter: 'message' required for the 'post_message' operation"
):
connector.run(messages=[message], service_openapi_spec={})

# Verify that the service was never called since validation failed
call_post_message.assert_not_called()

def test_serialization(self):
for test_val in ("myvalue", True, None):
connector = OpenAPIServiceConnector(test_val)
Expand Down

0 comments on commit 5aedea5

Please sign in to comment.