From 17dcde20c77d895063a1e378497aed7a7364dd4b Mon Sep 17 00:00:00 2001 From: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com> Date: Mon, 21 Sep 2026 23:13:56 +0530 Subject: [PATCH] test(ollama): narrow pytest.raises blocks to wrap only get_response in error tests In test_cmc_with_invalid_data_content_media_type and test_cmc_with_invalid_content_type, the entire test setup including message creation and client instantiation was enclosed inside the with pytest.raises block. If any setup statement failed with ChatClientInvalidRequestException, the test would pass falsely. Moving the fixture setup outside the context manager ensures that only the actual get_response call is asserted to raise the exception. Signed-off-by: Manohar Paturi <186662190+ManoharPaturi@users.noreply.github.com> --- .../ollama/tests/test_ollama_chat_client.py | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/python/packages/ollama/tests/test_ollama_chat_client.py b/python/packages/ollama/tests/test_ollama_chat_client.py index a8245bf3ef0..e393e808980 100644 --- a/python/packages/ollama/tests/test_ollama_chat_client.py +++ b/python/packages/ollama/tests/test_ollama_chat_client.py @@ -639,19 +639,19 @@ async def test_cmc_with_invalid_data_content_media_type( chat_history: list[Message], mock_streaming_chat_completion_response: AsyncStream[OllamaChatResponse], ) -> None: - with pytest.raises(ChatClientInvalidRequestException): - mock_chat.return_value = mock_streaming_chat_completion_response - # Remote Uris are not supported by Ollama client - chat_history.append( - Message( - contents=[Content.from_uri(uri="data:audio/mp3;base64,xyz", media_type="audio/mp3")], - role="user", - ) + mock_chat.return_value = mock_streaming_chat_completion_response + # Remote Uris are not supported by Ollama client + chat_history.append( + Message( + contents=[Content.from_uri(uri="data:audio/mp3;base64,xyz", media_type="audio/mp3")], + role="user", ) + ) - ollama_client = OllamaChatClient() - ollama_client.client.chat = AsyncMock(return_value=mock_streaming_chat_completion_response) # type: ignore[method-assign] + ollama_client = OllamaChatClient() + ollama_client.client.chat = AsyncMock(return_value=mock_streaming_chat_completion_response) # type: ignore[method-assign] + with pytest.raises(ChatClientInvalidRequestException): await ollama_client.get_response(messages=chat_history) @@ -662,18 +662,18 @@ async def test_cmc_with_invalid_content_type( chat_history: list[Message], mock_chat_completion_response: AsyncStream[OllamaChatResponse], ) -> None: - with pytest.raises(ChatClientInvalidRequestException): - mock_chat.return_value = mock_chat_completion_response - # Remote Uris are not supported by Ollama client - chat_history.append( - Message( - contents=[Content.from_uri(uri="http://example.com/image.png", media_type="image/png")], - role="user", - ) + mock_chat.return_value = mock_chat_completion_response + # Remote Uris are not supported by Ollama client + chat_history.append( + Message( + contents=[Content.from_uri(uri="http://example.com/image.png", media_type="image/png")], + role="user", ) + ) - ollama_client = OllamaChatClient() + ollama_client = OllamaChatClient() + with pytest.raises(ChatClientInvalidRequestException): await ollama_client.get_response(messages=chat_history)