|
| 1 | +import pytest |
| 2 | +from aioresponses import aioresponses |
| 3 | +from apple_maps.client import AppleMapsApiClient, AppleMapsApiClientError, AppleMapsApiClientCommunicationError, AppleMapsApiClientAuthenticationError |
| 4 | + |
| 5 | +@pytest.fixture |
| 6 | +def client(): |
| 7 | + return AppleMapsApiClient( |
| 8 | + key_id="test_key_id", |
| 9 | + service_id="test_service_id", |
| 10 | + team_id="test_team_id", |
| 11 | + key_pem="test_key_pem", |
| 12 | + session=None, |
| 13 | + ) |
| 14 | + |
| 15 | +@pytest.mark.asyncio |
| 16 | +async def test_get_travel_time(client): |
| 17 | + with aioresponses() as m: |
| 18 | + m.get( |
| 19 | + "https://maps-api.apple.com/v1/etas?origin=37.7749,-122.4194&destination=34.0522,-118.2437&transportType=automobile", |
| 20 | + payload={"travelTime": 3600}, |
| 21 | + ) |
| 22 | + |
| 23 | + result = await client.get_travel_time(37.7749, -122.4194, 34.0522, -118.2437, "automobile") |
| 24 | + assert result["travelTime"] == 3600 |
| 25 | + |
| 26 | +@pytest.mark.asyncio |
| 27 | +async def test_get_maps_access_token(client): |
| 28 | + with aioresponses() as m: |
| 29 | + m.post( |
| 30 | + "https://maps-api.apple.com/v1/token", |
| 31 | + payload={"access_token": "test_token", "expires_in": 3600}, |
| 32 | + ) |
| 33 | + |
| 34 | + result = await client.get_maps_access_token() |
| 35 | + assert result["access_token"] == "test_token" |
| 36 | + assert result["expires_in"] == 3600 |
| 37 | + |
| 38 | +@pytest.mark.asyncio |
| 39 | +async def test_get_travel_time_authentication_error(client): |
| 40 | + with aioresponses() as m: |
| 41 | + m.get( |
| 42 | + "https://maps-api.apple.com/v1/etas?origin=37.7749,-122.4194&destination=34.0522,-118.2437&transportType=automobile", |
| 43 | + status=401, |
| 44 | + body="Invalid credentials", |
| 45 | + ) |
| 46 | + |
| 47 | + with pytest.raises(AppleMapsApiClientAuthenticationError): |
| 48 | + await client.get_travel_time(37.7749, -122.4194, 34.0522, -118.2437, "automobile") |
| 49 | + |
| 50 | +@pytest.mark.asyncio |
| 51 | +async def test_get_travel_time_communication_error(client): |
| 52 | + with aioresponses() as m: |
| 53 | + m.get( |
| 54 | + "https://maps-api.apple.com/v1/etas?origin=37.7749,-122.4194&destination=34.0522,-118.2437&transportType=automobile", |
| 55 | + exception=Exception("Communication error"), |
| 56 | + ) |
| 57 | + |
| 58 | + with pytest.raises(AppleMapsApiClientCommunicationError): |
| 59 | + await client.get_travel_time(37.7749, -122.4194, 34.0522, -118.2437, "automobile") |
0 commit comments