Add type hints to rest tests (#81304)

This commit is contained in:
epenet 2022-10-31 15:30:29 +01:00 committed by GitHub
parent 82151bfd40
commit a0ed91e30c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 130 additions and 74 deletions

View file

@ -4,6 +4,7 @@ from http import HTTPStatus
from unittest.mock import MagicMock, patch
import httpx
import pytest
import respx
from homeassistant import config as hass_config
@ -31,14 +32,14 @@ from homeassistant.setup import async_setup_component
from tests.common import get_fixture_path
async def test_setup_missing_config(hass):
async def test_setup_missing_config(hass: HomeAssistant) -> None:
"""Test setup with configuration missing required entries."""
assert await async_setup_component(hass, DOMAIN, {"sensor": {"platform": "rest"}})
await hass.async_block_till_done()
assert len(hass.states.async_all("sensor")) == 0
async def test_setup_missing_schema(hass):
async def test_setup_missing_schema(hass: HomeAssistant) -> None:
"""Test setup with resource missing schema."""
assert await async_setup_component(
hass,
@ -50,7 +51,9 @@ async def test_setup_missing_schema(hass):
@respx.mock
async def test_setup_failed_connect(hass, caplog):
async def test_setup_failed_connect(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test setup when connection error occurs."""
respx.get("http://localhost").mock(
side_effect=httpx.RequestError("server offline", request=MagicMock())
@ -72,7 +75,7 @@ async def test_setup_failed_connect(hass, caplog):
@respx.mock
async def test_setup_timeout(hass):
async def test_setup_timeout(hass: HomeAssistant) -> None:
"""Test setup when connection timeout occurs."""
respx.get("http://localhost").mock(side_effect=asyncio.TimeoutError())
assert await async_setup_component(
@ -85,7 +88,7 @@ async def test_setup_timeout(hass):
@respx.mock
async def test_setup_minimum(hass):
async def test_setup_minimum(hass: HomeAssistant) -> None:
"""Test setup with minimum configuration."""
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
@ -104,7 +107,7 @@ async def test_setup_minimum(hass):
@respx.mock
async def test_manual_update(hass):
async def test_manual_update(hass: HomeAssistant) -> None:
"""Test setup with minimum configuration."""
await async_setup_component(hass, "homeassistant", {})
respx.get("http://localhost").respond(
@ -140,7 +143,7 @@ async def test_manual_update(hass):
@respx.mock
async def test_setup_minimum_resource_template(hass):
async def test_setup_minimum_resource_template(hass: HomeAssistant) -> None:
"""Test setup with minimum configuration (resource_template)."""
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
@ -158,7 +161,7 @@ async def test_setup_minimum_resource_template(hass):
@respx.mock
async def test_setup_duplicate_resource_template(hass):
async def test_setup_duplicate_resource_template(hass: HomeAssistant) -> None:
"""Test setup with duplicate resources."""
respx.get("http://localhost") % HTTPStatus.OK
assert await async_setup_component(
@ -177,7 +180,7 @@ async def test_setup_duplicate_resource_template(hass):
@respx.mock
async def test_setup_get(hass):
async def test_setup_get(hass: HomeAssistant) -> None:
"""Test setup with valid configuration."""
respx.get("http://localhost").respond(status_code=HTTPStatus.OK, json={})
assert await async_setup_component(
@ -223,7 +226,9 @@ async def test_setup_get(hass):
@respx.mock
async def test_setup_timestamp(hass, caplog):
async def test_setup_timestamp(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test setup with valid configuration."""
respx.get("http://localhost").respond(
status_code=HTTPStatus.OK, json={"key": "2021-11-11 11:39Z"}
@ -286,7 +291,7 @@ async def test_setup_timestamp(hass, caplog):
@respx.mock
async def test_setup_get_templated_headers_params(hass):
async def test_setup_get_templated_headers_params(hass: HomeAssistant) -> None:
"""Test setup with valid configuration."""
respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
@ -320,7 +325,7 @@ async def test_setup_get_templated_headers_params(hass):
@respx.mock
async def test_setup_get_digest_auth(hass):
async def test_setup_get_digest_auth(hass: HomeAssistant) -> None:
"""Test setup with valid configuration."""
respx.get("http://localhost").respond(status_code=HTTPStatus.OK, json={})
assert await async_setup_component(
@ -349,7 +354,7 @@ async def test_setup_get_digest_auth(hass):
@respx.mock
async def test_setup_post(hass):
async def test_setup_post(hass: HomeAssistant) -> None:
"""Test setup with valid configuration."""
respx.post("http://localhost").respond(status_code=HTTPStatus.OK, json={})
assert await async_setup_component(
@ -378,7 +383,7 @@ async def test_setup_post(hass):
@respx.mock
async def test_setup_get_xml(hass):
async def test_setup_get_xml(hass: HomeAssistant) -> None:
"""Test setup with valid xml configuration."""
respx.get("http://localhost").respond(
status_code=HTTPStatus.OK,
@ -410,7 +415,7 @@ async def test_setup_get_xml(hass):
@respx.mock
async def test_setup_query_params(hass):
async def test_setup_query_params(hass: HomeAssistant) -> None:
"""Test setup with query params."""
respx.get("http://localhost", params={"search": "something"}) % HTTPStatus.OK
assert await async_setup_component(
@ -430,7 +435,7 @@ async def test_setup_query_params(hass):
@respx.mock
async def test_update_with_json_attrs(hass):
async def test_update_with_json_attrs(hass: HomeAssistant) -> None:
"""Test attributes get extracted from a JSON result."""
respx.get("http://localhost").respond(
@ -463,7 +468,7 @@ async def test_update_with_json_attrs(hass):
@respx.mock
async def test_update_with_no_template(hass):
async def test_update_with_no_template(hass: HomeAssistant) -> None:
"""Test update when there is no value template."""
respx.get("http://localhost").respond(
@ -495,7 +500,9 @@ async def test_update_with_no_template(hass):
@respx.mock
async def test_update_with_json_attrs_no_data(hass, caplog):
async def test_update_with_json_attrs_no_data(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test attributes when no JSON result fetched."""
respx.get("http://localhost").respond(
@ -531,7 +538,9 @@ async def test_update_with_json_attrs_no_data(hass, caplog):
@respx.mock
async def test_update_with_json_attrs_not_dict(hass, caplog):
async def test_update_with_json_attrs_not_dict(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test attributes get extracted from a JSON result."""
respx.get("http://localhost").respond(
@ -566,7 +575,9 @@ async def test_update_with_json_attrs_not_dict(hass, caplog):
@respx.mock
async def test_update_with_json_attrs_bad_JSON(hass, caplog):
async def test_update_with_json_attrs_bad_JSON(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test attributes get extracted from a JSON result."""
respx.get("http://localhost").respond(
@ -602,7 +613,7 @@ async def test_update_with_json_attrs_bad_JSON(hass, caplog):
@respx.mock
async def test_update_with_json_attrs_with_json_attrs_path(hass):
async def test_update_with_json_attrs_with_json_attrs_path(hass: HomeAssistant) -> None:
"""Test attributes get extracted from a JSON result with a template for the attributes."""
respx.get("http://localhost").respond(
@ -646,7 +657,9 @@ async def test_update_with_json_attrs_with_json_attrs_path(hass):
@respx.mock
async def test_update_with_xml_convert_json_attrs_with_json_attrs_path(hass):
async def test_update_with_xml_convert_json_attrs_with_json_attrs_path(
hass: HomeAssistant,
) -> None:
"""Test attributes get extracted from a JSON result that was converted from XML with a template for the attributes."""
respx.get("http://localhost").respond(
@ -682,7 +695,9 @@ async def test_update_with_xml_convert_json_attrs_with_json_attrs_path(hass):
@respx.mock
async def test_update_with_xml_convert_json_attrs_with_jsonattr_template(hass):
async def test_update_with_xml_convert_json_attrs_with_jsonattr_template(
hass: HomeAssistant,
) -> None:
"""Test attributes get extracted from a JSON result that was converted from XML."""
respx.get("http://localhost").respond(
@ -722,8 +737,8 @@ async def test_update_with_xml_convert_json_attrs_with_jsonattr_template(hass):
@respx.mock
async def test_update_with_application_xml_convert_json_attrs_with_jsonattr_template(
hass,
):
hass: HomeAssistant,
) -> None:
"""Test attributes get extracted from a JSON result that was converted from XML with application/xml mime type."""
respx.get("http://localhost").respond(
@ -759,7 +774,9 @@ async def test_update_with_application_xml_convert_json_attrs_with_jsonattr_temp
@respx.mock
async def test_update_with_xml_convert_bad_xml(hass, caplog):
async def test_update_with_xml_convert_bad_xml(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test attributes get extracted from a XML result with bad xml."""
respx.get("http://localhost").respond(
@ -794,7 +811,9 @@ async def test_update_with_xml_convert_bad_xml(hass, caplog):
@respx.mock
async def test_update_with_failed_get(hass, caplog):
async def test_update_with_failed_get(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test attributes get extracted from a XML result with bad xml."""
respx.get("http://localhost").respond(
@ -829,7 +848,7 @@ async def test_update_with_failed_get(hass, caplog):
@respx.mock
async def test_reload(hass):
async def test_reload(hass: HomeAssistant) -> None:
"""Verify we can reload reset sensors."""
respx.get("http://localhost") % HTTPStatus.OK