Improve code quality of Tesla Fleet tests (#123959)

This commit is contained in:
Brett Adams 2024-08-15 17:59:08 +10:00 committed by GitHub
parent ac30efb5ac
commit dde1ecbf5b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 34 additions and 28 deletions

View File

@ -106,7 +106,7 @@ def mock_wake_up() -> Generator[AsyncMock]:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def mock_live_status() -> Generator[AsyncMock]: def mock_live_status() -> Generator[AsyncMock]:
"""Mock Teslemetry Energy Specific live_status method.""" """Mock Tesla Fleet API Energy Specific live_status method."""
with patch( with patch(
"homeassistant.components.tesla_fleet.EnergySpecific.live_status", "homeassistant.components.tesla_fleet.EnergySpecific.live_status",
side_effect=lambda: deepcopy(LIVE_STATUS), side_effect=lambda: deepcopy(LIVE_STATUS),
@ -116,7 +116,7 @@ def mock_live_status() -> Generator[AsyncMock]:
@pytest.fixture(autouse=True) @pytest.fixture(autouse=True)
def mock_site_info() -> Generator[AsyncMock]: def mock_site_info() -> Generator[AsyncMock]:
"""Mock Teslemetry Energy Specific site_info method.""" """Mock Tesla Fleet API Energy Specific site_info method."""
with patch( with patch(
"homeassistant.components.tesla_fleet.EnergySpecific.site_info", "homeassistant.components.tesla_fleet.EnergySpecific.site_info",
side_effect=lambda: deepcopy(SITE_INFO), side_effect=lambda: deepcopy(SITE_INFO),

View File

@ -1,8 +1,10 @@
"""Test the Tesla Fleet binary sensor platform.""" """Test the Tesla Fleet binary sensor platform."""
from unittest.mock import AsyncMock
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from syrupy import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from tesla_fleet_api.exceptions import VehicleOffline from tesla_fleet_api.exceptions import VehicleOffline
from homeassistant.components.tesla_fleet.coordinator import VEHICLE_INTERVAL from homeassistant.components.tesla_fleet.coordinator import VEHICLE_INTERVAL
@ -34,7 +36,7 @@ async def test_binary_sensor_refresh(
hass: HomeAssistant, hass: HomeAssistant,
snapshot: SnapshotAssertion, snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
mock_vehicle_data, mock_vehicle_data: AsyncMock,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
normal_config_entry: MockConfigEntry, normal_config_entry: MockConfigEntry,
) -> None: ) -> None:
@ -53,7 +55,7 @@ async def test_binary_sensor_refresh(
async def test_binary_sensor_offline( async def test_binary_sensor_offline(
hass: HomeAssistant, hass: HomeAssistant,
mock_vehicle_data, mock_vehicle_data: AsyncMock,
normal_config_entry: MockConfigEntry, normal_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Tests that the binary sensor entities are correct when offline.""" """Tests that the binary sensor entities are correct when offline."""

View File

@ -57,7 +57,7 @@ async def test_full_flow(
hass: HomeAssistant, hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator, hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
access_token, access_token: str,
) -> None: ) -> None:
"""Check full flow.""" """Check full flow."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
@ -121,7 +121,7 @@ async def test_full_flow_user_cred(
hass: HomeAssistant, hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator, hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
access_token, access_token: str,
) -> None: ) -> None:
"""Check full flow.""" """Check full flow."""
@ -200,7 +200,7 @@ async def test_reauthentication(
hass: HomeAssistant, hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator, hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
access_token, access_token: str,
) -> None: ) -> None:
"""Test Tesla Fleet reauthentication.""" """Test Tesla Fleet reauthentication."""
old_entry = MockConfigEntry( old_entry = MockConfigEntry(
@ -261,7 +261,7 @@ async def test_reauth_account_mismatch(
hass: HomeAssistant, hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator, hass_client_no_auth: ClientSessionGenerator,
aioclient_mock: AiohttpClientMocker, aioclient_mock: AiohttpClientMocker,
access_token, access_token: str,
) -> None: ) -> None:
"""Test Tesla Fleet reauthentication with different account.""" """Test Tesla Fleet reauthentication with different account."""
old_entry = MockConfigEntry(domain=DOMAIN, unique_id="baduid", version=1, data={}) old_entry = MockConfigEntry(domain=DOMAIN, unique_id="baduid", version=1, data={})

View File

@ -1,6 +1,8 @@
"""Test the Tesla Fleet device tracker platform.""" """Test the Tesla Fleet device tracker platform."""
from syrupy import SnapshotAssertion from unittest.mock import AsyncMock
from syrupy.assertion import SnapshotAssertion
from tesla_fleet_api.exceptions import VehicleOffline from tesla_fleet_api.exceptions import VehicleOffline
from homeassistant.const import STATE_UNKNOWN, Platform from homeassistant.const import STATE_UNKNOWN, Platform
@ -26,7 +28,7 @@ async def test_device_tracker(
async def test_device_tracker_offline( async def test_device_tracker_offline(
hass: HomeAssistant, hass: HomeAssistant,
mock_vehicle_data, mock_vehicle_data: AsyncMock,
normal_config_entry: MockConfigEntry, normal_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Tests that the device tracker entities are correct when offline.""" """Tests that the device tracker entities are correct when offline."""

View File

@ -4,7 +4,7 @@ from unittest.mock import AsyncMock
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from syrupy import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from tesla_fleet_api.exceptions import ( from tesla_fleet_api.exceptions import (
InvalidToken, InvalidToken,
LoginRequired, LoginRequired,
@ -59,9 +59,9 @@ async def test_load_unload(
async def test_init_error( async def test_init_error(
hass: HomeAssistant, hass: HomeAssistant,
normal_config_entry: MockConfigEntry, normal_config_entry: MockConfigEntry,
mock_products, mock_products: AsyncMock,
side_effect, side_effect: TeslaFleetError,
state, state: ConfigEntryState,
) -> None: ) -> None:
"""Test init with errors.""" """Test init with errors."""
@ -91,8 +91,8 @@ async def test_devices(
async def test_vehicle_refresh_offline( async def test_vehicle_refresh_offline(
hass: HomeAssistant, hass: HomeAssistant,
normal_config_entry: MockConfigEntry, normal_config_entry: MockConfigEntry,
mock_vehicle_state, mock_vehicle_state: AsyncMock,
mock_vehicle_data, mock_vehicle_data: AsyncMock,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test coordinator refresh with an error.""" """Test coordinator refresh with an error."""
@ -148,7 +148,7 @@ async def test_vehicle_refresh_error(
async def test_vehicle_refresh_ratelimited( async def test_vehicle_refresh_ratelimited(
hass: HomeAssistant, hass: HomeAssistant,
normal_config_entry: MockConfigEntry, normal_config_entry: MockConfigEntry,
mock_vehicle_data, mock_vehicle_data: AsyncMock,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test coordinator refresh handles 429.""" """Test coordinator refresh handles 429."""
@ -179,7 +179,7 @@ async def test_vehicle_refresh_ratelimited(
async def test_vehicle_sleep( async def test_vehicle_sleep(
hass: HomeAssistant, hass: HomeAssistant,
normal_config_entry: MockConfigEntry, normal_config_entry: MockConfigEntry,
mock_vehicle_data, mock_vehicle_data: AsyncMock,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test coordinator refresh with an error.""" """Test coordinator refresh with an error."""
@ -241,9 +241,9 @@ async def test_vehicle_sleep(
async def test_energy_live_refresh_error( async def test_energy_live_refresh_error(
hass: HomeAssistant, hass: HomeAssistant,
normal_config_entry: MockConfigEntry, normal_config_entry: MockConfigEntry,
mock_live_status, mock_live_status: AsyncMock,
side_effect, side_effect: TeslaFleetError,
state, state: ConfigEntryState,
) -> None: ) -> None:
"""Test coordinator refresh with an error.""" """Test coordinator refresh with an error."""
mock_live_status.side_effect = side_effect mock_live_status.side_effect = side_effect
@ -256,9 +256,9 @@ async def test_energy_live_refresh_error(
async def test_energy_site_refresh_error( async def test_energy_site_refresh_error(
hass: HomeAssistant, hass: HomeAssistant,
normal_config_entry: MockConfigEntry, normal_config_entry: MockConfigEntry,
mock_site_info, mock_site_info: AsyncMock,
side_effect, side_effect: TeslaFleetError,
state, state: ConfigEntryState,
) -> None: ) -> None:
"""Test coordinator refresh with an error.""" """Test coordinator refresh with an error."""
mock_site_info.side_effect = side_effect mock_site_info.side_effect = side_effect
@ -300,7 +300,7 @@ async def test_energy_live_refresh_ratelimited(
async def test_energy_info_refresh_ratelimited( async def test_energy_info_refresh_ratelimited(
hass: HomeAssistant, hass: HomeAssistant,
normal_config_entry: MockConfigEntry, normal_config_entry: MockConfigEntry,
mock_site_info, mock_site_info: AsyncMock,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
) -> None: ) -> None:
"""Test coordinator refresh handles 429.""" """Test coordinator refresh handles 429."""

View File

@ -1,8 +1,10 @@
"""Test the Tesla Fleet sensor platform.""" """Test the Tesla Fleet sensor platform."""
from unittest.mock import AsyncMock
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
from syrupy import SnapshotAssertion from syrupy.assertion import SnapshotAssertion
from homeassistant.components.tesla_fleet.coordinator import VEHICLE_INTERVAL from homeassistant.components.tesla_fleet.coordinator import VEHICLE_INTERVAL
from homeassistant.const import Platform from homeassistant.const import Platform
@ -22,7 +24,7 @@ async def test_sensors(
normal_config_entry: MockConfigEntry, normal_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry, entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory, freezer: FrozenDateTimeFactory,
mock_vehicle_data, mock_vehicle_data: AsyncMock,
) -> None: ) -> None:
"""Tests that the sensor entities are correct.""" """Tests that the sensor entities are correct."""