arcade-mcp/toolkits/x/tests/test_users.py
Sam Partee bebfcab1e9
Add lookup_tweet_by_id to X Toolkit (#165)
This PR introduces the `lookup_tweet_by_id` tool to the X toolkit,
enabling users to retrieve tweet details by tweet ID. This enhancement
extends the toolkit's capabilities, allowing for more comprehensive
interactions with the X (Twitter) API.

**Key Changes:**

- **Added `lookup_tweet_by_id` Tool:**
- Implemented the `lookup_tweet_by_id` function in `tools/tweets.py`,
which allows users to fetch tweet information using a tweet ID.
- Included error handling for API response codes and expanded URLs in
tweets to assist language models in avoiding hallucinations due to
shortened URLs.

- **Enhanced Toolkit Structure:**
- Added several configuration files to the X toolkit to establish a
standardized project structure, which in the future will be generated by
`arcade new`. These include:
- `.pre-commit-config.yaml`: Defines pre-commit hooks for code quality
checks.
    - `.ruff.toml`: Configuration for the Ruff linter.
    - `LICENSE`: MIT License file for the toolkit.
- `Makefile`: Contains common commands for building, testing, and
linting the toolkit.

- **Updated Makefile:**
- Added `make check-toolkits` command to the top-level `Makefile`. This
command runs code quality tools for each toolkit that contains a
`Makefile`.

**Additional Notes:**

- **Tests:**
- Added unit tests for the new `lookup_tweet_by_id` tool in
`tests/test_tweets.py`.
- Included tests for the user lookup functionality in
`tests/test_users.py`.

- **Linting and Code Quality:**
- Configured pre-commit hooks and Ruff linter to enforce code standards.
- Updated the `pyproject.toml` file with development dependencies for
testing and linting.

-

---------

Co-authored-by: Eric Gustin <eric@arcade-ai.com>
2024-11-27 17:07:12 -08:00

78 lines
2.7 KiB
Python

from unittest.mock import MagicMock
import httpx
import pytest
from arcade.sdk.errors import ToolExecutionError
from arcade_x.tools.users import lookup_single_user_by_username
@pytest.mark.asyncio
async def test_lookup_single_user_by_username_success(tool_context, mock_httpx_client):
"""Test successful lookup of a user by username."""
# Mock response for a successful user lookup
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.json.return_value = {
"data": {
"id": "1234567890",
"name": "Test User",
"username": "testuser",
"description": "This is a test user",
# Additional fields can be added here as needed
}
}
mock_httpx_client.get.return_value = mock_response
username = "testuser"
result = await lookup_single_user_by_username(tool_context, username)
assert "data" in result
assert result["data"]["username"] == "testuser"
assert result["data"]["name"] == "Test User"
mock_httpx_client.get.assert_called_once()
@pytest.mark.asyncio
async def test_lookup_single_user_by_username_user_not_found(tool_context, mock_httpx_client):
"""Test behavior when looking up user fails due to API error"""
# Mock response for user not found
mock_response = httpx.HTTPStatusError(
"Not Found", request=MagicMock(), response=MagicMock(status_code=404)
)
mock_httpx_client.get.side_effect = mock_response
username = "nonexistentuser"
with pytest.raises(ToolExecutionError):
await lookup_single_user_by_username(tool_context, username)
mock_httpx_client.get.assert_called_once()
@pytest.mark.asyncio
async def test_lookup_single_user_by_username_api_error(tool_context, mock_httpx_client):
"""Test behavior when API returns an error other than 404."""
# Mock response for API error
mock_response = httpx.HTTPStatusError(
"Internal Server Error", request=MagicMock(), response=MagicMock(status_code=500)
)
mock_httpx_client.get.side_effect = mock_response
username = "testuser"
with pytest.raises(ToolExecutionError):
await lookup_single_user_by_username(tool_context, username)
mock_httpx_client.get.assert_called_once()
@pytest.mark.asyncio
async def test_lookup_single_user_by_username_network_error(tool_context, mock_httpx_client):
"""Test behavior when there is a network error during the request."""
# Mock client.get to raise an HTTPError
mock_httpx_client.get.side_effect = httpx.HTTPError("Network Error")
username = "testuser"
with pytest.raises(ToolExecutionError):
await lookup_single_user_by_username(tool_context, username)
mock_httpx_client.get.assert_called_once()