|
6 | 6 | from http import HTTPMethod |
7 | 7 |
|
8 | 8 | import aiohttp |
| 9 | +import aiohttp_client_cache.session |
9 | 10 | import pytest |
10 | 11 | import requests |
11 | 12 | from multidict import CIMultiDict |
12 | 13 | from multidict import CIMultiDictProxy |
| 14 | +from requests_cache import CachedSession |
13 | 15 |
|
14 | 16 | from homeassistant_api import AsyncClient |
15 | 17 | from homeassistant_api import AsyncWebsocketClient |
|
28 | 30 | from homeassistant_api.errors import ResponseError |
29 | 31 | from homeassistant_api.errors import UnauthorizedError |
30 | 32 | from homeassistant_api.errors import UnexpectedStatusCodeError |
| 33 | +from homeassistant_api.models.states import State |
31 | 34 | from homeassistant_api.models.websocket import Error |
32 | 35 | from homeassistant_api.processing import async_process_response |
33 | 36 | from homeassistant_api.processing import process_response |
@@ -287,3 +290,114 @@ def test_error_model_without_optional_fields() -> None: |
287 | 290 | assert error.translation_key is None |
288 | 291 | assert error.translation_placeholders is None |
289 | 292 | assert error.translation_domain is None |
| 293 | + |
| 294 | + |
| 295 | +# --- Processing: async processor not found --- |
| 296 | + |
| 297 | + |
| 298 | +async def test_async_exception_processor_not_found_error() -> None: |
| 299 | + """Tests that async_process_response raises ProcessorNotFoundError for unknown MIME types.""" |
| 300 | + with pytest.raises(ProcessorNotFoundError, match="this_type/does-not-exist"): |
| 301 | + await async_process_response( |
| 302 | + make_async_response(200, "", {"Content-Type": "this_type/does-not-exist"}), |
| 303 | + ) |
| 304 | + |
| 305 | + |
| 306 | +async def test_async_exception_bad_request() -> None: |
| 307 | + """Tests that async_process_response raises RequestError for 400 responses.""" |
| 308 | + with pytest.raises(RequestError): |
| 309 | + await async_process_response( |
| 310 | + make_async_response(400, "bad request data", {}), |
| 311 | + ) |
| 312 | + |
| 313 | + |
| 314 | +async def test_async_exception_internal_server_error() -> None: |
| 315 | + """Tests that async_process_response raises InternalServerError for 500 responses.""" |
| 316 | + with pytest.raises(InternalServerError): |
| 317 | + await async_process_response(make_async_response(500, "server broke", {})) |
| 318 | + |
| 319 | + |
| 320 | +async def test_async_exception_unexpected_status_code() -> None: |
| 321 | + """Tests that async_process_response raises UnexpectedStatusCodeError for unknown status.""" |
| 322 | + with pytest.raises(UnexpectedStatusCodeError): |
| 323 | + await async_process_response(make_async_response(0, "", {})) |
| 324 | + |
| 325 | + |
| 326 | +# --- WebSocket: NotImplementedError stubs --- |
| 327 | + |
| 328 | + |
| 329 | +def test_websocket_set_state_not_supported(websocket_client: WebsocketClient) -> None: |
| 330 | + """Tests that WebsocketClient.set_state raises NotImplementedError.""" |
| 331 | + state = State(state="test", entity_id="sun.sun") |
| 332 | + with pytest.raises( |
| 333 | + NotImplementedError, |
| 334 | + match="not supported over the WebSocket API", |
| 335 | + ): |
| 336 | + websocket_client.set_state(state) |
| 337 | + |
| 338 | + |
| 339 | +def test_websocket_get_entity_histories_not_supported( |
| 340 | + websocket_client: WebsocketClient, |
| 341 | +) -> None: |
| 342 | + """Tests that WebsocketClient.get_entity_histories raises NotImplementedError.""" |
| 343 | + with pytest.raises( |
| 344 | + NotImplementedError, |
| 345 | + match="not supported over the WebSocket API", |
| 346 | + ): |
| 347 | + list(websocket_client.get_entity_histories()) |
| 348 | + |
| 349 | + |
| 350 | +async def test_async_websocket_set_state_not_supported( |
| 351 | + async_websocket_client: AsyncWebsocketClient, |
| 352 | +) -> None: |
| 353 | + """Tests that AsyncWebsocketClient.set_state raises NotImplementedError.""" |
| 354 | + state = State(state="test", entity_id="sun.sun") |
| 355 | + with pytest.raises( |
| 356 | + NotImplementedError, |
| 357 | + match="not supported over the WebSocket API", |
| 358 | + ): |
| 359 | + await async_websocket_client.set_state(state) |
| 360 | + |
| 361 | + |
| 362 | +async def test_async_websocket_get_entity_histories_not_supported( |
| 363 | + async_websocket_client: AsyncWebsocketClient, |
| 364 | +) -> None: |
| 365 | + """Tests that AsyncWebsocketClient.get_entity_histories raises NotImplementedError.""" |
| 366 | + with pytest.raises( |
| 367 | + NotImplementedError, |
| 368 | + match="not supported over the WebSocket API", |
| 369 | + ): |
| 370 | + async for _ in async_websocket_client.get_entity_histories(): |
| 371 | + pass |
| 372 | + |
| 373 | + |
| 374 | +# --- Client: no-cache session --- |
| 375 | + |
| 376 | + |
| 377 | +def test_client_no_cache_session() -> None: |
| 378 | + """Tests that Client can be created without a cache session.""" |
| 379 | + token = os.environ["HOMEASSISTANTAPI_TOKEN"] |
| 380 | + client = Client(HA_URL, token, use_cache=False) |
| 381 | + assert isinstance(client._session, requests.Session) |
| 382 | + assert not isinstance(client._session, CachedSession) |
| 383 | + |
| 384 | + |
| 385 | +def test_client_default_cache_session() -> None: |
| 386 | + """Tests that Client creates a CachedSession when use_cache=True.""" |
| 387 | + token = os.environ["HOMEASSISTANTAPI_TOKEN"] |
| 388 | + client = Client(HA_URL, token, use_cache=True) |
| 389 | + assert isinstance(client._session, CachedSession) |
| 390 | + |
| 391 | + |
| 392 | +async def test_async_client_no_cache_session() -> None: |
| 393 | + """Tests that AsyncClient can be created without a cache session.""" |
| 394 | + token = os.environ["HOMEASSISTANTAPI_TOKEN"] |
| 395 | + client = AsyncClient(HA_URL, token, use_cache=False) |
| 396 | + assert isinstance(client._session, aiohttp.ClientSession) |
| 397 | + |
| 398 | + |
| 399 | +async def test_async_client_default_cache_session() -> None: |
| 400 | + """Tests that AsyncClient creates a CachedSession when use_cache=True.""" |
| 401 | + token = os.environ["HOMEASSISTANTAPI_TOKEN"] |
| 402 | + client = AsyncClient(HA_URL, token, use_cache=True) |
| 403 | + assert isinstance(client._session, aiohttp_client_cache.session.CachedSession) |
0 commit comments