# ================================================================ # services/data/tests/test_external.py # ================================================================ """Tests for external API clients""" import pytest from unittest.mock import AsyncMock, patch from datetime import datetime, timedelta from app.external.aemet import AEMETClient from app.external.madrid_opendata import MadridOpenDataClient @pytest.mark.asyncio async def test_aemet_client_synthetic_weather(): """Test AEMET client synthetic weather generation""" client = AEMETClient() weather = await client._generate_synthetic_weather() assert weather is not None assert 'temperature' in weather assert 'precipitation' in weather assert 'humidity' in weather assert weather['source'] == 'synthetic' @pytest.mark.asyncio async def test_aemet_client_synthetic_forecast(): """Test AEMET client synthetic forecast""" client = AEMETClient() forecast = await client._generate_synthetic_forecast(7) assert len(forecast) == 7 assert all('forecast_date' in item for item in forecast) assert all('temperature' in item for item in forecast) assert all(item['source'] == 'synthetic' for item in forecast) @pytest.mark.asyncio async def test_madrid_client_synthetic_traffic(): """Test Madrid Open Data client synthetic traffic""" client = MadridOpenDataClient() traffic = await client._generate_synthetic_traffic(40.4168, -3.7038) assert traffic is not None assert 'traffic_volume' in traffic assert 'pedestrian_count' in traffic assert 'congestion_level' in traffic assert traffic['congestion_level'] in ['low', 'medium', 'high'] @pytest.mark.asyncio async def test_madrid_client_historical_traffic(): """Test Madrid Open Data client historical traffic""" client = MadridOpenDataClient() start_date = datetime.now() - timedelta(days=7) end_date = datetime.now() historical = await client._generate_historical_traffic( 40.4168, -3.7038, start_date, end_date ) assert len(historical) > 0 assert all('traffic_volume' in item for item in historical) assert all('date' in item for item in historical) @pytest.mark.asyncio async def test_distance_calculation(): """Test distance calculation in AEMET client""" client = AEMETClient() # Distance between Madrid Centro and Madrid Norte (approximately) distance = client._calculate_distance(40.4168, -3.7038, 40.4518, -3.7246) # Should be roughly 4-5 km assert 3 < distance < 6 @pytest.mark.asyncio async def test_madrid_area_detection(): """Test Madrid area detection""" client = AEMETClient() # Madrid coordinates should be detected as Madrid area assert client._is_in_madrid_area(40.4168, -3.7038) == True # Barcelona coordinates should not assert client._is_in_madrid_area(41.3851, 2.1734) == False