Address comments for Brother tests (#156877)

This commit is contained in:
Maciej Bieniek
2025-11-19 15:06:27 +01:00
committed by GitHub
parent 081b769abc
commit 249c1530d0
3 changed files with 35 additions and 47 deletions
+1 -3
View File
@@ -5,9 +5,7 @@ from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def init_integration(
hass: HomeAssistant, entry: MockConfigEntry
) -> MockConfigEntry:
async def init_integration(hass: HomeAssistant, entry: MockConfigEntry) -> None:
"""Set up the Brother integration in Home Assistant."""
entry.add_to_hass(hass)
-9
View File
@@ -90,15 +90,6 @@ def mock_setup_entry() -> Generator[AsyncMock]:
yield mock_setup_entry
@pytest.fixture
def mock_unload_entry() -> Generator[AsyncMock]:
"""Override async_unload_entry."""
with patch(
"homeassistant.components.brother.async_unload_entry", return_value=True
) as mock_unload_entry:
yield mock_unload_entry
@pytest.fixture
def mock_brother() -> Generator[AsyncMock]:
"""Mock the Brother class."""
+34 -35
View File
@@ -1,7 +1,7 @@
"""Define tests for the Brother Printer config flow."""
from ipaddress import ip_address
from unittest.mock import AsyncMock, patch
from unittest.mock import AsyncMock
from brother import SnmpError, UnsupportedModelError
import pytest
@@ -27,17 +27,7 @@ CONFIG = {
SECTION_ADVANCED_SETTINGS: {CONF_PORT: 161, CONF_COMMUNITY: "public"},
}
pytestmark = pytest.mark.usefixtures("mock_setup_entry", "mock_unload_entry")
async def test_show_form(hass: HomeAssistant) -> None:
"""Test that the form is served with no input."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
pytestmark = pytest.mark.usefixtures("mock_setup_entry")
@pytest.mark.parametrize("host", ["example.local", "127.0.0.1", "2001:db8::1428:57ab"])
@@ -49,9 +39,15 @@ async def test_create_entry(
config[CONF_HOST] = host
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data=config,
DOMAIN, context={"source": SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
flow_id=result["flow_id"],
user_input=config,
)
assert result["type"] is FlowResultType.CREATE_ENTRY
@@ -60,6 +56,7 @@ async def test_create_entry(
assert result["data"][CONF_TYPE] == "laser"
assert result["data"][SECTION_ADVANCED_SETTINGS][CONF_PORT] == 161
assert result["data"][SECTION_ADVANCED_SETTINGS][CONF_COMMUNITY] == "public"
assert result["result"].unique_id == "0123456789"
async def test_invalid_hostname(
@@ -97,6 +94,7 @@ async def test_invalid_hostname(
assert result["data"][CONF_TYPE] == "laser"
assert result["data"][SECTION_ADVANCED_SETTINGS][CONF_PORT] == 161
assert result["data"][SECTION_ADVANCED_SETTINGS][CONF_COMMUNITY] == "public"
assert result["result"].unique_id == "0123456789"
@pytest.mark.parametrize(
@@ -142,6 +140,7 @@ async def test_errors(
assert result["data"][CONF_TYPE] == "laser"
assert result["data"][SECTION_ADVANCED_SETTINGS][CONF_PORT] == 161
assert result["data"][SECTION_ADVANCED_SETTINGS][CONF_COMMUNITY] == "public"
assert result["result"].unique_id == "0123456789"
async def test_unsupported_model_error(
@@ -250,32 +249,31 @@ async def test_zeroconf_device_exists_abort(
assert mock_config_entry.data[CONF_HOST] == "127.0.0.1"
async def test_zeroconf_no_probe_existing_device(hass: HomeAssistant) -> None:
async def test_zeroconf_no_probe_existing_device(
hass: HomeAssistant, mock_brother_client: AsyncMock
) -> None:
"""Test we do not probe the device is the host is already configured."""
entry = MockConfigEntry(domain=DOMAIN, unique_id="0123456789", data=CONFIG)
entry.add_to_hass(hass)
with (
patch("homeassistant.components.brother.Brother.initialize"),
patch("homeassistant.components.brother.Brother._get_data") as mock_get_data,
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data=ZeroconfServiceInfo(
ip_address=ip_address("127.0.0.1"),
ip_addresses=[ip_address("127.0.0.1")],
hostname="example.local.",
name="Brother Printer",
port=None,
properties={},
type="mock_type",
),
)
await hass.async_block_till_done()
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_ZEROCONF},
data=ZeroconfServiceInfo(
ip_address=ip_address("127.0.0.1"),
ip_addresses=[ip_address("127.0.0.1")],
hostname="example.local.",
name="Brother Printer",
port=None,
properties={},
type="mock_type",
),
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
assert len(mock_get_data.mock_calls) == 0
mock_brother_client.async_update.assert_not_called()
async def test_zeroconf_confirm_create_entry(
@@ -315,6 +313,7 @@ async def test_zeroconf_confirm_create_entry(
assert result["data"][CONF_TYPE] == "laser"
assert result["data"][SECTION_ADVANCED_SETTINGS][CONF_PORT] == 161
assert result["data"][SECTION_ADVANCED_SETTINGS][CONF_COMMUNITY] == "public"
assert result["result"].unique_id == "0123456789"
async def test_reconfigure_successful(