Skip to content

Commit 3d130a9

Browse files
authored
Simplify generic camera tests (home-assistant#154313)
1 parent 2b38f33 commit 3d130a9

File tree

1 file changed

+18
-35
lines changed

1 file changed

+18
-35
lines changed

tests/components/generic/test_config_flow.py

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from homeassistant.config_entries import ConfigFlowResult
3333
from homeassistant.const import (
3434
CONF_AUTHENTICATION,
35-
CONF_NAME,
3635
CONF_PASSWORD,
3736
CONF_USERNAME,
3837
CONF_VERIFY_SSL,
@@ -56,16 +55,17 @@
5655
CONF_VERIFY_SSL: False,
5756
}
5857

58+
TESTDATA_ONLYSTILL = TESTDATA.copy()
59+
TESTDATA_ONLYSTILL.pop(CONF_STREAM_SOURCE)
60+
61+
TESTDATA_ONLYSTREAM = TESTDATA.copy()
62+
TESTDATA_ONLYSTREAM.pop(CONF_STILL_IMAGE_URL)
63+
5964
TESTDATA_OPTIONS = {
6065
CONF_LIMIT_REFETCH_TO_URL_CHANGE: False,
6166
**TESTDATA,
6267
}
6368

64-
TESTDATA_YAML = {
65-
CONF_NAME: "Yaml Defined Name",
66-
**TESTDATA,
67-
}
68-
6969

7070
@respx.mock
7171
@pytest.mark.usefixtures("fakeimg_png")
@@ -135,11 +135,9 @@ async def test_form_only_stillimage(
135135
mock_setup_entry: _patch[MagicMock],
136136
) -> None:
137137
"""Test we complete ok if the user wants still images only."""
138-
data = TESTDATA.copy()
139-
data.pop(CONF_STREAM_SOURCE)
140138
result1 = await hass.config_entries.flow.async_configure(
141139
user_flow["flow_id"],
142-
data,
140+
TESTDATA_ONLYSTILL,
143141
)
144142
await hass.async_block_till_done()
145143
assert result1["type"] is FlowResultType.FORM
@@ -235,11 +233,9 @@ async def test_form_only_stillimage_gif(
235233
mock_setup_entry: _patch[MagicMock],
236234
) -> None:
237235
"""Test we complete ok if the user wants a gif."""
238-
data = TESTDATA.copy()
239-
data.pop(CONF_STREAM_SOURCE)
240236
result1 = await hass.config_entries.flow.async_configure(
241237
user_flow["flow_id"],
242-
data,
238+
TESTDATA_ONLYSTILL,
243239
)
244240
assert result1["type"] is FlowResultType.FORM
245241
assert result1["step_id"] == "user_confirm"
@@ -262,11 +258,9 @@ async def test_form_only_svg_whitespace(
262258
"""Test we complete ok if svg starts with whitespace, issue #68889."""
263259
fakeimgbytes_wspace_svg = bytes(" \n ", encoding="utf-8") + fakeimgbytes_svg
264260
respx.get("http://127.0.0.1/testurl/1").respond(stream=fakeimgbytes_wspace_svg)
265-
data = TESTDATA.copy()
266-
data.pop(CONF_STREAM_SOURCE)
267261
result1 = await hass.config_entries.flow.async_configure(
268262
user_flow["flow_id"],
269-
data,
263+
TESTDATA_ONLYSTILL,
270264
)
271265
assert result1["type"] is FlowResultType.FORM
272266
assert result1["step_id"] == "user_confirm"
@@ -296,11 +290,9 @@ async def test_form_only_still_sample(
296290
image_path = os.path.join(os.path.dirname(__file__), image_file)
297291
image_bytes = await hass.async_add_executor_job(Path(image_path).read_bytes)
298292
respx.get("http://127.0.0.1/testurl/1").respond(stream=image_bytes)
299-
data = TESTDATA.copy()
300-
data.pop(CONF_STREAM_SOURCE)
301293
result1 = await hass.config_entries.flow.async_configure(
302294
user_flow["flow_id"],
303-
data,
295+
TESTDATA_ONLYSTILL,
304296
)
305297
assert result1["type"] is FlowResultType.FORM
306298
assert result1["step_id"] == "user_confirm"
@@ -364,8 +356,7 @@ async def test_form_still_template(
364356
# There is no need to mock the request if its an
365357
# invalid url because we will never make the request
366358
respx.get(url).respond(stream=fakeimgbytes_png)
367-
data = TESTDATA.copy()
368-
data.pop(CONF_STREAM_SOURCE)
359+
data = TESTDATA_ONLYSTILL.copy()
369360
data[CONF_STILL_IMAGE_URL] = template
370361
result2 = await hass.config_entries.flow.async_configure(
371362
user_flow["flow_id"],
@@ -417,8 +408,7 @@ async def test_form_only_stream(
417408
mock_create_stream: _patch[MagicMock],
418409
) -> None:
419410
"""Test we complete ok if the user wants stream only."""
420-
data = TESTDATA.copy()
421-
data.pop(CONF_STILL_IMAGE_URL)
411+
data = TESTDATA_ONLYSTREAM.copy()
422412
data[CONF_STREAM_SOURCE] = "rtsp://user:[email protected]/testurl/2"
423413
result1 = await hass.config_entries.flow.async_configure(
424414
user_flow["flow_id"],
@@ -592,16 +582,14 @@ async def test_form_stream_timeout(
592582
@respx.mock
593583
async def test_form_stream_not_set_up(hass: HomeAssistant, user_flow) -> None:
594584
"""Test we handle if stream has not been set up."""
595-
TESTDATA_ONLY_STREAM = TESTDATA.copy()
596-
TESTDATA_ONLY_STREAM.pop(CONF_STILL_IMAGE_URL)
597585

598586
with patch(
599587
"homeassistant.components.generic.config_flow.create_stream",
600588
side_effect=HomeAssistantError("Stream integration is not set up."),
601589
):
602590
result1 = await hass.config_entries.flow.async_configure(
603591
user_flow["flow_id"],
604-
TESTDATA_ONLY_STREAM,
592+
TESTDATA_ONLYSTREAM,
605593
)
606594
await hass.async_block_till_done()
607595

@@ -612,8 +600,6 @@ async def test_form_stream_not_set_up(hass: HomeAssistant, user_flow) -> None:
612600
@respx.mock
613601
async def test_form_stream_other_error(hass: HomeAssistant, user_flow) -> None:
614602
"""Test the unknown error for streams."""
615-
TESTDATA_ONLY_STREAM = TESTDATA.copy()
616-
TESTDATA_ONLY_STREAM.pop(CONF_STILL_IMAGE_URL)
617603

618604
with (
619605
patch(
@@ -624,7 +610,7 @@ async def test_form_stream_other_error(hass: HomeAssistant, user_flow) -> None:
624610
):
625611
await hass.config_entries.flow.async_configure(
626612
user_flow["flow_id"],
627-
TESTDATA_ONLY_STREAM,
613+
TESTDATA_ONLYSTREAM,
628614
)
629615
await hass.async_block_till_done()
630616

@@ -794,14 +780,12 @@ async def test_options_only_stream(
794780
mock_create_stream: _patch[MagicMock],
795781
) -> None:
796782
"""Test the options flow without a still_image_url."""
797-
data = TESTDATA.copy()
798-
data.pop(CONF_STILL_IMAGE_URL)
799783

800784
mock_entry = MockConfigEntry(
801785
title="Test Camera",
802786
domain=DOMAIN,
803787
data={},
804-
options=data,
788+
options=TESTDATA_ONLYSTREAM,
805789
)
806790
mock_entry.add_to_hass(hass)
807791
await hass.config_entries.async_setup(mock_entry.entry_id)
@@ -813,7 +797,7 @@ async def test_options_only_stream(
813797
# try updating the config options
814798
result2 = await hass.config_entries.options.async_configure(
815799
result["flow_id"],
816-
user_input=data,
800+
user_input=TESTDATA_ONLYSTREAM,
817801
)
818802
assert result2["type"] is FlowResultType.FORM
819803
assert result2["step_id"] == "user_confirm"
@@ -830,7 +814,8 @@ async def test_options_still_and_stream_not_provided(
830814
mock_setup_entry: _patch[MagicMock],
831815
) -> None:
832816
"""Test we show a suitable error if neither still or stream URL are provided."""
833-
data = TESTDATA.copy()
817+
data = TESTDATA_ONLYSTILL.copy()
818+
data.pop(CONF_STILL_IMAGE_URL)
834819

835820
mock_entry = MockConfigEntry(
836821
title="Test Camera",
@@ -845,8 +830,6 @@ async def test_options_still_and_stream_not_provided(
845830
assert result["type"] is FlowResultType.FORM
846831
assert result["step_id"] == "init"
847832

848-
data.pop(CONF_STILL_IMAGE_URL)
849-
data.pop(CONF_STREAM_SOURCE)
850833
result2 = await hass.config_entries.options.async_configure(
851834
result["flow_id"],
852835
user_input=data,

0 commit comments

Comments
 (0)