Skip to content

Commit 102942d

Browse files
authored
test: Openrouter - add unit tests (#3216)
1 parent 25020f2 commit 102942d

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

integrations/openrouter/tests/test_openrouter_chat_generator.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,33 @@ def test_run_with_params(self, chat_messages, mock_chat_completion, monkeypatch)
268268
assert len(response["replies"]) == 1
269269
assert [isinstance(reply, ChatMessage) for reply in response["replies"]]
270270

271+
def test_prepare_api_call_with_tools_strict(self, chat_messages, tools, monkeypatch):
272+
monkeypatch.setenv("OPENROUTER_API_KEY", "fake-api-key")
273+
component = OpenRouterChatGenerator(tools=tools)
274+
api_args = component._prepare_api_call(messages=chat_messages, tools_strict=True)
275+
276+
assert api_args["tools"][0]["type"] == "function"
277+
function_spec = api_args["tools"][0]["function"]
278+
assert function_spec["name"] == "weather"
279+
assert function_spec["strict"] is True
280+
assert function_spec["parameters"]["additionalProperties"] is False
281+
282+
def test_prepare_api_call_raises_when_streaming_with_multiple_responses(self, chat_messages, monkeypatch):
283+
monkeypatch.setenv("OPENROUTER_API_KEY", "fake-api-key")
284+
component = OpenRouterChatGenerator(generation_kwargs={"n": 2})
285+
with pytest.raises(ValueError, match="Cannot stream multiple responses"):
286+
component._prepare_api_call(messages=chat_messages, streaming_callback=print_streaming_chunk)
287+
288+
def test_prepare_api_call_with_response_format_and_streaming(self, chat_messages, monkeypatch):
289+
monkeypatch.setenv("OPENROUTER_API_KEY", "fake-api-key")
290+
response_format = {"type": "json_schema", "json_schema": {"name": "Foo", "schema": {"type": "object"}}}
291+
component = OpenRouterChatGenerator(generation_kwargs={"response_format": response_format})
292+
api_args = component._prepare_api_call(messages=chat_messages, streaming_callback=print_streaming_chunk)
293+
294+
assert api_args["stream"] is True
295+
assert api_args["openai_endpoint"] == "create"
296+
assert api_args["response_format"] == response_format
297+
271298
@pytest.mark.skipif(
272299
not os.environ.get("OPENROUTER_API_KEY", None),
273300
reason="Export an env var called OPENROUTER_API_KEY containing the OpenRouter API key to run this test.",

0 commit comments

Comments
 (0)