1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
"""Tests for accuweather package."""
from http import HTTPStatus
from typing import Any
import aiohttp
import pytest
from aiohttp import ClientSession
from aioresponses import aioresponses
from syrupy import SnapshotAssertion
from accuweather import (
AccuWeather,
ApiError,
InvalidApiKeyError,
InvalidCoordinatesError,
RequestsExceededError,
)
HEADERS = {"RateLimit-Remaining": "23"}
INVALID_API_KEY = "abcdef"
LATITUDE = 52.0677904
LOCATION_KEY = "268068"
LONGITUDE = 19.4795644
VALID_API_KEY = "32-character-string-1234567890qw"
@pytest.mark.asyncio()
async def test_get_location(location_data: dict[str, Any]) -> None:
"""Test with valid location data."""
session = aiohttp.ClientSession()
with aioresponses() as session_mock:
session_mock.get(
"https://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=32-character-string-1234567890qw&q=52.0677904%252C19.4795644&language=en-us",
payload=location_data,
headers=HEADERS,
)
accuweather = AccuWeather(
VALID_API_KEY, session, latitude=LATITUDE, longitude=LONGITUDE
)
await accuweather.async_get_location()
await session.close()
assert accuweather.location_name == "PiÄ…tek"
assert accuweather.location_key == "268068"
assert accuweather.requests_remaining == 23
@pytest.mark.asyncio()
async def test_get_current_conditions(
location_data: dict[str, Any],
current_condition_data: dict[str, Any],
snapshot: SnapshotAssertion,
) -> None:
"""Test with valid current condition data."""
session = aiohttp.ClientSession()
with aioresponses() as session_mock:
session_mock.get(
"https://dataservice.accuweather.com/currentconditions/v1/268068?apikey=32-character-string-1234567890qw&details=true&language=en-us",
payload=current_condition_data,
headers=HEADERS,
)
session_mock.get(
"https://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=32-character-string-1234567890qw&q=52.0677904%252C19.4795644&language=en-us",
payload=location_data,
headers=HEADERS,
)
accuweather = AccuWeather(
VALID_API_KEY, session, latitude=LATITUDE, longitude=LONGITUDE
)
current_conditions = await accuweather.async_get_current_conditions()
await session.close()
assert current_conditions == snapshot
assert accuweather.requests_remaining == 23
@pytest.mark.asyncio()
async def test_get_daily_forecast(
location_data: dict[str, Any],
daily_forecast_data: dict[str, Any],
snapshot: SnapshotAssertion,
) -> None:
"""Test with valid daily forecast data."""
session = aiohttp.ClientSession()
with aioresponses() as session_mock:
session_mock.get(
"https://dataservice.accuweather.com/forecasts/v1/daily/5day/268068?apikey=32-character-string-1234567890qw&details=true&metric=true&language=en-us",
payload=daily_forecast_data,
headers=HEADERS,
)
session_mock.get(
"https://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=32-character-string-1234567890qw&q=52.0677904%252C19.4795644&language=en-us",
payload=location_data,
headers=HEADERS,
)
accuweather = AccuWeather(
VALID_API_KEY, session, latitude=LATITUDE, longitude=LONGITUDE
)
forecast = await accuweather.async_get_daily_forecast()
await session.close()
assert forecast == snapshot
assert accuweather.requests_remaining == 23
@pytest.mark.asyncio()
async def test_get_hourly_forecast(
location_data: dict[str, Any],
hourly_forecast_data: list[dict[str, Any]],
snapshot: SnapshotAssertion,
) -> None:
"""Test with valid hourly forecast data."""
session = aiohttp.ClientSession()
with aioresponses() as session_mock:
session_mock.get(
"https://dataservice.accuweather.com/forecasts/v1/hourly/12hour/268068?apikey=32-character-string-1234567890qw&details=true&metric=true&language=en-us",
payload=hourly_forecast_data,
headers=HEADERS,
)
session_mock.get(
"https://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=32-character-string-1234567890qw&q=52.0677904%252C19.4795644&language=en-us",
payload=location_data,
headers=HEADERS,
)
accuweather = AccuWeather(
VALID_API_KEY, session, latitude=LATITUDE, longitude=LONGITUDE
)
forecast = await accuweather.async_get_hourly_forecast()
await session.close()
assert forecast == snapshot
assert accuweather.requests_remaining == 23
@pytest.mark.asyncio()
async def test_invalid_api_key_1() -> None:
"""Test with invalid API key."""
async with ClientSession() as session:
with pytest.raises(
InvalidApiKeyError,
match="Your API Key must be a 32-character hexadecimal string",
):
AccuWeather(
INVALID_API_KEY, session, latitude=LATITUDE, longitude=LONGITUDE
)
@pytest.mark.asyncio()
async def test_invalid_api_key_2() -> None:
"""Test with invalid API key."""
session = aiohttp.ClientSession()
with aioresponses() as session_mock:
session_mock.get(
"https://dataservice.accuweather.com/currentconditions/v1/268068?apikey=32-character-string-1234567890qw&details=true&language=en-us",
status=HTTPStatus.UNAUTHORIZED.value,
)
accuweather = AccuWeather(VALID_API_KEY, session, location_key=LOCATION_KEY)
with pytest.raises(InvalidApiKeyError, match="Invalid API key"):
await accuweather.async_get_current_conditions()
await session.close()
@pytest.mark.asyncio()
async def test_invalid_coordinates_1() -> None:
"""Test with invalid coordinates."""
async with ClientSession() as session:
with pytest.raises(
InvalidCoordinatesError, match="Your coordinates are invalid"
):
AccuWeather(VALID_API_KEY, session, latitude=55.55, longitude="78.00")
@pytest.mark.asyncio()
async def test_invalid_coordinates_2() -> None:
"""Test with invalid coordinates."""
async with ClientSession() as session:
with pytest.raises(
InvalidCoordinatesError, match="Your coordinates are invalid"
):
AccuWeather(VALID_API_KEY, session, latitude=199.99, longitude=90.0)
@pytest.mark.asyncio()
async def test_api_error() -> None:
"""Test with API error."""
payload = {
"Code": "ServiceError",
"Message": "API error.",
}
session = aiohttp.ClientSession()
with aioresponses() as session_mock:
session_mock.get(
"https://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=32-character-string-1234567890qw&q=52.0677904%252C19.4795644&language=en-us",
payload=payload,
status=404,
)
accuweather = AccuWeather(
VALID_API_KEY, session, latitude=LATITUDE, longitude=LONGITUDE
)
with pytest.raises(
ApiError, match="Invalid response from AccuWeather API: 404"
):
await accuweather.async_get_location()
await session.close()
@pytest.mark.asyncio()
async def test_requests_exceeded_error() -> None:
"""Test with requests exceeded error."""
payload = {
"Code": "ServiceUnavailable",
"Message": "The allowed number of requests has been exceeded.",
}
session = aiohttp.ClientSession()
with aioresponses() as session_mock:
session_mock.get(
"https://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=32-character-string-1234567890qw&q=52.0677904%252C19.4795644&language=en-us",
payload=payload,
status=503,
)
accuweather = AccuWeather(
VALID_API_KEY, session, latitude=LATITUDE, longitude=LONGITUDE
)
with pytest.raises(
RequestsExceededError,
match="The allowed number of requests has been exceeded",
):
await accuweather.async_get_location()
await session.close()
|