Skip to content

Commit 826f6c6

Browse files
authored
Refactor tests for Brother integration (#117377)
* Refactor tests - step 1 * Remove fixture * Refactor test_init * Refactor test_diagnostics * Refactor test_config_flow * Increase test coverage * Cleaning * Cleaning * Check config entry state in test_async_setup_entry * Simplify patching * Use AsyncMock when patching --------- Co-authored-by: Maciej Bieniek <[email protected]>
1 parent d2008ff commit 826f6c6

File tree

8 files changed

+337
-423
lines changed

8 files changed

+337
-423
lines changed
Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,15 @@
11
"""Tests for Brother Printer integration."""
22

3-
import json
4-
from unittest.mock import patch
5-
6-
from homeassistant.components.brother.const import DOMAIN
7-
from homeassistant.const import CONF_HOST, CONF_TYPE
83
from homeassistant.core import HomeAssistant
94

10-
from tests.common import MockConfigEntry, load_fixture
5+
from tests.common import MockConfigEntry
116

127

138
async def init_integration(
14-
hass: HomeAssistant, skip_setup: bool = False
9+
hass: HomeAssistant, entry: MockConfigEntry
1510
) -> MockConfigEntry:
1611
"""Set up the Brother integration in Home Assistant."""
17-
entry = MockConfigEntry(
18-
domain=DOMAIN,
19-
title="HL-L2340DW 0123456789",
20-
unique_id="0123456789",
21-
data={CONF_HOST: "localhost", CONF_TYPE: "laser"},
22-
)
23-
2412
entry.add_to_hass(hass)
2513

26-
if not skip_setup:
27-
with (
28-
patch("brother.Brother.initialize"),
29-
patch(
30-
"brother.Brother._get_data",
31-
return_value=json.loads(load_fixture("printer_data.json", "brother")),
32-
),
33-
):
34-
await hass.config_entries.async_setup(entry.entry_id)
35-
await hass.async_block_till_done()
36-
37-
return entry
14+
await hass.config_entries.async_setup(entry.entry_id)
15+
await hass.async_block_till_done()

tests/components/brother/conftest.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,81 @@
11
"""Test fixtures for brother."""
22

33
from collections.abc import Generator
4+
from datetime import UTC, datetime
45
from unittest.mock import AsyncMock, patch
56

7+
from brother import BrotherSensors
68
import pytest
79

10+
from homeassistant.components.brother.const import DOMAIN
11+
from homeassistant.const import CONF_HOST, CONF_TYPE
12+
13+
from tests.common import MockConfigEntry
14+
15+
BROTHER_DATA = BrotherSensors(
16+
belt_unit_remaining_life=97,
17+
belt_unit_remaining_pages=48436,
18+
black_counter=None,
19+
black_drum_counter=1611,
20+
black_drum_remaining_life=92,
21+
black_drum_remaining_pages=16389,
22+
black_ink_remaining=None,
23+
black_ink_status=None,
24+
black_ink=None,
25+
black_toner_remaining=75,
26+
black_toner_status=1,
27+
black_toner=80,
28+
bw_counter=709,
29+
color_counter=902,
30+
cyan_counter=None,
31+
cyan_drum_counter=1611,
32+
cyan_drum_remaining_life=92,
33+
cyan_drum_remaining_pages=16389,
34+
cyan_ink_remaining=None,
35+
cyan_ink_status=None,
36+
cyan_ink=None,
37+
cyan_toner_remaining=10,
38+
cyan_toner_status=1,
39+
cyan_toner=10,
40+
drum_counter=986,
41+
drum_remaining_life=92,
42+
drum_remaining_pages=11014,
43+
drum_status=1,
44+
duplex_unit_pages_counter=538,
45+
fuser_remaining_life=97,
46+
fuser_unit_remaining_pages=None,
47+
image_counter=None,
48+
laser_remaining_life=None,
49+
laser_unit_remaining_pages=48389,
50+
magenta_counter=None,
51+
magenta_drum_counter=1611,
52+
magenta_drum_remaining_life=92,
53+
magenta_drum_remaining_pages=16389,
54+
magenta_ink_remaining=None,
55+
magenta_ink_status=None,
56+
magenta_ink=None,
57+
magenta_toner_remaining=8,
58+
magenta_toner_status=2,
59+
magenta_toner=10,
60+
page_counter=986,
61+
pf_kit_1_remaining_life=98,
62+
pf_kit_1_remaining_pages=48741,
63+
pf_kit_mp_remaining_life=None,
64+
pf_kit_mp_remaining_pages=None,
65+
status="waiting",
66+
uptime=datetime(2024, 3, 3, 15, 4, 24, tzinfo=UTC),
67+
yellow_counter=None,
68+
yellow_drum_counter=1611,
69+
yellow_drum_remaining_life=92,
70+
yellow_drum_remaining_pages=16389,
71+
yellow_ink_remaining=None,
72+
yellow_ink_status=None,
73+
yellow_ink=None,
74+
yellow_toner_remaining=2,
75+
yellow_toner_status=2,
76+
yellow_toner=10,
77+
)
78+
879

980
@pytest.fixture
1081
def mock_setup_entry() -> Generator[AsyncMock, None, None]:
@@ -13,3 +84,34 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]:
1384
"homeassistant.components.brother.async_setup_entry", return_value=True
1485
) as mock_setup_entry:
1586
yield mock_setup_entry
87+
88+
89+
@pytest.fixture
90+
def mock_brother_client() -> Generator[AsyncMock, None, None]:
91+
"""Mock Brother client."""
92+
with (
93+
patch("homeassistant.components.brother.Brother", autospec=True) as mock_client,
94+
patch(
95+
"homeassistant.components.brother.config_flow.Brother",
96+
new=mock_client,
97+
),
98+
):
99+
client = mock_client.create.return_value
100+
client.async_update.return_value = BROTHER_DATA
101+
client.serial = "0123456789"
102+
client.mac = "AA:BB:CC:DD:EE:FF"
103+
client.model = "HL-L2340DW"
104+
client.firmware = "1.2.3"
105+
106+
yield client
107+
108+
109+
@pytest.fixture
110+
def mock_config_entry() -> MockConfigEntry:
111+
"""Mock a config entry."""
112+
return MockConfigEntry(
113+
domain=DOMAIN,
114+
title="HL-L2340DW 0123456789",
115+
unique_id="0123456789",
116+
data={CONF_HOST: "localhost", CONF_TYPE: "laser"},
117+
)

tests/components/brother/fixtures/printer_data.json

Lines changed: 0 additions & 77 deletions
This file was deleted.

tests/components/brother/snapshots/test_diagnostics.ambr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
'pf_kit_mp_remaining_life': None,
5353
'pf_kit_mp_remaining_pages': None,
5454
'status': 'waiting',
55-
'uptime': '2019-09-24T12:14:56+00:00',
55+
'uptime': '2024-03-03T15:04:24+00:00',
5656
'yellow_counter': None,
5757
'yellow_drum_counter': 1611,
5858
'yellow_drum_remaining_life': 92,
@@ -64,7 +64,7 @@
6464
'yellow_toner_remaining': 2,
6565
'yellow_toner_status': 2,
6666
}),
67-
'firmware': '1.17',
67+
'firmware': '1.2.3',
6868
'info': dict({
6969
'host': 'localhost',
7070
'type': 'laser',

0 commit comments

Comments
 (0)