1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Add type hints to integration tests (part 7) (#87980)

This commit is contained in:
epenet
2023-02-13 09:53:09 +01:00
committed by GitHub
parent b9beed4624
commit b68f502769
51 changed files with 755 additions and 395 deletions

View File

@@ -1,5 +1,4 @@
"""Test The generic (IP Camera) config flow."""
import errno
from http import HTTPStatus
import os.path
@@ -38,6 +37,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from tests.common import MockConfigEntry
from tests.typing import ClientSessionGenerator
TESTDATA = {
CONF_STILL_IMAGE_URL: "http://127.0.0.1/testurl/1",
@@ -61,7 +61,13 @@ TESTDATA_YAML = {
@respx.mock
async def test_form(hass, fakeimgbytes_png, hass_client, user_flow, mock_create_stream):
async def test_form(
hass: HomeAssistant,
fakeimgbytes_png,
hass_client: ClientSessionGenerator,
user_flow,
mock_create_stream,
) -> None:
"""Test the form with a normal set of settings."""
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_png)
@@ -108,7 +114,9 @@ async def test_form(hass, fakeimgbytes_png, hass_client, user_flow, mock_create_
@respx.mock
async def test_form_only_stillimage(hass, fakeimg_png, user_flow):
async def test_form_only_stillimage(
hass: HomeAssistant, fakeimg_png, user_flow
) -> None:
"""Test we complete ok if the user wants still images only."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
@@ -148,8 +156,8 @@ async def test_form_only_stillimage(hass, fakeimg_png, user_flow):
@respx.mock
async def test_form_reject_still_preview(
hass, fakeimgbytes_png, mock_create_stream, user_flow
):
hass: HomeAssistant, fakeimgbytes_png, mock_create_stream, user_flow
) -> None:
"""Test we go back to the config screen if the user rejects the still preview."""
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_png)
with mock_create_stream:
@@ -169,8 +177,12 @@ async def test_form_reject_still_preview(
@respx.mock
async def test_form_still_preview_cam_off(
hass, fakeimg_png, mock_create_stream, user_flow, hass_client
):
hass: HomeAssistant,
fakeimg_png,
mock_create_stream,
user_flow,
hass_client: ClientSessionGenerator,
) -> None:
"""Test camera errors are triggered during preview."""
with patch(
"homeassistant.components.generic.camera.GenericCamera.is_on",
@@ -190,7 +202,9 @@ async def test_form_still_preview_cam_off(
@respx.mock
async def test_form_only_stillimage_gif(hass, fakeimg_gif, user_flow):
async def test_form_only_stillimage_gif(
hass: HomeAssistant, fakeimg_gif, user_flow
) -> None:
"""Test we complete ok if the user wants a gif."""
data = TESTDATA.copy()
data.pop(CONF_STREAM_SOURCE)
@@ -211,7 +225,9 @@ async def test_form_only_stillimage_gif(hass, fakeimg_gif, user_flow):
@respx.mock
async def test_form_only_svg_whitespace(hass, fakeimgbytes_svg, user_flow):
async def test_form_only_svg_whitespace(
hass: HomeAssistant, fakeimgbytes_svg, user_flow
) -> None:
"""Test we complete ok if svg starts with whitespace, issue #68889."""
fakeimgbytes_wspace_svg = bytes(" \n ", encoding="utf-8") + fakeimgbytes_svg
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_wspace_svg)
@@ -243,7 +259,9 @@ async def test_form_only_svg_whitespace(hass, fakeimgbytes_svg, user_flow):
("sample5_webp.webp"),
],
)
async def test_form_only_still_sample(hass, user_flow, image_file):
async def test_form_only_still_sample(
hass: HomeAssistant, user_flow, image_file
) -> None:
"""Test various sample images #69037."""
image_path = os.path.join(os.path.dirname(__file__), image_file)
with open(image_path, "rb") as image:
@@ -303,7 +321,13 @@ async def test_form_only_still_sample(hass, user_flow, image_file):
],
)
async def test_still_template(
hass, user_flow, fakeimgbytes_png, template, url, expected_result, expected_errors
hass: HomeAssistant,
user_flow,
fakeimgbytes_png,
template,
url,
expected_result,
expected_errors,
) -> None:
"""Test we can handle various templates."""
respx.get(url).respond(stream=fakeimgbytes_png)
@@ -321,7 +345,9 @@ async def test_still_template(
@respx.mock
async def test_form_rtsp_mode(hass, fakeimg_png, user_flow, mock_create_stream):
async def test_form_rtsp_mode(
hass: HomeAssistant, fakeimg_png, user_flow, mock_create_stream
) -> None:
"""Test we complete ok if the user enters a stream url."""
data = TESTDATA.copy()
data[CONF_RTSP_TRANSPORT] = "tcp"
@@ -357,7 +383,9 @@ async def test_form_rtsp_mode(hass, fakeimg_png, user_flow, mock_create_stream):
assert len(mock_setup.mock_calls) == 1
async def test_form_only_stream(hass, fakeimgbytes_jpg, user_flow, mock_create_stream):
async def test_form_only_stream(
hass: HomeAssistant, fakeimgbytes_jpg, user_flow, mock_create_stream
) -> None:
"""Test we complete ok if the user wants stream only."""
data = TESTDATA.copy()
data.pop(CONF_STILL_IMAGE_URL)
@@ -398,7 +426,9 @@ async def test_form_only_stream(hass, fakeimgbytes_jpg, user_flow, mock_create_s
assert len(mock_setup.mock_calls) == 1
async def test_form_still_and_stream_not_provided(hass, user_flow):
async def test_form_still_and_stream_not_provided(
hass: HomeAssistant, user_flow
) -> None:
"""Test we show a suitable error if neither still or stream URL are provided."""
result2 = await hass.config_entries.flow.async_configure(
user_flow["flow_id"],
@@ -413,7 +443,9 @@ async def test_form_still_and_stream_not_provided(hass, user_flow):
@respx.mock
async def test_form_image_timeout(hass, user_flow, mock_create_stream):
async def test_form_image_timeout(
hass: HomeAssistant, user_flow, mock_create_stream
) -> None:
"""Test we handle invalid image timeout."""
respx.get("http://127.0.0.1/testurl/1").side_effect = [
httpx.TimeoutException,
@@ -431,7 +463,9 @@ async def test_form_image_timeout(hass, user_flow, mock_create_stream):
@respx.mock
async def test_form_stream_invalidimage(hass, user_flow, mock_create_stream):
async def test_form_stream_invalidimage(
hass: HomeAssistant, user_flow, mock_create_stream
) -> None:
"""Test we handle invalid image when a stream is specified."""
respx.get("http://127.0.0.1/testurl/1").respond(stream=b"invalid")
with mock_create_stream:
@@ -446,7 +480,9 @@ async def test_form_stream_invalidimage(hass, user_flow, mock_create_stream):
@respx.mock
async def test_form_stream_invalidimage2(hass, user_flow, mock_create_stream):
async def test_form_stream_invalidimage2(
hass: HomeAssistant, user_flow, mock_create_stream
) -> None:
"""Test we handle invalid image when a stream is specified."""
respx.get("http://127.0.0.1/testurl/1").respond(content=None)
with mock_create_stream:
@@ -461,7 +497,9 @@ async def test_form_stream_invalidimage2(hass, user_flow, mock_create_stream):
@respx.mock
async def test_form_stream_invalidimage3(hass, user_flow, mock_create_stream):
async def test_form_stream_invalidimage3(
hass: HomeAssistant, user_flow, mock_create_stream
) -> None:
"""Test we handle invalid image when a stream is specified."""
respx.get("http://127.0.0.1/testurl/1").respond(content=bytes([0xFF]))
with mock_create_stream:
@@ -476,7 +514,7 @@ async def test_form_stream_invalidimage3(hass, user_flow, mock_create_stream):
@respx.mock
async def test_form_stream_timeout(hass, fakeimg_png, user_flow):
async def test_form_stream_timeout(hass: HomeAssistant, fakeimg_png, user_flow) -> None:
"""Test we handle invalid auth."""
with patch(
"homeassistant.components.generic.config_flow.create_stream"
@@ -495,7 +533,9 @@ async def test_form_stream_timeout(hass, fakeimg_png, user_flow):
@respx.mock
async def test_form_stream_worker_error(hass, fakeimg_png, user_flow):
async def test_form_stream_worker_error(
hass: HomeAssistant, fakeimg_png, user_flow
) -> None:
"""Test we handle a StreamWorkerError and pass the message through."""
with patch(
"homeassistant.components.generic.config_flow.create_stream",
@@ -510,7 +550,9 @@ async def test_form_stream_worker_error(hass, fakeimg_png, user_flow):
@respx.mock
async def test_form_stream_permission_error(hass, fakeimgbytes_png, user_flow):
async def test_form_stream_permission_error(
hass: HomeAssistant, fakeimgbytes_png, user_flow
) -> None:
"""Test we handle permission error."""
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_png)
with patch(
@@ -526,7 +568,9 @@ async def test_form_stream_permission_error(hass, fakeimgbytes_png, user_flow):
@respx.mock
async def test_form_no_route_to_host(hass, fakeimg_png, user_flow):
async def test_form_no_route_to_host(
hass: HomeAssistant, fakeimg_png, user_flow
) -> None:
"""Test we handle no route to host."""
with patch(
"homeassistant.components.generic.config_flow.create_stream",
@@ -541,7 +585,9 @@ async def test_form_no_route_to_host(hass, fakeimg_png, user_flow):
@respx.mock
async def test_form_stream_io_error(hass, fakeimg_png, user_flow):
async def test_form_stream_io_error(
hass: HomeAssistant, fakeimg_png, user_flow
) -> None:
"""Test we handle no io error when setting up stream."""
with patch(
"homeassistant.components.generic.config_flow.create_stream",
@@ -556,7 +602,7 @@ async def test_form_stream_io_error(hass, fakeimg_png, user_flow):
@respx.mock
async def test_form_oserror(hass, fakeimg_png, user_flow):
async def test_form_oserror(hass: HomeAssistant, fakeimg_png, user_flow) -> None:
"""Test we handle OS error when setting up stream."""
with patch(
"homeassistant.components.generic.config_flow.create_stream",
@@ -569,7 +615,9 @@ async def test_form_oserror(hass, fakeimg_png, user_flow):
@respx.mock
async def test_options_template_error(hass, fakeimgbytes_png, mock_create_stream):
async def test_options_template_error(
hass: HomeAssistant, fakeimgbytes_png, mock_create_stream
) -> None:
"""Test the options flow with a template error."""
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_png)
respx.get("http://127.0.0.1/testurl/2").respond(stream=fakeimgbytes_png)
@@ -665,7 +713,9 @@ async def test_slug(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> No
@respx.mock
async def test_options_only_stream(hass, fakeimgbytes_png, mock_create_stream):
async def test_options_only_stream(
hass: HomeAssistant, fakeimgbytes_png, mock_create_stream
) -> None:
"""Test the options flow without a still_image_url."""
respx.get("http://127.0.0.1/testurl/2").respond(stream=fakeimgbytes_png)
data = TESTDATA.copy()
@@ -703,7 +753,7 @@ async def test_options_only_stream(hass, fakeimgbytes_png, mock_create_stream):
# These below can be deleted after deprecation period is finished.
@respx.mock
async def test_import(hass, fakeimg_png):
async def test_import(hass: HomeAssistant, fakeimg_png) -> None:
"""Test configuration.yaml import used during migration."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=TESTDATA_YAML
@@ -723,7 +773,7 @@ async def test_import(hass, fakeimg_png):
# These above can be deleted after deprecation period is finished.
async def test_unload_entry(hass, fakeimg_png):
async def test_unload_entry(hass: HomeAssistant, fakeimg_png) -> None:
"""Test unloading the generic IP Camera entry."""
mock_entry = MockConfigEntry(domain=DOMAIN, options=TESTDATA)
mock_entry.add_to_hass(hass)
@@ -794,8 +844,8 @@ async def test_migrate_existing_ids(hass: HomeAssistant) -> None:
@respx.mock
async def test_use_wallclock_as_timestamps_option(
hass, fakeimg_png, mock_create_stream
):
hass: HomeAssistant, fakeimg_png, mock_create_stream
) -> None:
"""Test the use_wallclock_as_timestamps option flow."""
mock_entry = MockConfigEntry(