mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Waze travel time sensor tests (#66558)
This commit is contained in:
parent
2cba9b3d7e
commit
6ec0e3811a
@ -1395,9 +1395,6 @@ omit =
|
||||
homeassistant/components/watson_tts/tts.py
|
||||
homeassistant/components/watttime/__init__.py
|
||||
homeassistant/components/watttime/sensor.py
|
||||
homeassistant/components/waze_travel_time/__init__.py
|
||||
homeassistant/components/waze_travel_time/helpers.py
|
||||
homeassistant/components/waze_travel_time/sensor.py
|
||||
homeassistant/components/wiffi/__init__.py
|
||||
homeassistant/components/wiffi/binary_sensor.py
|
||||
homeassistant/components/wiffi/sensor.py
|
||||
|
@ -5,11 +5,13 @@ from WazeRouteCalculator import WRCError
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_wrc():
|
||||
@pytest.fixture(name="mock_wrc", autouse=True)
|
||||
def mock_wrc_fixture():
|
||||
"""Mock out WazeRouteCalculator."""
|
||||
with patch("homeassistant.components.waze_travel_time.sensor.WazeRouteCalculator"):
|
||||
yield
|
||||
with patch(
|
||||
"homeassistant.components.waze_travel_time.sensor.WazeRouteCalculator"
|
||||
) as mock_wrc:
|
||||
yield mock_wrc
|
||||
|
||||
|
||||
@pytest.fixture(name="validate_config_entry")
|
||||
@ -44,13 +46,11 @@ def bypass_platform_setup_fixture():
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_update")
|
||||
def mock_update_fixture():
|
||||
def mock_update_fixture(mock_wrc):
|
||||
"""Mock an update to the sensor."""
|
||||
with patch(
|
||||
"homeassistant.components.waze_travel_time.sensor.WazeRouteCalculator.calc_all_routes_info",
|
||||
return_value={"My route": (150, 300)},
|
||||
):
|
||||
yield
|
||||
obj = mock_wrc.return_value
|
||||
obj.calc_all_routes_info.return_value = {"My route": (150, 300)}
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(name="invalidate_config_entry")
|
||||
|
13
tests/components/waze_travel_time/const.py
Normal file
13
tests/components/waze_travel_time/const.py
Normal file
@ -0,0 +1,13 @@
|
||||
"""Constants for waze_travel_time tests."""
|
||||
|
||||
from homeassistant.components.waze_travel_time.const import (
|
||||
CONF_DESTINATION,
|
||||
CONF_ORIGIN,
|
||||
)
|
||||
from homeassistant.const import CONF_REGION
|
||||
|
||||
MOCK_CONFIG = {
|
||||
CONF_ORIGIN: "location1",
|
||||
CONF_DESTINATION: "location2",
|
||||
CONF_REGION: "US",
|
||||
}
|
@ -16,6 +16,8 @@ from homeassistant.components.waze_travel_time.const import (
|
||||
)
|
||||
from homeassistant.const import CONF_NAME, CONF_REGION, CONF_UNIT_SYSTEM_IMPERIAL
|
||||
|
||||
from .const import MOCK_CONFIG
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@ -29,11 +31,7 @@ async def test_minimum_fields(hass, validate_config_entry, bypass_setup):
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_ORIGIN: "location1",
|
||||
CONF_DESTINATION: "location2",
|
||||
CONF_REGION: "US",
|
||||
},
|
||||
MOCK_CONFIG,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@ -52,11 +50,7 @@ async def test_options(hass, validate_config_entry, mock_update):
|
||||
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data={
|
||||
CONF_ORIGIN: "location1",
|
||||
CONF_DESTINATION: "location2",
|
||||
CONF_REGION: "US",
|
||||
},
|
||||
data=MOCK_CONFIG,
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
@ -178,11 +172,7 @@ async def test_dupe(hass, validate_config_entry, bypass_setup):
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_ORIGIN: "location1",
|
||||
CONF_DESTINATION: "location2",
|
||||
CONF_REGION: "US",
|
||||
},
|
||||
MOCK_CONFIG,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@ -197,11 +187,7 @@ async def test_dupe(hass, validate_config_entry, bypass_setup):
|
||||
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_ORIGIN: "location1",
|
||||
CONF_DESTINATION: "location2",
|
||||
CONF_REGION: "US",
|
||||
},
|
||||
MOCK_CONFIG,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
@ -217,11 +203,7 @@ async def test_invalid_config_entry(hass, invalidate_config_entry):
|
||||
assert result["errors"] == {}
|
||||
result2 = await hass.config_entries.flow.async_configure(
|
||||
result["flow_id"],
|
||||
{
|
||||
CONF_ORIGIN: "location1",
|
||||
CONF_DESTINATION: "location2",
|
||||
CONF_REGION: "US",
|
||||
},
|
||||
MOCK_CONFIG,
|
||||
)
|
||||
|
||||
assert result2["type"] == data_entry_flow.RESULT_TYPE_FORM
|
||||
|
92
tests/components/waze_travel_time/test_sensor.py
Normal file
92
tests/components/waze_travel_time/test_sensor.py
Normal file
@ -0,0 +1,92 @@
|
||||
"""Test Waze Travel Time sensors."""
|
||||
|
||||
from WazeRouteCalculator import WRCError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.waze_travel_time.const import DOMAIN
|
||||
|
||||
from .const import MOCK_CONFIG
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_config")
|
||||
async def mock_config_fixture(hass, data):
|
||||
"""Mock a Waze Travel Time config entry."""
|
||||
config_entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
data=data,
|
||||
entry_id="test",
|
||||
)
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_update_wrcerror")
|
||||
def mock_update_wrcerror_fixture(mock_wrc):
|
||||
"""Mock an update to the sensor failed with WRCError."""
|
||||
obj = mock_wrc.return_value
|
||||
obj.calc_all_routes_info.side_effect = WRCError("test")
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_update_keyerror")
|
||||
def mock_update_keyerror_fixture(mock_wrc):
|
||||
"""Mock an update to the sensor failed with KeyError."""
|
||||
obj = mock_wrc.return_value
|
||||
obj.calc_all_routes_info.side_effect = KeyError("test")
|
||||
yield
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data",
|
||||
[MOCK_CONFIG],
|
||||
)
|
||||
@pytest.mark.usefixtures("mock_update", "mock_config")
|
||||
async def test_sensor(hass):
|
||||
"""Test that sensor works."""
|
||||
assert hass.states.get("sensor.waze_travel_time").state == "150"
|
||||
assert (
|
||||
hass.states.get("sensor.waze_travel_time").attributes["attribution"]
|
||||
== "Powered by Waze"
|
||||
)
|
||||
assert hass.states.get("sensor.waze_travel_time").attributes["duration"] == 150
|
||||
assert hass.states.get("sensor.waze_travel_time").attributes["distance"] == 300
|
||||
assert hass.states.get("sensor.waze_travel_time").attributes["route"] == "My route"
|
||||
assert (
|
||||
hass.states.get("sensor.waze_travel_time").attributes["origin"] == "location1"
|
||||
)
|
||||
assert (
|
||||
hass.states.get("sensor.waze_travel_time").attributes["destination"]
|
||||
== "location2"
|
||||
)
|
||||
assert (
|
||||
hass.states.get("sensor.waze_travel_time").attributes["unit_of_measurement"]
|
||||
== "min"
|
||||
)
|
||||
assert hass.states.get("sensor.waze_travel_time").attributes["icon"] == "mdi:car"
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_update_wrcerror")
|
||||
async def test_sensor_failed_wrcerror(hass, caplog):
|
||||
"""Test that sensor update fails with log message."""
|
||||
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("sensor.waze_travel_time").state == "unknown"
|
||||
assert "Error on retrieving data: " in caplog.text
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_update_keyerror")
|
||||
async def test_sensor_failed_keyerror(hass, caplog):
|
||||
"""Test that sensor update fails with log message."""
|
||||
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
|
||||
config_entry.add_to_hass(hass)
|
||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get("sensor.waze_travel_time").state == "unknown"
|
||||
assert "Error retrieving data from server" in caplog.text
|
Loading…
x
Reference in New Issue
Block a user