mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-02 07:42:40 +00:00
Update tests to match new to_json implementation
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -51,30 +51,16 @@ class TestCustomOpenAIJson:
|
|||||||
# Verify that the fallback mechanism was used
|
# Verify that the fallback mechanism was used
|
||||||
mock_llm_with_function_calling.call.assert_called_once()
|
mock_llm_with_function_calling.call.assert_called_once()
|
||||||
|
|
||||||
# The result should be a JSON string
|
# The result should be a dictionary
|
||||||
assert isinstance(result, str)
|
assert isinstance(result, dict)
|
||||||
|
assert result.get("name") == "John"
|
||||||
# The result might be a string representation of a JSON string
|
assert result.get("age") == 30
|
||||||
# Try to parse it directly first, and if that fails, try to parse it as a string representation
|
|
||||||
try:
|
|
||||||
parsed_result = json.loads(result)
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
# If it's a string representation of a JSON string, it will be surrounded by quotes
|
|
||||||
# and have escaped quotes inside
|
|
||||||
if result.startswith('"') and result.endswith('"'):
|
|
||||||
# Remove the surrounding quotes and unescape the string
|
|
||||||
unescaped = result[1:-1].replace('\\"', '"')
|
|
||||||
parsed_result = json.loads(unescaped)
|
|
||||||
|
|
||||||
assert isinstance(parsed_result, dict)
|
|
||||||
assert parsed_result.get("name") == "John"
|
|
||||||
assert parsed_result.get("age") == 30
|
|
||||||
|
|
||||||
def test_custom_openai_json_conversion_without_error(self, mock_llm_with_function_calling):
|
def test_custom_openai_json_conversion_without_error(self, mock_llm_with_function_calling):
|
||||||
"""Test that JSON conversion works normally when Instructor doesn't raise an error."""
|
"""Test that JSON conversion works normally when Instructor doesn't raise an error."""
|
||||||
# Mock Instructor that returns JSON without error
|
# Mock Instructor that returns JSON without error
|
||||||
mock_instructor = Mock()
|
mock_instructor = Mock()
|
||||||
mock_instructor.to_json.return_value = '{"name": "John", "age": 30}'
|
mock_instructor.to_json.return_value = {"name": "John", "age": 30}
|
||||||
|
|
||||||
# Create converter with mocked dependencies
|
# Create converter with mocked dependencies
|
||||||
converter = Converter(
|
converter = Converter(
|
||||||
@@ -93,7 +79,8 @@ class TestCustomOpenAIJson:
|
|||||||
mock_llm_with_function_calling.call.assert_not_called()
|
mock_llm_with_function_calling.call.assert_not_called()
|
||||||
|
|
||||||
# Verify the result matches the expected output
|
# Verify the result matches the expected output
|
||||||
assert result == '{"name": "John", "age": 30}'
|
assert isinstance(result, dict)
|
||||||
|
assert result == {"name": "John", "age": 30}
|
||||||
|
|
||||||
def test_custom_openai_json_conversion_with_invalid_json(self, mock_llm_with_function_calling):
|
def test_custom_openai_json_conversion_with_invalid_json(self, mock_llm_with_function_calling):
|
||||||
"""Test that JSON conversion handles invalid JSON gracefully."""
|
"""Test that JSON conversion handles invalid JSON gracefully."""
|
||||||
@@ -117,9 +104,9 @@ class TestCustomOpenAIJson:
|
|||||||
|
|
||||||
# Mock the _create_instructor method to return our mocked instructor
|
# Mock the _create_instructor method to return our mocked instructor
|
||||||
with patch.object(converter, '_create_instructor', return_value=mock_instructor):
|
with patch.object(converter, '_create_instructor', return_value=mock_instructor):
|
||||||
# Call to_json method
|
# Call to_json method and expect it to raise a ConverterError
|
||||||
result = converter.to_json()
|
with pytest.raises(ConverterError) as excinfo:
|
||||||
|
converter.to_json()
|
||||||
|
|
||||||
# The result should be a ConverterError instance
|
# Check the error message
|
||||||
assert isinstance(result, ConverterError)
|
assert "invalid json" in str(excinfo.value).lower() or "expecting value" in str(excinfo.value).lower()
|
||||||
assert "invalid json" in str(result).lower() or "expecting value" in str(result).lower()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user