Speed up slow tests in Husqvarna Automower (#122854)

This commit is contained in:
Thomas55555 2024-07-30 16:27:58 +02:00 committed by GitHub
parent d9e996def5
commit a5136a1021
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 5 deletions

View File

@ -1,13 +1,18 @@
"""Tests for number platform.""" """Tests for number platform."""
from datetime import timedelta
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
from aioautomower.exceptions import ApiException from aioautomower.exceptions import ApiException
from aioautomower.utils import mower_list_to_dictionary_dataclass from aioautomower.utils import mower_list_to_dictionary_dataclass
from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from syrupy import SnapshotAssertion from syrupy import SnapshotAssertion
from homeassistant.components.husqvarna_automower.const import DOMAIN from homeassistant.components.husqvarna_automower.const import (
DOMAIN,
EXECUTION_TIME_DELAY,
)
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
@ -16,7 +21,12 @@ from homeassistant.helpers import entity_registry as er
from . import setup_integration from . import setup_integration
from .const import TEST_MOWER_ID from .const import TEST_MOWER_ID
from tests.common import MockConfigEntry, load_json_value_fixture, snapshot_platform from tests.common import (
MockConfigEntry,
async_fire_time_changed,
load_json_value_fixture,
snapshot_platform,
)
@pytest.mark.usefixtures("entity_registry_enabled_by_default") @pytest.mark.usefixtures("entity_registry_enabled_by_default")
@ -57,6 +67,7 @@ async def test_number_workarea_commands(
hass: HomeAssistant, hass: HomeAssistant,
mock_automower_client: AsyncMock, mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test number commands.""" """Test number commands."""
entity_id = "number.test_mower_1_front_lawn_cutting_height" entity_id = "number.test_mower_1_front_lawn_cutting_height"
@ -75,8 +86,11 @@ async def test_number_workarea_commands(
service="set_value", service="set_value",
target={"entity_id": entity_id}, target={"entity_id": entity_id},
service_data={"value": "75"}, service_data={"value": "75"},
blocking=True, blocking=False,
) )
freezer.tick(timedelta(seconds=EXECUTION_TIME_DELAY))
async_fire_time_changed(hass)
await hass.async_block_till_done()
mocked_method.assert_called_once_with(TEST_MOWER_ID, 75, 123456) mocked_method.assert_called_once_with(TEST_MOWER_ID, 75, 123456)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state.state is not None assert state.state is not None

View File

@ -1,5 +1,6 @@
"""Tests for switch platform.""" """Tests for switch platform."""
from datetime import timedelta
from unittest.mock import AsyncMock, patch from unittest.mock import AsyncMock, patch
from aioautomower.exceptions import ApiException from aioautomower.exceptions import ApiException
@ -9,7 +10,10 @@ from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from syrupy import SnapshotAssertion from syrupy import SnapshotAssertion
from homeassistant.components.husqvarna_automower.const import DOMAIN from homeassistant.components.husqvarna_automower.const import (
DOMAIN,
EXECUTION_TIME_DELAY,
)
from homeassistant.components.husqvarna_automower.coordinator import SCAN_INTERVAL from homeassistant.components.husqvarna_automower.coordinator import SCAN_INTERVAL
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -109,6 +113,7 @@ async def test_stay_out_zone_switch_commands(
excepted_state: str, excepted_state: str,
mock_automower_client: AsyncMock, mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test switch commands.""" """Test switch commands."""
entity_id = "switch.test_mower_1_avoid_danger_zone" entity_id = "switch.test_mower_1_avoid_danger_zone"
@ -124,8 +129,11 @@ async def test_stay_out_zone_switch_commands(
domain="switch", domain="switch",
service=service, service=service,
service_data={"entity_id": entity_id}, service_data={"entity_id": entity_id},
blocking=True, blocking=False,
) )
freezer.tick(timedelta(seconds=EXECUTION_TIME_DELAY))
async_fire_time_changed(hass)
await hass.async_block_till_done()
mocked_method.assert_called_once_with(TEST_MOWER_ID, TEST_ZONE_ID, boolean) mocked_method.assert_called_once_with(TEST_MOWER_ID, TEST_ZONE_ID, boolean)
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None