Skip to content

Commit

Permalink
Add type hints to rest tests (home-assistant#81304)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Oct 31, 2022
1 parent 82151bf commit a0ed91e
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 74 deletions.
35 changes: 19 additions & 16 deletions tests/components/rest/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest.mock import MagicMock, patch

import httpx
import pytest
import respx

from homeassistant import config as hass_config
Expand All @@ -26,7 +27,7 @@
from tests.common import get_fixture_path


async def test_setup_missing_basic_config(hass):
async def test_setup_missing_basic_config(hass: HomeAssistant) -> None:
"""Test setup with configuration missing required entries."""
assert await async_setup_component(
hass, Platform.BINARY_SENSOR, {"binary_sensor": {"platform": "rest"}}
Expand All @@ -35,7 +36,7 @@ async def test_setup_missing_basic_config(hass):
assert len(hass.states.async_all("binary_sensor")) == 0


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,
Expand All @@ -53,7 +54,9 @@ async def test_setup_missing_config(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(
Expand All @@ -76,7 +79,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(
Expand All @@ -95,7 +98,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(
Expand All @@ -114,7 +117,7 @@ async def test_setup_minimum(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(
Expand All @@ -132,7 +135,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(
Expand All @@ -151,7 +154,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(
Expand Down Expand Up @@ -184,7 +187,7 @@ async def test_setup_get(hass):


@respx.mock
async def test_setup_get_template_headers_params(hass):
async def test_setup_get_template_headers_params(hass: HomeAssistant) -> None:
"""Test setup with valid configuration."""
respx.get("http://localhost").respond(status_code=200, json={})
assert await async_setup_component(
Expand Down Expand Up @@ -218,7 +221,7 @@ async def test_setup_get_template_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(
Expand Down Expand Up @@ -246,7 +249,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(
Expand Down Expand Up @@ -274,7 +277,7 @@ async def test_setup_post(hass):


@respx.mock
async def test_setup_get_off(hass):
async def test_setup_get_off(hass: HomeAssistant) -> None:
"""Test setup with valid off configuration."""
respx.get("http://localhost").respond(
status_code=HTTPStatus.OK,
Expand Down Expand Up @@ -304,7 +307,7 @@ async def test_setup_get_off(hass):


@respx.mock
async def test_setup_get_on(hass):
async def test_setup_get_on(hass: HomeAssistant) -> None:
"""Test setup with valid on configuration."""
respx.get("http://localhost").respond(
status_code=HTTPStatus.OK,
Expand Down Expand Up @@ -334,7 +337,7 @@ async def test_setup_get_on(hass):


@respx.mock
async def test_setup_with_exception(hass):
async def test_setup_with_exception(hass: HomeAssistant) -> None:
"""Test setup with exception."""
respx.get("http://localhost").respond(status_code=HTTPStatus.OK, json={})
assert await async_setup_component(
Expand Down Expand Up @@ -376,7 +379,7 @@ async def test_setup_with_exception(hass):


@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
Expand Down Expand Up @@ -416,7 +419,7 @@ async def test_reload(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(
Expand Down
13 changes: 7 additions & 6 deletions tests/components/rest/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
SERVICE_RELOAD,
STATE_UNAVAILABLE,
)
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow

from tests.common import async_fire_time_changed, get_fixture_path


@respx.mock
async def test_setup_with_endpoint_timeout_with_recovery(hass):
async def test_setup_with_endpoint_timeout_with_recovery(hass: HomeAssistant) -> None:
"""Test setup with an endpoint that times out that recovers."""
await async_setup_component(hass, "homeassistant", {})

Expand Down Expand Up @@ -129,7 +130,7 @@ async def test_setup_with_endpoint_timeout_with_recovery(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").respond(
Expand Down Expand Up @@ -187,7 +188,7 @@ async def test_setup_minimum_resource_template(hass):


@respx.mock
async def test_reload(hass):
async def test_reload(hass: HomeAssistant) -> None:
"""Verify we can reload."""

respx.get("http://localhost") % HTTPStatus.OK
Expand Down Expand Up @@ -236,7 +237,7 @@ async def test_reload(hass):


@respx.mock
async def test_reload_and_remove_all(hass):
async def test_reload_and_remove_all(hass: HomeAssistant) -> None:
"""Verify we can reload and remove all."""

respx.get("http://localhost") % HTTPStatus.OK
Expand Down Expand Up @@ -283,7 +284,7 @@ async def test_reload_and_remove_all(hass):


@respx.mock
async def test_reload_fails_to_read_configuration(hass):
async def test_reload_fails_to_read_configuration(hass: HomeAssistant) -> None:
"""Verify reload when configuration is missing or broken."""

respx.get("http://localhost") % HTTPStatus.OK
Expand Down Expand Up @@ -327,7 +328,7 @@ async def test_reload_fails_to_read_configuration(hass):


@respx.mock
async def test_multiple_rest_endpoints(hass):
async def test_multiple_rest_endpoints(hass: HomeAssistant) -> None:
"""Test multiple rest endpoints."""

respx.get("http://date.jsontest.com").respond(
Expand Down
3 changes: 2 additions & 1 deletion tests/components/rest/test_notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
import homeassistant.components.notify as notify
from homeassistant.components.rest import DOMAIN
from homeassistant.const import SERVICE_RELOAD
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component

from tests.common import get_fixture_path


@respx.mock
async def test_reload_notify(hass):
async def test_reload_notify(hass: HomeAssistant) -> None:
"""Verify we can reload the notify service."""
respx.get("http://localhost") % 200

Expand Down
Loading

0 comments on commit a0ed91e

Please sign in to comment.