From 674c3463ca73790f72105efb8ed8d42491611c0f Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Fri, 2 May 2025 08:14:47 +0000 Subject: [PATCH] Test: Improve empty messages tests with proper mocking Co-Authored-By: Joe Moura --- tests/test_empty_messages.py | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/tests/test_empty_messages.py b/tests/test_empty_messages.py index efffa294a..564fb65a1 100644 --- a/tests/test_empty_messages.py +++ b/tests/test_empty_messages.py @@ -1,11 +1,12 @@ -from unittest.mock import patch +from unittest.mock import patch, MagicMock import pytest from crewai.llm import LLM -def test_empty_messages_validation(): +@patch('crewai.llm.LLM._prepare_completion_params') +def test_empty_messages_validation(mock_prepare): """ Test that LLM.call() raises a ValueError when an empty messages list is passed. This prevents the IndexError in LiteLLM's ollama_pt() function. @@ -17,6 +18,33 @@ def test_empty_messages_validation(): with pytest.raises(ValueError, match="Messages list cannot be empty"): llm.call(messages=None) + + mock_prepare.assert_not_called() + + +@patch('crewai.llm.LLM._prepare_completion_params') +def test_empty_string_message(mock_prepare): + """ + Test that LLM.call() raises a ValueError when an empty string message is passed. + """ + llm = LLM(model="gpt-3.5-turbo") + + with pytest.raises(ValueError, match="Messages list cannot be empty"): + llm.call(messages="") + + mock_prepare.assert_not_called() + + +@patch('crewai.llm.LLM._prepare_completion_params') +def test_invalid_message_format(mock_prepare): + """ + Test that LLM.call() raises a TypeError when a message with invalid format is passed. + """ + mock_prepare.side_effect = TypeError("Invalid message format") + llm = LLM(model="gpt-3.5-turbo") + + with pytest.raises(TypeError, match="Invalid message format"): + llm.call(messages=[{}]) @pytest.mark.vcr(filter_headers=["authorization"])