mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 09:08:31 +00:00
feat: Add Bright Data tools (#314)
* Initial commit of BrightData tools * Renamed the BrightData test file names * Refactored and improved the overall BrightData tools * Add BrightData tools * Add tools to init * Added config class * Fix test failures and add missing __init__.py files - Remove problematic brightdata_dataset_tool_test.py that referenced non-existent classes - Fix brightdata_serp_tool_test.py to expect string responses instead of dict - Fix brightdata_webunlocker_tool_test.py to expect string responses instead of dict - Add missing tests/tools/__init__.py for proper test imports --------- Co-authored-by: Ranjan Dailata <ranjancse@gmail.com> Co-authored-by: Tony Kipkemboi <iamtonykipkemboi@gmail.com>
This commit is contained in:
0
tests/tools/__init__.py
Normal file
0
tests/tools/__init__.py
Normal file
54
tests/tools/brightdata_serp_tool_test.py
Normal file
54
tests/tools/brightdata_serp_tool_test.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from crewai_tools.tools.brightdata_tool.brightdata_serp import BrightDataSearchTool
|
||||
|
||||
|
||||
class TestBrightDataSearchTool(unittest.TestCase):
|
||||
@patch.dict(
|
||||
"os.environ",
|
||||
{"BRIGHT_DATA_API_KEY": "test_api_key", "BRIGHT_DATA_ZONE": "test_zone"},
|
||||
)
|
||||
def setUp(self):
|
||||
self.tool = BrightDataSearchTool()
|
||||
|
||||
@patch("requests.post")
|
||||
def test_run_successful_search(self, mock_post):
|
||||
# Sample mock JSON response
|
||||
mock_response = MagicMock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.text = "mock response text"
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
# Define search input
|
||||
input_data = {
|
||||
"query": "latest AI news",
|
||||
"search_engine": "google",
|
||||
"country": "us",
|
||||
"language": "en",
|
||||
"search_type": "nws",
|
||||
"device_type": "desktop",
|
||||
"parse_results": True,
|
||||
"save_file": False,
|
||||
}
|
||||
|
||||
result = self.tool._run(**input_data)
|
||||
|
||||
# Assertions
|
||||
self.assertIsInstance(result, str) # Your tool returns response.text (string)
|
||||
mock_post.assert_called_once()
|
||||
|
||||
@patch("requests.post")
|
||||
def test_run_with_request_exception(self, mock_post):
|
||||
mock_post.side_effect = Exception("Timeout")
|
||||
|
||||
result = self.tool._run(query="AI", search_engine="google")
|
||||
self.assertIn("Error", result)
|
||||
|
||||
def tearDown(self):
|
||||
# Clean up env vars
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
64
tests/tools/brightdata_webunlocker_tool_test.py
Normal file
64
tests/tools/brightdata_webunlocker_tool_test.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
import requests
|
||||
|
||||
from crewai_tools.tools.brightdata_tool.brightdata_unlocker import (
|
||||
BrightDataWebUnlockerTool,
|
||||
)
|
||||
|
||||
|
||||
@patch.dict(
|
||||
"os.environ",
|
||||
{"BRIGHT_DATA_API_KEY": "test_api_key", "BRIGHT_DATA_ZONE": "test_zone"},
|
||||
)
|
||||
@patch("crewai_tools.tools.brightdata_tool.brightdata_unlocker.requests.post")
|
||||
def test_run_success_html(mock_post):
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.text = "<html><body>Test</body></html>"
|
||||
mock_response.raise_for_status = Mock()
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
tool = BrightDataWebUnlockerTool()
|
||||
result = tool._run(url="https://example.com", format="html", save_file=False)
|
||||
|
||||
print(result)
|
||||
|
||||
|
||||
@patch.dict(
|
||||
"os.environ",
|
||||
{"BRIGHT_DATA_API_KEY": "test_api_key", "BRIGHT_DATA_ZONE": "test_zone"},
|
||||
)
|
||||
@patch("crewai_tools.tools.brightdata_tool.brightdata_unlocker.requests.post")
|
||||
def test_run_success_json(mock_post):
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 200
|
||||
mock_response.text = "mock response text"
|
||||
mock_response.raise_for_status = Mock()
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
tool = BrightDataWebUnlockerTool()
|
||||
result = tool._run(url="https://example.com", format="json")
|
||||
|
||||
assert isinstance(result, str)
|
||||
|
||||
|
||||
@patch.dict(
|
||||
"os.environ",
|
||||
{"BRIGHT_DATA_API_KEY": "test_api_key", "BRIGHT_DATA_ZONE": "test_zone"},
|
||||
)
|
||||
@patch("crewai_tools.tools.brightdata_tool.brightdata_unlocker.requests.post")
|
||||
def test_run_http_error(mock_post):
|
||||
mock_response = Mock()
|
||||
mock_response.status_code = 403
|
||||
mock_response.text = "Forbidden"
|
||||
mock_response.raise_for_status.side_effect = requests.HTTPError(
|
||||
response=mock_response
|
||||
)
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
tool = BrightDataWebUnlockerTool()
|
||||
result = tool._run(url="https://example.com")
|
||||
|
||||
assert "HTTP Error" in result
|
||||
assert "Forbidden" in result
|
||||
Reference in New Issue
Block a user