Timeout and rate-limit failures now show a warning message to the user. Timeout increased to 10s. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
49 lines
1.7 KiB
Python
49 lines
1.7 KiB
Python
import pytest
|
|
import requests as req_lib
|
|
from unittest.mock import patch, MagicMock
|
|
from plants.services.perenual import search_species, PerenualError
|
|
|
|
|
|
def test_search_returns_empty_without_api_key(settings):
|
|
settings.PERENUAL_API_KEY = ''
|
|
results = search_species('rose')
|
|
assert results == []
|
|
|
|
|
|
def test_search_returns_data(settings):
|
|
settings.PERENUAL_API_KEY = 'test-key'
|
|
mock_resp = MagicMock()
|
|
mock_resp.json.return_value = {
|
|
'data': [{'id': 1, 'common_name': 'Rose', 'scientific_name': ['Rosa multiflora']}]
|
|
}
|
|
mock_resp.raise_for_status = MagicMock()
|
|
|
|
with patch('plants.services.perenual.requests.get', return_value=mock_resp):
|
|
results = search_species('rose')
|
|
|
|
assert len(results) == 1
|
|
assert results[0]['common_name'] == 'Rose'
|
|
|
|
|
|
def test_search_raises_on_timeout(settings):
|
|
settings.PERENUAL_API_KEY = 'test-key'
|
|
with patch('plants.services.perenual.requests.get', side_effect=req_lib.Timeout):
|
|
with pytest.raises(PerenualError, match='timed out'):
|
|
search_species('rose')
|
|
|
|
|
|
def test_search_raises_on_rate_limit(settings):
|
|
settings.PERENUAL_API_KEY = 'test-key'
|
|
mock_resp = MagicMock()
|
|
mock_resp.status_code = 429
|
|
mock_resp.raise_for_status.side_effect = req_lib.HTTPError(response=mock_resp)
|
|
with patch('plants.services.perenual.requests.get', return_value=mock_resp):
|
|
with pytest.raises(PerenualError, match='rate limit'):
|
|
search_species('rose')
|
|
|
|
|
|
def test_search_raises_on_other_error(settings):
|
|
settings.PERENUAL_API_KEY = 'test-key'
|
|
with patch('plants.services.perenual.requests.get', side_effect=Exception('network error')):
|
|
with pytest.raises(PerenualError):
|
|
search_species('rose')
|