Skip to content

Commit 2e0c830

Browse files
committed
Remove lint exceptions from tests
1 parent 54dc31d commit 2e0c830

File tree

3 files changed

+36
-45
lines changed

3 files changed

+36
-45
lines changed

.ruff.toml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,6 @@ ignore = [
2020
"tests/*" = [
2121
"S101", # Assert
2222
"PLR2004", # Magic value comparison
23-
"PT019", # Fixture
24-
"PLR0913", # Too many arguments
25-
"E501", # Lint too long
26-
"FBT001", # Boolean positional argument
27-
"FBT002", # Boolean positional argument
28-
"TRY002", # Exception
29-
"ARG001", # Unused function
3023
]
3124

3225
[lint.flake8-pytest-style]

tests/conftest.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@
1515
#
1616
# See here for more info: https://docs.pytest.org/en/latest/fixture.html (note that
1717
# pytest includes fixtures OOB which you can use as defined on this page)
18+
from collections.abc import Generator
19+
from unittest.mock import AsyncMock, patch
20+
1821
import pytest
1922

2023

2124
# This fixture enables loading custom integrations in all tests.
2225
# Remove to enable selective use of this fixture
2326
@pytest.fixture(autouse=True)
24-
def _auto_enable_custom_integrations(enable_custom_integrations: bool) -> None:
27+
def _auto_enable_custom_integrations(enable_custom_integrations: bool) -> None: # noqa: ARG001, FBT001
2528
"""Enable loading custom components."""
2629
return
30+
31+
32+
@pytest.fixture(autouse=True)
33+
def sleep() -> Generator[AsyncMock, None, None]:
34+
"""Disable sleep for all tests."""
35+
with patch("custom_components.retry.asyncio.sleep") as mock:
36+
yield mock

tests/test_init.py

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import datetime
66
from typing import TYPE_CHECKING, Any
7-
from unittest.mock import AsyncMock, Mock, patch
7+
from unittest.mock import AsyncMock, Mock
88

99
import homeassistant.util.dt as dt_util
1010
import pytest
@@ -74,7 +74,7 @@
7474

7575
async def async_setup(
7676
hass: HomeAssistant,
77-
raises: bool = True,
77+
raises: bool = True, # noqa: FBT001, FBT002
7878
options: dict | None = None,
7979
) -> list[ServiceCall]:
8080
"""Load retry custom integration and basic environment."""
@@ -102,7 +102,7 @@ def async_service(service_call: ServiceCall) -> None:
102102
"""Mock service call."""
103103
calls.append(service_call)
104104
if service_call.service == TEST_SERVICE and raises:
105-
raise Exception # pylint: disable=broad-exception-raised
105+
raise Exception # noqa: TRY002
106106

107107
hass.services.async_register(
108108
DOMAIN,
@@ -209,9 +209,9 @@ async def test_entity_unavailable(
209209
for entity in entities:
210210
assert f"{entity} is not available" in caplog.text
211211
assert (
212-
f"[Failed]: attempt 7/7: {DOMAIN}.{TEST_SERVICE}(entity_id={entity})[expected_state=on]"
213-
in caplog.text
214-
)
212+
f"[Failed]: attempt 7/7: {DOMAIN}.{TEST_SERVICE}(entity_id={entity})"
213+
"[expected_state=on]"
214+
) in caplog.text
215215

216216

217217
async def test_selective_retry(
@@ -240,12 +240,11 @@ async def test_selective_retry(
240240
],
241241
ids=["default", "grace", "validation"],
242242
)
243-
@patch("custom_components.retry.asyncio.sleep")
244-
async def test_entity_wrong_state(
245-
sleep_mock: AsyncMock,
243+
async def test_entity_wrong_state( # noqa: PLR0913
246244
hass: HomeAssistant,
247245
freezer: FrozenDateTimeFactory,
248246
caplog: pytest.LogCaptureFixture,
247+
sleep: AsyncMock,
249248
expected_state: str | None,
250249
validation: str | None,
251250
grace: float | None,
@@ -270,16 +269,15 @@ async def test_entity_wrong_state(
270269
if validation:
271270
validation = validation.replace("[", "{").replace("]", "}")
272271
assert f'"{validation}" is False' in caplog.text
273-
wait_times = [x.args[0] for x in sleep_mock.await_args_list]
272+
wait_times = [x.args[0] for x in sleep.await_args_list]
274273
assert wait_times.count(grace or 0.2) == 7
275274

276275

277-
@patch("custom_components.retry.asyncio.sleep")
278276
async def test_state_delay(
279-
sleep_mock: AsyncMock,
280277
hass: HomeAssistant,
281278
freezer: FrozenDateTimeFactory,
282279
caplog: pytest.LogCaptureFixture,
280+
sleep: AsyncMock,
283281
) -> None:
284282
"""Test initial state delay time."""
285283
await async_setup(hass, raises=False)
@@ -292,13 +290,13 @@ async def test_state_delay(
292290
},
293291
)
294292
await async_shutdown(hass, freezer)
295-
wait_times = [x.args[0] for x in sleep_mock.await_args_list]
293+
wait_times = [x.args[0] for x in sleep.await_args_list]
296294
assert wait_times.count(1.2) == 7 # state_delay
297295
assert wait_times.count(0.2) == 7 # state_grace
298296
assert (
299-
f"[Failed]: attempt 7/7: {DOMAIN}.{TEST_SERVICE}(entity_id=binary_sensor.test)[expected_state=off, state_delay=1.2]"
300-
in caplog.text
301-
)
297+
f"[Failed]: attempt 7/7: {DOMAIN}.{TEST_SERVICE}(entity_id=binary_sensor.test)"
298+
"[expected_state=off, state_delay=1.2]"
299+
) in caplog.text
302300

303301

304302
async def test_entity_expected_state_list(
@@ -318,9 +316,7 @@ async def test_entity_expected_state_list(
318316
assert len(calls) == 1
319317

320318

321-
@patch("custom_components.retry.asyncio.sleep")
322319
async def test_validation_success(
323-
_: AsyncMock,
324320
hass: HomeAssistant,
325321
freezer: FrozenDateTimeFactory,
326322
) -> None:
@@ -339,9 +335,7 @@ async def test_validation_success(
339335
assert len(calls) == 1
340336

341337

342-
@patch("custom_components.retry.asyncio.sleep")
343338
async def test_float_point_zero(
344-
_: AsyncMock,
345339
hass: HomeAssistant,
346340
freezer: FrozenDateTimeFactory,
347341
) -> None:
@@ -507,9 +501,7 @@ async def test_on_error(
507501
assert calls[-1].data[ATTR_ENTITY_ID] == "binary_sensor.test"
508502

509503

510-
@patch("custom_components.retry.asyncio.sleep")
511504
async def test_validation_in_automation(
512-
_: AsyncMock,
513505
hass: HomeAssistant,
514506
freezer: FrozenDateTimeFactory,
515507
) -> None:
@@ -542,7 +534,10 @@ def async_service(service_call: ServiceCall) -> None:
542534
ATTR_SERVICE: f"{DOMAIN}.{CALL_SERVICE}",
543535
"data": {
544536
ATTR_SERVICE: f"{DOMAIN}.tick",
545-
ATTR_VALIDATION: f"[[ now().timestamp() == {dt_util.now().timestamp()} ]]",
537+
ATTR_VALIDATION: (
538+
"[[ now().timestamp() == "
539+
f"{dt_util.now().timestamp()} ]]"
540+
),
546541
},
547542
}
548543
],
@@ -670,9 +665,7 @@ async def test_invalid_validation(hass: HomeAssistant) -> None:
670665
],
671666
ids=["call-param", "call-target", "actions"],
672667
)
673-
@patch("custom_components.retry.asyncio.sleep")
674-
async def test_all_entities(
675-
_: AsyncMock,
668+
async def test_all_entities( # noqa: PLR0913
676669
hass: HomeAssistant,
677670
freezer: FrozenDateTimeFactory,
678671
caplog: pytest.LogCaptureFixture,
@@ -697,9 +690,9 @@ async def test_all_entities(
697690
await async_shutdown(hass, freezer)
698691
for i in [1, 2]:
699692
assert (
700-
f"[Failed]: attempt 7/7: script.turn_off(entity_id=script.test{i})[expected_state=on]"
701-
in caplog.text
702-
)
693+
f"[Failed]: attempt 7/7: script.turn_off(entity_id=script.test{i})"
694+
"[expected_state=on]"
695+
) in caplog.text
703696

704697

705698
async def test_disable_repair(
@@ -892,9 +885,7 @@ async def test_action_types() -> None:
892885
]
893886

894887

895-
@patch("custom_components.retry.asyncio.sleep")
896888
async def test_actions_propagating_args(
897-
_: AsyncMock,
898889
hass: HomeAssistant,
899890
freezer: FrozenDateTimeFactory,
900891
caplog: pytest.LogCaptureFixture,
@@ -915,9 +906,8 @@ async def test_actions_propagating_args(
915906
await async_shutdown(hass, freezer)
916907
assert len(calls) == 4
917908
assert (
918-
f'[Failed]: attempt 3/3: {DOMAIN}.{TEST_SERVICE}()[validation="{{{{ False }}}}"]'
919-
in caplog.text
920-
)
909+
f"[Failed]: attempt 3/3: {DOMAIN}.{TEST_SERVICE}()" '[validation="{{ False }}"]'
910+
) in caplog.text
921911

922912

923913
@pytest.mark.parametrize(
@@ -933,9 +923,7 @@ async def test_actions_propagating_args(
933923
],
934924
ids=["default - exponential backoff", "linear", "slow exponential backoff"],
935925
)
936-
@patch("custom_components.retry.asyncio.sleep")
937-
async def test_actions_backoff(
938-
_: AsyncMock,
926+
async def test_actions_backoff( # noqa: PLR0913
939927
hass: HomeAssistant,
940928
freezer: FrozenDateTimeFactory,
941929
caplog: pytest.LogCaptureFixture,

0 commit comments

Comments
 (0)