arcade-mcp/toolkits/search/tests/test_google.py
Nate Barbettini e9ee3bba40
fix: Use tool secrets in toolkits (#271)
~~Note: Don't merge until the correct secrets have been added to Arcade
Cloud.~~

Ready to merge, the feature is already on its way to prod.

---------

Co-authored-by: Eric Gustin <eric@arcade.dev>
2025-03-04 13:35:36 -08:00

50 lines
1.6 KiB
Python

import json
from unittest.mock import patch
import pytest
from arcade.core.schema import ToolSecretItem
from arcade.sdk import ToolContext
from arcade_search.tools.google import search_google
@pytest.fixture
def mock_context():
return ToolContext(secrets=[ToolSecretItem(key="serp_api_key", value="fake_api_key")])
@pytest.mark.asyncio
async def test_search_google_success(mock_context):
with (
patch("serpapi.Client") as MockClient,
):
mock_client_instance = MockClient.return_value
mock_client_instance.search.return_value.as_dict.return_value = {
"organic_results": [
{"title": "Result 1", "link": "http://example.com/1"},
{"title": "Result 2", "link": "http://example.com/2"},
{"title": "Result 3", "link": "http://example.com/3"},
]
}
result = await search_google(mock_context, "test query", 2)
expected_result = json.dumps([
{"title": "Result 1", "link": "http://example.com/1"},
{"title": "Result 2", "link": "http://example.com/2"},
])
assert result == expected_result
@pytest.mark.asyncio
async def test_search_google_no_results(mock_context):
with (
patch("serpapi.Client") as MockClient,
):
mock_client_instance = MockClient.return_value
mock_client_instance.search.return_value.as_dict.return_value = {"organic_results": []}
result = await search_google(mock_context, "test query", 2)
expected_result = json.dumps([])
assert result == expected_result