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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
"""Tests for the WAQI Library."""
from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING
import aiohttp
from aiohttp.web_request import BaseRequest
from aresponses import Response, ResponsesMockServer
import pytest
from aiowaqi import (
WAQIAirQuality,
WAQIAuthenticationError,
WAQIClient,
WAQIConnectionError,
WAQIError,
WAQISearchResult,
WAQIUnknownStationError,
)
from . import load_fixture
if TYPE_CHECKING:
from syrupy import SnapshotAssertion
WAQI_URL = "api.waqi.info"
@pytest.mark.parametrize(
"city",
[
"utrecht",
"maarssen",
"klundert",
"olivias",
"failing_klundert",
],
)
async def test_by_city(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
city: str,
snapshot: SnapshotAssertion,
) -> None:
"""Test retrieving air quality by city."""
aresponses.add(
WAQI_URL,
f"/feed/{city}?token=test",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture(f"city_feed_{city}.json"),
),
match_querystring=True,
)
response: WAQIAirQuality = await authenticated_client.get_by_city(city)
assert response == snapshot
async def test_new_dominant_pol(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test retrieving new dominant pol."""
aresponses.add(
WAQI_URL,
"/feed/maarssen?token=test",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture("city_feed_new_dominant_pol.json"),
),
match_querystring=True,
)
await authenticated_client.get_by_city("maarssen")
assert (
"'h' is an unsupported value for <enum 'Pollutant'>,"
" please report this at https://github.com/joostlek/python-waqi/issues"
in caplog.text
)
async def test_unknown_dominant_pol(
aresponses: ResponsesMockServer,
authenticated_client: WAQIClient,
) -> None:
"""Test retrieving unknown dominant pol."""
aresponses.add(
WAQI_URL,
"/feed/maarssen?token=test",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture("city_feed_unknown_dominant_pol.json"),
),
match_querystring=True,
)
air_quality = await authenticated_client.get_by_city("maarssen")
assert air_quality.dominant_pollutant is None
async def test_own_session(
aresponses: ResponsesMockServer,
) -> None:
"""Test creating own session."""
aresponses.add(
WAQI_URL,
"/feed/utrecht",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture("city_feed_utrecht.json"),
),
)
async with WAQIClient() as waqi:
assert waqi.session is None
waqi.authenticate("test")
await waqi.get_by_city("utrecht")
assert waqi.session is not None
async def test_unexpected_server_response(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
) -> None:
"""Test handling unexpected response."""
aresponses.add(
WAQI_URL,
"/feed/utrecht",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "plain/text"},
text="Yes",
),
)
with pytest.raises(WAQIError):
assert await authenticated_client.get_by_city("utrecht")
async def test_unknown_city(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
) -> None:
"""Test unknown city."""
aresponses.add(
WAQI_URL,
"/feed/unknown",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture("city_feed_unknown.json"),
),
)
with pytest.raises(WAQIError):
assert await authenticated_client.get_by_city("unknown")
async def test_unauthenticated(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
) -> None:
"""Test unauthenticated."""
aresponses.add(
WAQI_URL,
"/feed/utrecht",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture("unauthenticated.json"),
),
)
with pytest.raises(WAQIAuthenticationError):
assert await authenticated_client.get_by_city("utrecht")
async def test_timeout(aresponses: ResponsesMockServer) -> None:
"""Test request timeout."""
# Faking a timeout by sleeping
async def response_handler(_: BaseRequest) -> Response:
"""Response handler for this test."""
await asyncio.sleep(2)
return aresponses.Response(body="Goodmorning!")
aresponses.add(
WAQI_URL,
"/feed/utrecht",
"GET",
response_handler,
)
async with aiohttp.ClientSession() as session:
waqi = WAQIClient(session=session, request_timeout=1)
waqi.authenticate("test")
with pytest.raises(WAQIConnectionError):
assert await waqi.get_by_city("utrecht")
await waqi.close()
@pytest.mark.parametrize(
"keyword",
[
"klundert",
"failing_klundert",
"unknown",
],
)
async def test_search(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
keyword: str,
snapshot: SnapshotAssertion,
) -> None:
"""Test searching stations."""
aresponses.add(
WAQI_URL,
f"/search/?keyword={keyword}&token=test",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture(f"search_{keyword}.json"),
),
match_querystring=True,
)
response: list[WAQISearchResult] = await authenticated_client.search(keyword)
assert response == snapshot
@pytest.mark.parametrize(
"name",
[
"klundert",
"failing_klundert",
],
)
async def test_get_by_name(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
name: str,
snapshot: SnapshotAssertion,
) -> None:
"""Test getting stations by name."""
aresponses.add(
WAQI_URL,
f"/feed/{name}?token=test",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture(f"name_feed_{name}.json"),
),
match_querystring=True,
)
response = await authenticated_client.get_by_name(name)
assert response == snapshot
async def test_get_unknown_by_name(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
) -> None:
"""Test getting unknown station by name."""
aresponses.add(
WAQI_URL,
"/feed/unknown",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture("name_feed_unknown.json"),
),
)
with pytest.raises(WAQIUnknownStationError):
await authenticated_client.get_by_name("unknown")
@pytest.mark.parametrize(
"station_number",
[
6337,
372382,
10142,
10002,
],
)
async def test_get_by_station_number(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
station_number: int,
snapshot: SnapshotAssertion,
) -> None:
"""Test getting stations by station_number."""
aresponses.add(
WAQI_URL,
f"/feed/@{station_number}?token=test",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture(f"station_number_feed_{station_number}.json"),
),
match_querystring=True,
)
response = await authenticated_client.get_by_station_number(station_number)
assert response == snapshot
@pytest.mark.parametrize(
"identifier",
[
"unknown",
"@123946",
"A10142",
],
)
async def test_get_unknown_by_station_number(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
identifier: str,
) -> None:
"""Test getting unknown station by station_number."""
aresponses.add(
WAQI_URL,
"/feed/@0",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture(f"station_number_feed_{identifier}.json"),
),
)
with pytest.raises(WAQIUnknownStationError):
await authenticated_client.get_by_station_number(0)
async def test_get_by_coordinates(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
) -> None:
"""Test getting measuring station via coordinates."""
aresponses.add(
WAQI_URL,
"/feed/geo:52.105031;5.124464",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture("coordinates.json"),
),
)
response = await authenticated_client.get_by_coordinates(52.105031, 5.124464)
assert response == snapshot
async def test_get_by_ip(
authenticated_client: WAQIClient,
aresponses: ResponsesMockServer,
snapshot: SnapshotAssertion,
) -> None:
"""Test getting measuring station via ip."""
aresponses.add(
WAQI_URL,
"/feed/here",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixture("here.json"),
),
)
response = await authenticated_client.get_by_ip()
assert response == snapshot
|