|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | + |
| 6 | +from app.models.models import Holiday |
| 7 | +from app.services.holiday_providers.argentina_api_provider import ArgentinaApiProvider |
| 8 | + |
| 9 | + |
| 10 | +class TestArgentinaApiProvider: |
| 11 | + """ |
| 12 | + Tests for the ArgentinaApiProvider. |
| 13 | + """ |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def mock_response(self): |
| 17 | + """Creates a mock response object.""" |
| 18 | + mock = MagicMock(spec=requests.Response) |
| 19 | + mock.raise_for_status.return_value = None |
| 20 | + return mock |
| 21 | + |
| 22 | + def test_get_holidays_success(self, mock_response): |
| 23 | + """ |
| 24 | + Test successful holiday parsing from API JSON response. |
| 25 | + """ |
| 26 | + year = 2025 |
| 27 | + mock_response.json.return_value = [ |
| 28 | + {"fecha": "2025-01-01", "nombre": "Año Nuevo", "tipo": "inamovible"}, |
| 29 | + { |
| 30 | + "fecha": "2025-05-01", |
| 31 | + "nombre": "Día del Trabajador", |
| 32 | + "tipo": "inamovible", |
| 33 | + }, |
| 34 | + ] |
| 35 | + provider = ArgentinaApiProvider(api_url="http://fake-api.com/{year}") |
| 36 | + with patch("requests.get", return_value=mock_response): |
| 37 | + holidays = provider.get_holidays(year) |
| 38 | + assert len(holidays) == 2 |
| 39 | + assert holidays[0].description == "Año Nuevo" |
| 40 | + assert holidays[0].date.year == 2025 |
| 41 | + |
| 42 | + def test_get_holidays_filter_by_year(self, mock_response): |
| 43 | + """ |
| 44 | + Test that holidays from other years are filtered out. |
| 45 | + """ |
| 46 | + year = 2025 |
| 47 | + mock_response.json.return_value = [ |
| 48 | + {"fecha": "2025-01-01", "nombre": "Correct Year", "tipo": "inamovible"}, |
| 49 | + {"fecha": "2024-12-31", "nombre": "Wrong Year", "tipo": "inamovible"}, |
| 50 | + ] |
| 51 | + provider = ArgentinaApiProvider(api_url="http://fake-api.com/{year}") |
| 52 | + with patch("requests.get", return_value=mock_response): |
| 53 | + holidays = provider.get_holidays(year) |
| 54 | + assert len(holidays) == 1 |
| 55 | + assert holidays[0].description == "Correct Year" |
| 56 | + |
| 57 | + def test_get_holidays_malformed_entry(self, mock_response): |
| 58 | + """ |
| 59 | + Test that malformed entries are skipped. |
| 60 | + """ |
| 61 | + year = 2025 |
| 62 | + mock_response.json.return_value = [ |
| 63 | + {"fecha": "2025-01-01", "nombre": "Good", "tipo": "inamovible"}, |
| 64 | + {"fecha": "", "nombre": "Bad Date", "tipo": "inamovible"}, |
| 65 | + {"fecha": "2025-01-02", "nombre": "", "tipo": "inamovible"}, |
| 66 | + ] |
| 67 | + provider = ArgentinaApiProvider(api_url="http://fake-api.com/{year}") |
| 68 | + with patch("requests.get", return_value=mock_response): |
| 69 | + holidays = provider.get_holidays(year) |
| 70 | + assert len(holidays) == 1 |
| 71 | + assert holidays[0].description == "Good" |
| 72 | + |
| 73 | + def test_get_holidays_network_error(self): |
| 74 | + """ |
| 75 | + Test that network errors are handled gracefully. |
| 76 | + """ |
| 77 | + provider = ArgentinaApiProvider(api_url="http://fake-api.com/{year}") |
| 78 | + with patch( |
| 79 | + "requests.get", side_effect=requests.RequestException("Network Error") |
| 80 | + ): |
| 81 | + holidays = provider.get_holidays(2025) |
| 82 | + assert holidays == [] |
| 83 | + |
| 84 | + def test_get_holidays_json_error(self, mock_response): |
| 85 | + """ |
| 86 | + Test that JSON decoding errors are handled gracefully. |
| 87 | + """ |
| 88 | + mock_response.json = MagicMock(side_effect=ValueError("Invalid JSON")) |
| 89 | + provider = ArgentinaApiProvider(api_url="http://fake-api.com/{year}") |
| 90 | + with patch("requests.get", return_value=mock_response): |
| 91 | + holidays = provider.get_holidays(2025) |
| 92 | + assert holidays == [] |
| 93 | + |
| 94 | + def test_get_holidays_parsing_exceptions(self, mock_response): |
| 95 | + """ |
| 96 | + Test that parsing exceptions (ValueError, AttributeError) are caught. |
| 97 | + """ |
| 98 | + year = 2025 |
| 99 | + mock_response.json.return_value = [ |
| 100 | + # Good entry |
| 101 | + {"fecha": "2025-01-01", "nombre": "Good", "tipo": "inamovible"}, |
| 102 | + # ValueError: Invalid date format (strptime fails) |
| 103 | + { |
| 104 | + "fecha": "invalid-01-01", |
| 105 | + "nombre": "Bad Date Format", |
| 106 | + "tipo": "inamovible", |
| 107 | + }, |
| 108 | + # AttributeError: Entry is not a dict (no .get method) |
| 109 | + "invalid_string_entry", |
| 110 | + ] |
| 111 | + provider = ArgentinaApiProvider(api_url="http://fake-api.com/{year}") |
| 112 | + with patch("requests.get", return_value=mock_response): |
| 113 | + holidays = provider.get_holidays(year) |
| 114 | + # Should only contain the valid holiday |
| 115 | + assert len(holidays) == 1 |
| 116 | + assert holidays[0].description == "Good" |
0 commit comments