Test: Improve empty messages tests with proper mocking

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-05-02 08:14:47 +00:00
parent c9912e9979
commit 674c3463ca

View File

@@ -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"])