40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
import pytest
|
|
from unittest.mock import patch, MagicMock
|
|
from plants.services.perenual import search_species
|
|
|
|
|
|
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_returns_empty_on_timeout(settings):
|
|
settings.PERENUAL_API_KEY = 'test-key'
|
|
with patch('plants.services.perenual.requests.get', side_effect=Exception('timeout')):
|
|
results = search_species('rose')
|
|
assert results == []
|
|
|
|
|
|
def test_search_returns_empty_on_bad_status(settings):
|
|
settings.PERENUAL_API_KEY = 'test-key'
|
|
mock_resp = MagicMock()
|
|
mock_resp.raise_for_status.side_effect = Exception('404')
|
|
with patch('plants.services.perenual.requests.get', return_value=mock_resp):
|
|
results = search_species('rose')
|
|
assert results == []
|