main: HA-Integration mit Exception-Handling und Testabdeckung
This commit is contained in:
60
tests/ha/test_ha_client.py
Normal file
60
tests/ha/test_ha_client.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
import requests
|
||||
|
||||
from app.ha.client import HaClient
|
||||
from app.ha.exceptions import HaAuthError, HaHttpError, HaTimeoutError, HaUnexpectedPayloadError
|
||||
from tests.helpers import build_ha_client_settings
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def client():
|
||||
settings = build_ha_client_settings()
|
||||
return HaClient(settings)
|
||||
|
||||
|
||||
def test_list_entities_timeout(client):
|
||||
with patch.object(client._session, "get", side_effect=requests.Timeout("t")):
|
||||
with pytest.raises(HaTimeoutError):
|
||||
client.list_entities()
|
||||
|
||||
|
||||
def test_list_entities_auth_error(client):
|
||||
response = MagicMock()
|
||||
response.status_code = 401
|
||||
response.raise_for_status = MagicMock()
|
||||
with patch.object(client._session, "get", return_value=response):
|
||||
with pytest.raises(HaAuthError):
|
||||
client.list_entities()
|
||||
|
||||
|
||||
def test_list_entities_http_error(client):
|
||||
response = MagicMock()
|
||||
response.status_code = 502
|
||||
response.raise_for_status = MagicMock(side_effect=requests.HTTPError("bad"))
|
||||
with patch.object(client._session, "get", return_value=response):
|
||||
with pytest.raises(HaHttpError):
|
||||
client.list_entities()
|
||||
|
||||
|
||||
def test_list_entities_invalid_json(client):
|
||||
response = MagicMock()
|
||||
response.status_code = 200
|
||||
response.raise_for_status = MagicMock()
|
||||
response.json = MagicMock(side_effect=ValueError("invalid json"))
|
||||
with patch.object(client._session, "get", return_value=response):
|
||||
with pytest.raises(HaUnexpectedPayloadError):
|
||||
client.list_entities()
|
||||
|
||||
|
||||
def test_list_entities_wrong_payload_type(client):
|
||||
response = MagicMock()
|
||||
response.status_code = 200
|
||||
response.raise_for_status = MagicMock()
|
||||
response.json = MagicMock(return_value={"data": []})
|
||||
with patch.object(client._session, "get", return_value=response):
|
||||
with pytest.raises(HaUnexpectedPayloadError):
|
||||
client.list_entities()
|
||||
Reference in New Issue
Block a user