mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Improve tests in Tessie (#105430)
This commit is contained in:
parent
bf9c2a08b7
commit
8922c93259
@ -81,7 +81,7 @@ class TessieConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
)
|
)
|
||||||
except ClientResponseError as e:
|
except ClientResponseError as e:
|
||||||
if e.status == HTTPStatus.UNAUTHORIZED:
|
if e.status == HTTPStatus.UNAUTHORIZED:
|
||||||
errors["base"] = "invalid_access_token"
|
errors[CONF_ACCESS_TOKEN] = "invalid_access_token"
|
||||||
else:
|
else:
|
||||||
errors["base"] = "unknown"
|
errors["base"] = "unknown"
|
||||||
except ClientConnectionError:
|
except ClientConnectionError:
|
||||||
|
28
tests/components/tessie/conftest.py
Normal file
28
tests/components/tessie/conftest.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
"""Fixtures for Tessie."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from .common import TEST_STATE_OF_ALL_VEHICLES, TEST_VEHICLE_STATE_ONLINE
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_get_state():
|
||||||
|
"""Mock get_state function."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.tessie.coordinator.get_state",
|
||||||
|
return_value=TEST_VEHICLE_STATE_ONLINE,
|
||||||
|
) as mock_get_state:
|
||||||
|
yield mock_get_state
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_get_state_of_all_vehicles():
|
||||||
|
"""Mock get_state_of_all_vehicles function."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.tessie.config_flow.get_state_of_all_vehicles",
|
||||||
|
return_value=TEST_STATE_OF_ALL_VEHICLES,
|
||||||
|
) as mock_get_state_of_all_vehicles:
|
||||||
|
yield mock_get_state_of_all_vehicles
|
@ -15,24 +15,13 @@ from .common import (
|
|||||||
ERROR_CONNECTION,
|
ERROR_CONNECTION,
|
||||||
ERROR_UNKNOWN,
|
ERROR_UNKNOWN,
|
||||||
TEST_CONFIG,
|
TEST_CONFIG,
|
||||||
TEST_STATE_OF_ALL_VEHICLES,
|
|
||||||
setup_platform,
|
setup_platform,
|
||||||
)
|
)
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
async def test_form(hass: HomeAssistant, mock_get_state_of_all_vehicles) -> None:
|
||||||
def mock_get_state_of_all_vehicles():
|
|
||||||
"""Mock get_state_of_all_vehicles function."""
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.tessie.config_flow.get_state_of_all_vehicles",
|
|
||||||
return_value=TEST_STATE_OF_ALL_VEHICLES,
|
|
||||||
) as mock_get_state_of_all_vehicles:
|
|
||||||
yield mock_get_state_of_all_vehicles
|
|
||||||
|
|
||||||
|
|
||||||
async def test_form(hass: HomeAssistant) -> None:
|
|
||||||
"""Test we get the form."""
|
"""Test we get the form."""
|
||||||
|
|
||||||
result1 = await hass.config_entries.flow.async_init(
|
result1 = await hass.config_entries.flow.async_init(
|
||||||
@ -42,9 +31,6 @@ async def test_form(hass: HomeAssistant) -> None:
|
|||||||
assert not result1["errors"]
|
assert not result1["errors"]
|
||||||
|
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.tessie.config_flow.get_state_of_all_vehicles",
|
|
||||||
return_value=TEST_STATE_OF_ALL_VEHICLES,
|
|
||||||
) as mock_get_state_of_all_vehicles, patch(
|
|
||||||
"homeassistant.components.tessie.async_setup_entry",
|
"homeassistant.components.tessie.async_setup_entry",
|
||||||
return_value=True,
|
return_value=True,
|
||||||
) as mock_setup_entry:
|
) as mock_setup_entry:
|
||||||
@ -61,92 +47,34 @@ async def test_form(hass: HomeAssistant) -> None:
|
|||||||
assert result2["data"] == TEST_CONFIG
|
assert result2["data"] == TEST_CONFIG
|
||||||
|
|
||||||
|
|
||||||
async def test_form_invalid_access_token(hass: HomeAssistant) -> None:
|
@pytest.mark.parametrize(
|
||||||
|
("side_effect", "error"),
|
||||||
|
[
|
||||||
|
(ERROR_AUTH, {CONF_ACCESS_TOKEN: "invalid_access_token"}),
|
||||||
|
(ERROR_UNKNOWN, {"base": "unknown"}),
|
||||||
|
(ERROR_CONNECTION, {"base": "cannot_connect"}),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_form_errors(
|
||||||
|
hass: HomeAssistant, side_effect, error, mock_get_state_of_all_vehicles
|
||||||
|
) -> None:
|
||||||
"""Test invalid auth is handled."""
|
"""Test invalid auth is handled."""
|
||||||
|
|
||||||
result1 = await hass.config_entries.flow.async_init(
|
result1 = await hass.config_entries.flow.async_init(
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
||||||
)
|
)
|
||||||
|
|
||||||
with patch(
|
mock_get_state_of_all_vehicles.side_effect = side_effect
|
||||||
"homeassistant.components.tessie.config_flow.get_state_of_all_vehicles",
|
|
||||||
side_effect=ERROR_AUTH,
|
|
||||||
):
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result1["flow_id"],
|
result1["flow_id"],
|
||||||
TEST_CONFIG,
|
TEST_CONFIG,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result2["type"] == FlowResultType.FORM
|
assert result2["type"] == FlowResultType.FORM
|
||||||
assert result2["errors"] == {CONF_ACCESS_TOKEN: "invalid_access_token"}
|
assert result2["errors"] == error
|
||||||
|
|
||||||
# Complete the flow
|
# Complete the flow
|
||||||
with patch(
|
mock_get_state_of_all_vehicles.side_effect = None
|
||||||
"homeassistant.components.tessie.config_flow.get_state_of_all_vehicles",
|
|
||||||
return_value=TEST_STATE_OF_ALL_VEHICLES,
|
|
||||||
):
|
|
||||||
result3 = await hass.config_entries.flow.async_configure(
|
|
||||||
result2["flow_id"],
|
|
||||||
TEST_CONFIG,
|
|
||||||
)
|
|
||||||
assert result3["type"] == FlowResultType.CREATE_ENTRY
|
|
||||||
|
|
||||||
|
|
||||||
async def test_form_invalid_response(hass: HomeAssistant) -> None:
|
|
||||||
"""Test invalid auth is handled."""
|
|
||||||
|
|
||||||
result1 = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
||||||
)
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.tessie.config_flow.get_state_of_all_vehicles",
|
|
||||||
side_effect=ERROR_UNKNOWN,
|
|
||||||
):
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
|
||||||
result1["flow_id"],
|
|
||||||
TEST_CONFIG,
|
|
||||||
)
|
|
||||||
|
|
||||||
assert result2["type"] == FlowResultType.FORM
|
|
||||||
assert result2["errors"] == {"base": "unknown"}
|
|
||||||
|
|
||||||
# Complete the flow
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.tessie.config_flow.get_state_of_all_vehicles",
|
|
||||||
return_value=TEST_STATE_OF_ALL_VEHICLES,
|
|
||||||
):
|
|
||||||
result3 = await hass.config_entries.flow.async_configure(
|
|
||||||
result2["flow_id"],
|
|
||||||
TEST_CONFIG,
|
|
||||||
)
|
|
||||||
assert result3["type"] == FlowResultType.CREATE_ENTRY
|
|
||||||
|
|
||||||
|
|
||||||
async def test_form_network_issue(hass: HomeAssistant) -> None:
|
|
||||||
"""Test network issues are handled."""
|
|
||||||
|
|
||||||
result1 = await hass.config_entries.flow.async_init(
|
|
||||||
DOMAIN, context={"source": config_entries.SOURCE_USER}
|
|
||||||
)
|
|
||||||
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.tessie.config_flow.get_state_of_all_vehicles",
|
|
||||||
side_effect=ERROR_CONNECTION,
|
|
||||||
):
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
|
||||||
result1["flow_id"],
|
|
||||||
TEST_CONFIG,
|
|
||||||
)
|
|
||||||
|
|
||||||
assert result2["type"] == FlowResultType.FORM
|
|
||||||
assert result2["errors"] == {"base": "cannot_connect"}
|
|
||||||
|
|
||||||
# Complete the flow
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.tessie.config_flow.get_state_of_all_vehicles",
|
|
||||||
return_value=TEST_STATE_OF_ALL_VEHICLES,
|
|
||||||
):
|
|
||||||
result3 = await hass.config_entries.flow.async_configure(
|
result3 = await hass.config_entries.flow.async_configure(
|
||||||
result2["flow_id"],
|
result2["flow_id"],
|
||||||
TEST_CONFIG,
|
TEST_CONFIG,
|
||||||
@ -196,7 +124,7 @@ async def test_reauth(hass: HomeAssistant, mock_get_state_of_all_vehicles) -> No
|
|||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
("side_effect", "error"),
|
("side_effect", "error"),
|
||||||
[
|
[
|
||||||
(ERROR_AUTH, {"base": "invalid_access_token"}),
|
(ERROR_AUTH, {CONF_ACCESS_TOKEN: "invalid_access_token"}),
|
||||||
(ERROR_UNKNOWN, {"base": "unknown"}),
|
(ERROR_UNKNOWN, {"base": "unknown"}),
|
||||||
(ERROR_CONNECTION, {"base": "cannot_connect"}),
|
(ERROR_CONNECTION, {"base": "cannot_connect"}),
|
||||||
],
|
],
|
||||||
|
@ -1,8 +1,5 @@
|
|||||||
"""Test the Tessie sensor platform."""
|
"""Test the Tessie sensor platform."""
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from homeassistant.components.tessie.coordinator import TESSIE_SYNC_INTERVAL
|
from homeassistant.components.tessie.coordinator import TESSIE_SYNC_INTERVAL
|
||||||
from homeassistant.components.tessie.sensor import TessieStatus
|
from homeassistant.components.tessie.sensor import TessieStatus
|
||||||
@ -25,15 +22,6 @@ from tests.common import async_fire_time_changed
|
|||||||
WAIT = timedelta(seconds=TESSIE_SYNC_INTERVAL)
|
WAIT = timedelta(seconds=TESSIE_SYNC_INTERVAL)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def mock_get_state():
|
|
||||||
"""Mock get_state function."""
|
|
||||||
with patch(
|
|
||||||
"homeassistant.components.tessie.coordinator.get_state",
|
|
||||||
) as mock_get_state:
|
|
||||||
yield mock_get_state
|
|
||||||
|
|
||||||
|
|
||||||
async def test_coordinator_online(hass: HomeAssistant, mock_get_state) -> None:
|
async def test_coordinator_online(hass: HomeAssistant, mock_get_state) -> None:
|
||||||
"""Tests that the coordinator handles online vehciles."""
|
"""Tests that the coordinator handles online vehciles."""
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user