mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-01 15:22:37 +00:00
Add additional tests for edge cases in provider data fetching
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import json
|
||||||
import tempfile
|
import tempfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
@@ -9,6 +10,7 @@ from crewai.cli.provider import (
|
|||||||
fetch_provider_data,
|
fetch_provider_data,
|
||||||
get_provider_data,
|
get_provider_data,
|
||||||
load_provider_data,
|
load_provider_data,
|
||||||
|
read_cache_file,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -59,6 +61,68 @@ class TestProviderFunctions:
|
|||||||
assert result is None
|
assert result is None
|
||||||
mock_get.assert_called_once()
|
mock_get.assert_called_once()
|
||||||
|
|
||||||
|
@mock.patch("crewai.cli.provider.requests.get")
|
||||||
|
def test_fetch_provider_data_handles_timeout(self, mock_get, mock_cache_file):
|
||||||
|
"""Test that fetch_provider_data handles Timeout exception properly."""
|
||||||
|
mock_get.side_effect = requests.Timeout("Connection timed out")
|
||||||
|
|
||||||
|
result = fetch_provider_data(mock_cache_file)
|
||||||
|
|
||||||
|
assert result is None
|
||||||
|
mock_get.assert_called_once()
|
||||||
|
|
||||||
|
@mock.patch("crewai.cli.provider.requests.get")
|
||||||
|
def test_fetch_provider_data_handles_ssl_error(self, mock_get, mock_cache_file):
|
||||||
|
"""Test that fetch_provider_data handles SSLError exception properly."""
|
||||||
|
mock_get.side_effect = requests.SSLError("SSL Certificate verification failed")
|
||||||
|
|
||||||
|
result = fetch_provider_data(mock_cache_file)
|
||||||
|
|
||||||
|
assert result is None
|
||||||
|
mock_get.assert_called_once()
|
||||||
|
|
||||||
|
@mock.patch("crewai.cli.provider.requests.get")
|
||||||
|
def test_fetch_provider_data_handles_json_decode_error(
|
||||||
|
self, mock_get, mock_response, mock_cache_file
|
||||||
|
):
|
||||||
|
"""Test that fetch_provider_data handles JSONDecodeError properly."""
|
||||||
|
mock_get.return_value = mock_response
|
||||||
|
mock_response.iter_content.return_value = [b"invalid json"]
|
||||||
|
|
||||||
|
result = fetch_provider_data(mock_cache_file)
|
||||||
|
|
||||||
|
assert result is None
|
||||||
|
mock_get.assert_called_once()
|
||||||
|
|
||||||
|
@mock.patch("builtins.open", new_callable=mock.mock_open, read_data="invalid json")
|
||||||
|
def test_read_cache_file_handles_json_decode_error(self, mock_file, mock_cache_file):
|
||||||
|
"""Test that read_cache_file handles JSONDecodeError properly."""
|
||||||
|
with mock.patch.object(Path, "exists", return_value=True):
|
||||||
|
result = read_cache_file(mock_cache_file)
|
||||||
|
|
||||||
|
assert result is None
|
||||||
|
mock_file.assert_called_once_with(mock_cache_file, "r")
|
||||||
|
|
||||||
|
@mock.patch("builtins.open")
|
||||||
|
def test_read_cache_file_handles_os_error(self, mock_file, mock_cache_file):
|
||||||
|
"""Test that read_cache_file handles OSError properly."""
|
||||||
|
mock_file.side_effect = OSError("File I/O error")
|
||||||
|
with mock.patch.object(Path, "exists", return_value=True):
|
||||||
|
result = read_cache_file(mock_cache_file)
|
||||||
|
|
||||||
|
assert result is None
|
||||||
|
mock_file.assert_called_once_with(mock_cache_file, "r")
|
||||||
|
|
||||||
|
@mock.patch("builtins.open", new_callable=mock.mock_open, read_data='{"key": [1, 2, 3]}')
|
||||||
|
def test_read_cache_file_handles_invalid_format(self, mock_file, mock_cache_file):
|
||||||
|
"""Test that read_cache_file handles invalid data format properly."""
|
||||||
|
with mock.patch.object(Path, "exists", return_value=True):
|
||||||
|
with mock.patch("json.load", return_value=["not", "a", "dict"]):
|
||||||
|
result = read_cache_file(mock_cache_file)
|
||||||
|
|
||||||
|
assert result is None
|
||||||
|
mock_file.assert_called_once_with(mock_cache_file, "r")
|
||||||
|
|
||||||
@mock.patch("crewai.cli.provider.fetch_provider_data")
|
@mock.patch("crewai.cli.provider.fetch_provider_data")
|
||||||
@mock.patch("crewai.cli.provider.read_cache_file")
|
@mock.patch("crewai.cli.provider.read_cache_file")
|
||||||
def test_load_provider_data_with_ssl_verify(
|
def test_load_provider_data_with_ssl_verify(
|
||||||
|
|||||||
Reference in New Issue
Block a user