"""Unit tests for tasks for the Chartmetric Charts Ingestion Workflow.""" from unittest.mock import MagicMock, patch import requests from feed_ingestion.flows.chartmetric_charts import ows_charts def test_prime_chart_dates_success(): """Test prime_chart_dates() on success.""" chart_dates = [ { 'chart_id': '31918ccc-46d0-4eac-97ed-e864d196f499', 'chart_date': '2024-02-10' } ] with patch.object(ows_charts.request, 'process') as mock_request: mock_request.return_value = MagicMock( spec=requests.models.Response, status_code=200, json=lambda: {'ok': True}, ) response = ows_charts.prime_chart_dates(chart_dates) assert response assert response.message == { 'ok': True, } def test_prime_chart_dates_error_json(): """Test prime_chart_dates() on error with a JSON body.""" chart_dates = [ { 'chart_id': '31918ccc-46d0-4eac-97ed-e864d196f499', 'chart_date': '2024-02-10' } ] with patch.object(ows_charts.request, 'process') as mock_request: mock_request.return_value = MagicMock( spec=requests.models.Response, status_code=500, json=lambda: {'error': 'an error occurred'}, ) response = ows_charts.prime_chart_dates(chart_dates) assert not response assert response.errors['code'] == ows_charts.OWS_CHARTS_ERROR assert response.errors['message'] == { 'error': 'an error occurred', } def test_prime_chart_dates_error_text(): """Test prime_chart_dates() on error with a text body.""" chart_dates = [ { 'chart_id': '31918ccc-46d0-4eac-97ed-e864d196f499', 'chart_date': '2024-02-10' } ] with patch.object(ows_charts.request, 'process') as mock_request: mock_request.return_value = MagicMock( spec=requests.models.Response, status_code=500, content=b'an error occurred', json=MagicMock( side_effect=ValueError, ), ) response = ows_charts.prime_chart_dates(chart_dates) assert not response assert response.errors['code'] == ows_charts.OWS_CHARTS_ERROR assert response.errors['message'] == 'an error occurred'