| 1 |
"""Test the tankerkoenig API client.""" |
| 2 |
|
| 3 |
import asyncio |
| 4 |
|
| 5 |
import aiohttp |
| 6 |
from aioresponses import aioresponses |
| 7 |
import pytest |
| 8 |
from syrupy import SnapshotAssertion |
| 9 |
|
| 10 |
from aiotankerkoenig import ( |
| 11 |
GasType, |
| 12 |
Sort, |
| 13 |
Tankerkoenig, |
| 14 |
TankerkoenigConnectionError, |
| 15 |
TankerkoenigConnectionTimeoutError, |
| 16 |
TankerkoenigError, |
| 17 |
TankerkoenigInvalidKeyError, |
| 18 |
TankerkoenigRateLimitError, |
| 19 |
) |
| 20 |
from tests import load_fixture |
| 21 |
|
| 22 |
TANKERKOENIG_ENDPOINT = "https://creativecommons.tankerkoenig.de" |
| 23 |
|
| 24 |
|
| 25 |
async def test_unexpected_server_response( |
| 26 |
responses: aioresponses, |
| 27 |
tankerkoenig_client: Tankerkoenig, |
| 28 |
) -> None: |
| 29 |
"""Test handling unexpected response.""" |
| 30 |
responses.get( |
| 31 |
f"{TANKERKOENIG_ENDPOINT}/json/detail.php?apikey=abc123&id=1", |
| 32 |
status=200, |
| 33 |
headers={"Content-Type": "plain/text"}, |
| 34 |
body="Yes", |
| 35 |
) |
| 36 |
with pytest.raises(TankerkoenigError): |
| 37 |
await tankerkoenig_client.station_details(station_id="1") |
| 38 |
|
| 39 |
|
| 40 |
async def test_non_200_response( |
| 41 |
responses: aioresponses, |
| 42 |
tankerkoenig_client: Tankerkoenig, |
| 43 |
) -> None: |
| 44 |
"""Test handling unexpected response.""" |
| 45 |
responses.get( |
| 46 |
f"{TANKERKOENIG_ENDPOINT}/json/detail.php?apikey=abc123&id=1", |
| 47 |
status=500, |
| 48 |
headers={"Content-Type": "plain/text"}, |
| 49 |
body="Yes", |
| 50 |
) |
| 51 |
with pytest.raises(TankerkoenigConnectionError): |
| 52 |
await tankerkoenig_client.station_details(station_id="1") |
| 53 |
|
| 54 |
|
| 55 |
async def test_api_rate_limit( |
| 56 |
responses: aioresponses, |
| 57 |
tankerkoenig_client: Tankerkoenig, |
| 58 |
) -> None: |
| 59 |
"""Test handling api rate limit response.""" |
| 60 |
responses.get( |
| 61 |
f"{TANKERKOENIG_ENDPOINT}/json/detail.php?apikey=abc123&id=1", |
| 62 |
status=503, |
| 63 |
headers={"Content-Type": "plain/text"}, |
| 64 |
body="Yes", |
| 65 |
) |
| 66 |
with pytest.raises(TankerkoenigRateLimitError): |
| 67 |
await tankerkoenig_client.station_details(station_id="1") |
| 68 |
|
| 69 |
|
| 70 |
@pytest.mark.parametrize( |
| 71 |
"message", |
| 72 |
[ |
| 73 |
"API-Key existiert nicht", |
| 74 |
"APIKey existiert nicht.", |
| 75 |
], |
| 76 |
) |
| 77 |
async def test_invalid_apikey_response( |
| 78 |
responses: aioresponses, |
| 79 |
tankerkoenig_client: Tankerkoenig, |
| 80 |
message: str, |
| 81 |
) -> None: |
| 82 |
"""Test handling of invalid api key response.""" |
| 83 |
responses.get( |
| 84 |
f"{TANKERKOENIG_ENDPOINT}/json/detail.php?apikey=abc123&id=1", |
| 85 |
status=200, |
| 86 |
body='{"status": "error", "ok": false, "message": "' + message + '"}', |
| 87 |
) |
| 88 |
with pytest.raises(TankerkoenigInvalidKeyError): |
| 89 |
await tankerkoenig_client.station_details(station_id="1") |
| 90 |
|
| 91 |
|
| 92 |
async def test_general_error_response( |
| 93 |
responses: aioresponses, |
| 94 |
tankerkoenig_client: Tankerkoenig, |
| 95 |
) -> None: |
| 96 |
"""Test handling of a general error response.""" |
| 97 |
responses.get( |
| 98 |
f"{TANKERKOENIG_ENDPOINT}/json/detail.php?apikey=abc123&id=1", |
| 99 |
status=200, |
| 100 |
body='{"status": "error", "ok": false, "message": "Idk what happened!"}', |
| 101 |
) |
| 102 |
with pytest.raises(TankerkoenigError): |
| 103 |
await tankerkoenig_client.station_details(station_id="1") |
| 104 |
|
| 105 |
|
| 106 |
async def test_putting_in_own_session( |
| 107 |
responses: aioresponses, |
| 108 |
) -> None: |
| 109 |
"""Test putting in own session.""" |
| 110 |
responses.get( |
| 111 |
f"{TANKERKOENIG_ENDPOINT}/json/detail.php?apikey=abc123&id=1", |
| 112 |
status=200, |
| 113 |
body=load_fixture("detail.json"), |
| 114 |
) |
| 115 |
async with aiohttp.ClientSession() as session: |
| 116 |
tankerkoenig = Tankerkoenig(api_key="abc123", session=session) |
| 117 |
await tankerkoenig.station_details(station_id="1") |
| 118 |
assert tankerkoenig.session is not None |
| 119 |
assert not tankerkoenig.session.closed |
| 120 |
await tankerkoenig.close() |
| 121 |
assert not tankerkoenig.session.closed |
| 122 |
|
| 123 |
|
| 124 |
async def test_creating_own_session( |
| 125 |
responses: aioresponses, |
| 126 |
) -> None: |
| 127 |
"""Test creating own session.""" |
| 128 |
responses.get( |
| 129 |
f"{TANKERKOENIG_ENDPOINT}/json/detail.php?apikey=abc123&id=1", |
| 130 |
status=200, |
| 131 |
body=load_fixture("detail.json"), |
| 132 |
) |
| 133 |
tankerkoenig = Tankerkoenig(api_key="abc123") |
| 134 |
await tankerkoenig.station_details(station_id="1") |
| 135 |
assert tankerkoenig.session is not None |
| 136 |
assert not tankerkoenig.session.closed |
| 137 |
await tankerkoenig.close() |
| 138 |
assert tankerkoenig.session.closed |
| 139 |
|
| 140 |
|
| 141 |
async def test_station_details( |
| 142 |
responses: aioresponses, |
| 143 |
tankerkoenig_client: Tankerkoenig, |
| 144 |
snapshot: SnapshotAssertion, |
| 145 |
) -> None: |
| 146 |
"""Test retrieving station details.""" |
| 147 |
responses.get( |
| 148 |
f"{TANKERKOENIG_ENDPOINT}/json/detail.php?apikey=abc123&id=1", |
| 149 |
status=200, |
| 150 |
body=load_fixture("detail.json"), |
| 151 |
) |
| 152 |
assert await tankerkoenig_client.station_details(station_id="1") == snapshot |
| 153 |
|
| 154 |
|
| 155 |
async def test_nearby_stations( |
| 156 |
responses: aioresponses, |
| 157 |
tankerkoenig_client: Tankerkoenig, |
| 158 |
snapshot: SnapshotAssertion, |
| 159 |
) -> None: |
| 160 |
"""Test retrieving nearby stations.""" |
| 161 |
responses.get( |
| 162 |
f"{TANKERKOENIG_ENDPOINT}/json/list.php?apikey=abc123&lat=53.1&lng=7.8&rad=5&sort=dist&type=all", |
| 163 |
status=200, |
| 164 |
body=load_fixture("list.json"), |
| 165 |
) |
| 166 |
assert ( |
| 167 |
await tankerkoenig_client.nearby_stations( |
| 168 |
coordinates=(53.1, 7.8), |
| 169 |
radius=5, |
| 170 |
gas_type=GasType.ALL, |
| 171 |
sort=Sort.DISTANCE, |
| 172 |
) |
| 173 |
== snapshot |
| 174 |
) |
| 175 |
|
| 176 |
|
| 177 |
async def test_prices( |
| 178 |
responses: aioresponses, |
| 179 |
tankerkoenig_client: Tankerkoenig, |
| 180 |
snapshot: SnapshotAssertion, |
| 181 |
) -> None: |
| 182 |
"""Test retrieving nearby stations.""" |
| 183 |
responses.get( |
| 184 |
f"{TANKERKOENIG_ENDPOINT}/json/prices.php?apikey=abc123&ids=4fd02fd5-0a4c-46b3-a9b9-a488a62454be", |
| 185 |
status=200, |
| 186 |
body=load_fixture("prices.json"), |
| 187 |
) |
| 188 |
assert ( |
| 189 |
await tankerkoenig_client.prices( |
| 190 |
station_ids=["4fd02fd5-0a4c-46b3-a9b9-a488a62454be"], |
| 191 |
) |
| 192 |
== snapshot |
| 193 |
) |
| 194 |
|
| 195 |
|
| 196 |
async def test_timeout( |
| 197 |
responses: aioresponses, |
| 198 |
) -> None: |
| 199 |
"""Test request timeout.""" |
| 200 |
responses.get( |
| 201 |
f"{TANKERKOENIG_ENDPOINT}/json/detail.php?apikey=abc123&id=1", |
| 202 |
timeout=True, |
| 203 |
) |
| 204 |
async with Tankerkoenig( |
| 205 |
api_key="abc123", |
| 206 |
) as tankerkoenig_client: |
| 207 |
with pytest.raises(TankerkoenigConnectionTimeoutError): |
| 208 |
await tankerkoenig_client.station_details(station_id="1") |
| 209 |
|
| 210 |
|
| 211 |
async def test_timeout_cancellation() -> None: |
| 212 |
"""Test request timeout raised as a cancelled task.""" |
| 213 |
|
| 214 |
class TimeoutSession: |
| 215 |
"""Session raising a timeout cancellation from get.""" |
| 216 |
|
| 217 |
def get( |
| 218 |
self, |
| 219 |
*_args: object, |
| 220 |
**_kwargs: object, |
| 221 |
) -> "TimeoutSession": |
| 222 |
"""Return self as an async context manager.""" |
| 223 |
return self |
| 224 |
|
| 225 |
async def __aenter__(self) -> None: |
| 226 |
"""Raise cancellation with a timeout reason.""" |
| 227 |
msg = "Global task timeout" |
| 228 |
raise asyncio.CancelledError(msg) |
| 229 |
|
| 230 |
async def __aexit__(self, *_exc_info: object) -> None: |
| 231 |
"""Async exit.""" |
| 232 |
|
| 233 |
tankerkoenig_client = Tankerkoenig( |
| 234 |
api_key="abc123", |
| 235 |
session=TimeoutSession(), # type: ignore[arg-type] |
| 236 |
) |
| 237 |
with pytest.raises(TankerkoenigConnectionTimeoutError): |
| 238 |
await tankerkoenig_client.station_details(station_id="1") |
| 239 |
|
| 240 |
|
| 241 |
async def test_non_timeout_cancellation() -> None: |
| 242 |
"""Test request cancellation without timeout reason.""" |
| 243 |
|
| 244 |
class CancelledSession: |
| 245 |
"""Session raising a non-timeout cancellation from get.""" |
| 246 |
|
| 247 |
def get( |
| 248 |
self, |
| 249 |
*_args: object, |
| 250 |
**_kwargs: object, |
| 251 |
) -> "CancelledSession": |
| 252 |
"""Return self as an async context manager.""" |
| 253 |
return self |
| 254 |
|
| 255 |
async def __aenter__(self) -> None: |
| 256 |
"""Raise cancellation without a timeout reason.""" |
| 257 |
raise asyncio.CancelledError |
| 258 |
|
| 259 |
async def __aexit__(self, *_exc_info: object) -> None: |
| 260 |
"""Async exit.""" |
| 261 |
|
| 262 |
tankerkoenig_client = Tankerkoenig( |
| 263 |
api_key="abc123", |
| 264 |
session=CancelledSession(), # type: ignore[arg-type] |
| 265 |
) |
| 266 |
with pytest.raises(asyncio.CancelledError): |
| 267 |
await tankerkoenig_client.station_details(station_id="1") |