Skip to content

Commit 68c38ac

Browse files
authored
Improve client mock for Brother tests (home-assistant#155037)
1 parent f5a6fa8 commit 68c38ac

File tree

3 files changed

+52
-49
lines changed

3 files changed

+52
-49
lines changed

tests/components/brother/conftest.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from collections.abc import Generator
44
from datetime import UTC, datetime
5-
from unittest.mock import AsyncMock, MagicMock, patch
5+
from unittest.mock import AsyncMock, patch
66

77
from brother import BrotherSensors
88
import pytest
@@ -100,23 +100,26 @@ def mock_unload_entry() -> Generator[AsyncMock]:
100100

101101

102102
@pytest.fixture
103-
def mock_brother_client() -> Generator[MagicMock]:
104-
"""Mock Brother client."""
103+
def mock_brother() -> Generator[AsyncMock]:
104+
"""Mock the Brother class."""
105105
with (
106-
patch("homeassistant.components.brother.Brother", autospec=True) as mock_client,
107-
patch(
108-
"homeassistant.components.brother.config_flow.Brother",
109-
new=mock_client,
110-
),
106+
patch("homeassistant.components.brother.Brother", autospec=True) as mock_class,
107+
patch("homeassistant.components.brother.config_flow.Brother", new=mock_class),
111108
):
112-
client = mock_client.create.return_value
113-
client.async_update.return_value = BROTHER_DATA
114-
client.serial = "0123456789"
115-
client.mac = "AA:BB:CC:DD:EE:FF"
116-
client.model = "HL-L2340DW"
117-
client.firmware = "1.2.3"
118-
119-
yield client
109+
yield mock_class
110+
111+
112+
@pytest.fixture
113+
def mock_brother_client(mock_brother: AsyncMock) -> AsyncMock:
114+
"""Mock Brother client."""
115+
client = mock_brother.create.return_value
116+
client.async_update.return_value = BROTHER_DATA
117+
client.serial = "0123456789"
118+
client.mac = "AA:BB:CC:DD:EE:FF"
119+
client.model = "HL-L2340DW"
120+
client.firmware = "1.2.3"
121+
122+
return client
120123

121124

122125
@pytest.fixture

tests/components/brother/test_config_flow.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,14 @@ async def test_errors(
9898
assert result["errors"] == {"base": base_error}
9999

100100

101-
async def test_unsupported_model_error(hass: HomeAssistant) -> None:
101+
async def test_unsupported_model_error(
102+
hass: HomeAssistant, mock_brother: AsyncMock, mock_brother_client: AsyncMock
103+
) -> None:
102104
"""Test unsupported printer model error."""
103-
with patch(
104-
"homeassistant.components.brother.Brother.create",
105-
new=AsyncMock(side_effect=UnsupportedModelError("error")),
106-
):
107-
result = await hass.config_entries.flow.async_init(
108-
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
109-
)
105+
mock_brother.create.side_effect = UnsupportedModelError("error")
106+
result = await hass.config_entries.flow.async_init(
107+
DOMAIN, context={"source": SOURCE_USER}, data=CONFIG
108+
)
110109

111110
assert result["type"] is FlowResultType.ABORT
112111
assert result["reason"] == "unsupported_model"
@@ -153,25 +152,24 @@ async def test_zeroconf_exception(
153152
assert result["reason"] == "cannot_connect"
154153

155154

156-
async def test_zeroconf_unsupported_model(hass: HomeAssistant) -> None:
155+
async def test_zeroconf_unsupported_model(
156+
hass: HomeAssistant, mock_brother: AsyncMock, mock_brother_client: AsyncMock
157+
) -> None:
157158
"""Test unsupported printer model error."""
158-
with patch(
159-
"homeassistant.components.brother.Brother.create",
160-
new=AsyncMock(side_effect=UnsupportedModelError("error")),
161-
):
162-
result = await hass.config_entries.flow.async_init(
163-
DOMAIN,
164-
context={"source": SOURCE_ZEROCONF},
165-
data=ZeroconfServiceInfo(
166-
ip_address=ip_address("127.0.0.1"),
167-
ip_addresses=[ip_address("127.0.0.1")],
168-
hostname="example.local.",
169-
name="Brother Printer",
170-
port=None,
171-
properties={"product": "MFC-8660DN"},
172-
type="mock_type",
173-
),
174-
)
159+
mock_brother.create.side_effect = UnsupportedModelError("error")
160+
result = await hass.config_entries.flow.async_init(
161+
DOMAIN,
162+
context={"source": SOURCE_ZEROCONF},
163+
data=ZeroconfServiceInfo(
164+
ip_address=ip_address("127.0.0.1"),
165+
ip_addresses=[ip_address("127.0.0.1")],
166+
hostname="example.local.",
167+
name="Brother Printer",
168+
port=None,
169+
properties={"product": "MFC-8660DN"},
170+
type="mock_type",
171+
),
172+
)
175173

176174
assert result["type"] is FlowResultType.ABORT
177175
assert result["reason"] == "unsupported_model"

tests/components/brother/test_init.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Test init of Brother integration."""
22

3-
from unittest.mock import AsyncMock, patch
3+
from unittest.mock import AsyncMock
44

55
from brother import SnmpError
66
import pytest
@@ -45,14 +45,16 @@ async def test_config_not_ready(
4545

4646
@pytest.mark.parametrize("exc", [(SnmpError("SNMP Error")), (ConnectionError)])
4747
async def test_error_on_init(
48-
hass: HomeAssistant, exc: Exception, mock_config_entry: MockConfigEntry
48+
hass: HomeAssistant,
49+
exc: Exception,
50+
mock_config_entry: MockConfigEntry,
51+
mock_brother: AsyncMock,
52+
mock_brother_client: AsyncMock,
4953
) -> None:
5054
"""Test for error on init."""
51-
with patch(
52-
"homeassistant.components.brother.Brother.create",
53-
new=AsyncMock(side_effect=exc),
54-
):
55-
await init_integration(hass, mock_config_entry)
55+
mock_brother.create.side_effect = exc
56+
57+
await init_integration(hass, mock_config_entry)
5658

5759
assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY
5860

0 commit comments

Comments
 (0)