- Add 111 unit tests covering all client classes and exceptions - Test BaseClientMixin: initialization, validation, retry logic - Test FastGPTClient: HTTP requests, streaming, error handling, context manager - Test ChatClient: all chat operations (completion, histories, records, feedback) - Test AppClient: app analytics and logs - Test all exception classes with various configurations - Add shared pytest fixtures in conftest.py 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
181 lines
6.4 KiB
Python
181 lines
6.4 KiB
Python
"""Tests for AppClient."""
|
|
|
|
from unittest.mock import Mock, patch
|
|
import pytest
|
|
|
|
import httpx
|
|
|
|
from fastgpt_client.client import AppClient
|
|
|
|
|
|
class TestAppClientGetAppLogsChart:
|
|
"""Test suite for AppClient.get_app_logs_chart method."""
|
|
|
|
def test_get_app_logs_chart_basic(self, api_key, sample_app_logs_response):
|
|
"""Test getting app logs chart with basic parameters."""
|
|
client = AppClient(api_key)
|
|
|
|
mock_response = Mock(spec=httpx.Response)
|
|
mock_response.status_code = 200
|
|
mock_response.json = Mock(return_value=sample_app_logs_response)
|
|
|
|
with patch.object(client, '_send_request', return_value=mock_response) as mock_send:
|
|
response = client.get_app_logs_chart(
|
|
appId="app-123",
|
|
dateStart="2024-01-01",
|
|
dateEnd="2024-01-31"
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
call_args = mock_send.call_args
|
|
assert call_args[0][0] == "POST"
|
|
assert call_args[0][1] == "/api/proApi/core/app/logs/getChartData"
|
|
json_data = call_args[1]['json']
|
|
assert json_data['appId'] == "app-123"
|
|
assert json_data['dateStart'] == "2024-01-01"
|
|
assert json_data['dateEnd'] == "2024-01-31"
|
|
# Default source should be ["api"]
|
|
assert json_data['source'] == ["api"]
|
|
|
|
def test_get_app_logs_chart_with_offset(self, api_key):
|
|
"""Test getting app logs chart with custom offset."""
|
|
client = AppClient(api_key)
|
|
|
|
mock_response = Mock(spec=httpx.Response)
|
|
mock_response.status_code = 200
|
|
|
|
with patch.object(client, '_send_request', return_value=mock_response) as mock_send:
|
|
response = client.get_app_logs_chart(
|
|
appId="app-123",
|
|
dateStart="2024-01-01",
|
|
dateEnd="2024-01-31",
|
|
offset=5
|
|
)
|
|
|
|
assert mock_send.call_args[1]['json']['offset'] == 5
|
|
|
|
def test_get_app_logs_chart_with_source_list(self, api_key):
|
|
"""Test getting app logs chart with custom source list."""
|
|
client = AppClient(api_key)
|
|
|
|
mock_response = Mock(spec=httpx.Response)
|
|
mock_response.status_code = 200
|
|
|
|
sources = ["api", "online", "share"]
|
|
|
|
with patch.object(client, '_send_request', return_value=mock_response) as mock_send:
|
|
response = client.get_app_logs_chart(
|
|
appId="app-123",
|
|
dateStart="2024-01-01",
|
|
dateEnd="2024-01-31",
|
|
source=sources
|
|
)
|
|
|
|
assert mock_send.call_args[1]['json']['source'] == sources
|
|
|
|
def test_get_app_logs_chart_with_all_timespans(self, api_key):
|
|
"""Test getting app logs chart with custom timespans."""
|
|
client = AppClient(api_key)
|
|
|
|
mock_response = Mock(spec=httpx.Response)
|
|
mock_response.status_code = 200
|
|
|
|
with patch.object(client, '_send_request', return_value=mock_response) as mock_send:
|
|
response = client.get_app_logs_chart(
|
|
appId="app-123",
|
|
dateStart="2024-01-01",
|
|
dateEnd="2024-01-31",
|
|
userTimespan="week",
|
|
chatTimespan="month",
|
|
appTimespan="day"
|
|
)
|
|
|
|
json_data = mock_send.call_args[1]['json']
|
|
assert json_data['userTimespan'] == "week"
|
|
assert json_data['chatTimespan'] == "month"
|
|
assert json_data['appTimespan'] == "day"
|
|
|
|
def test_get_app_logs_chart_all_parameters(self, api_key):
|
|
"""Test getting app logs chart with all parameters."""
|
|
client = AppClient(api_key)
|
|
|
|
mock_response = Mock(spec=httpx.Response)
|
|
mock_response.status_code = 200
|
|
|
|
with patch.object(client, '_send_request', return_value=mock_response) as mock_send:
|
|
response = client.get_app_logs_chart(
|
|
appId="app-123",
|
|
dateStart="2024-01-01",
|
|
dateEnd="2024-12-31",
|
|
offset=10,
|
|
source=["api", "online", "share", "test"],
|
|
userTimespan="month",
|
|
chatTimespan="week",
|
|
appTimespan="day"
|
|
)
|
|
|
|
json_data = mock_send.call_args[1]['json']
|
|
assert json_data == {
|
|
"appId": "app-123",
|
|
"dateStart": "2024-01-01",
|
|
"dateEnd": "2024-12-31",
|
|
"offset": 10,
|
|
"source": ["api", "online", "share", "test"],
|
|
"userTimespan": "month",
|
|
"chatTimespan": "week",
|
|
"appTimespan": "day"
|
|
}
|
|
|
|
def test_get_app_logs_chart_source_none(self, api_key):
|
|
"""Test that source=None results in default ['api']."""
|
|
client = AppClient(api_key)
|
|
|
|
mock_response = Mock(spec=httpx.Response)
|
|
mock_response.status_code = 200
|
|
|
|
with patch.object(client, '_send_request', return_value=mock_response) as mock_send:
|
|
response = client.get_app_logs_chart(
|
|
appId="app-123",
|
|
dateStart="2024-01-01",
|
|
dateEnd="2024-01-31",
|
|
source=None
|
|
)
|
|
|
|
# When source is None, it defaults to ["api"]
|
|
assert mock_send.call_args[1]['json']['source'] == ["api"]
|
|
|
|
def test_get_app_logs_chart_default_timespans(self, api_key):
|
|
"""Test that default timespans are 'day'."""
|
|
client = AppClient(api_key)
|
|
|
|
mock_response = Mock(spec=httpx.Response)
|
|
mock_response.status_code = 200
|
|
|
|
with patch.object(client, '_send_request', return_value=mock_response) as mock_send:
|
|
response = client.get_app_logs_chart(
|
|
appId="app-123",
|
|
dateStart="2024-01-01",
|
|
dateEnd="2024-01-31"
|
|
)
|
|
|
|
json_data = mock_send.call_args[1]['json']
|
|
assert json_data['userTimespan'] == "day"
|
|
assert json_data['chatTimespan'] == "day"
|
|
assert json_data['appTimespan'] == "day"
|
|
|
|
def test_get_app_logs_chart_default_offset(self, api_key):
|
|
"""Test that default offset is 1."""
|
|
client = AppClient(api_key)
|
|
|
|
mock_response = Mock(spec=httpx.Response)
|
|
mock_response.status_code = 200
|
|
|
|
with patch.object(client, '_send_request', return_value=mock_response) as mock_send:
|
|
response = client.get_app_logs_chart(
|
|
appId="app-123",
|
|
dateStart="2024-01-01",
|
|
dateEnd="2024-01-31"
|
|
)
|
|
|
|
assert mock_send.call_args[1]['json']['offset'] == 1
|