Cleanup Waze_travel_time_sensor_tests (#67047)

pull/67132/head
Kevin Stillhammer 2022-02-23 18:59:42 +01:00 committed by GitHub
parent e37402e1d5
commit 93fab1f996
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 54 deletions

View File

@ -14,6 +14,13 @@ def mock_wrc_fixture():
yield mock_wrc yield mock_wrc
@pytest.fixture(name="mock_update")
def mock_update_fixture(mock_wrc):
"""Mock an update to the sensor."""
obj = mock_wrc.return_value
obj.calc_all_routes_info.return_value = {"My route": (150, 300)}
@pytest.fixture(name="validate_config_entry") @pytest.fixture(name="validate_config_entry")
def validate_config_entry_fixture(): def validate_config_entry_fixture():
"""Return valid config entry.""" """Return valid config entry."""
@ -22,17 +29,15 @@ def validate_config_entry_fixture():
) as mock_wrc: ) as mock_wrc:
obj = mock_wrc.return_value obj = mock_wrc.return_value
obj.calc_all_routes_info.return_value = None obj.calc_all_routes_info.return_value = None
yield yield mock_wrc
@pytest.fixture(name="bypass_setup") @pytest.fixture(name="invalidate_config_entry")
def bypass_setup_fixture(): def invalidate_config_entry_fixture(validate_config_entry):
"""Bypass entry setup.""" """Return invalid config entry."""
with patch( obj = validate_config_entry.return_value
"homeassistant.components.waze_travel_time.async_setup_entry", obj.calc_all_routes_info.return_value = {}
return_value=True, obj.calc_all_routes_info.side_effect = WRCError("test")
):
yield
@pytest.fixture(name="bypass_platform_setup") @pytest.fixture(name="bypass_platform_setup")
@ -45,21 +50,11 @@ def bypass_platform_setup_fixture():
yield yield
@pytest.fixture(name="mock_update") @pytest.fixture(name="bypass_setup")
def mock_update_fixture(mock_wrc): def bypass_setup_fixture():
"""Mock an update to the sensor.""" """Bypass entry setup."""
obj = mock_wrc.return_value
obj.calc_all_routes_info.return_value = {"My route": (150, 300)}
yield
@pytest.fixture(name="invalidate_config_entry")
def invalidate_config_entry_fixture():
"""Return invalid config entry."""
with patch( with patch(
"homeassistant.components.waze_travel_time.helpers.WazeRouteCalculator" "homeassistant.components.waze_travel_time.async_setup_entry",
) as mock_wrc: return_value=True,
obj = mock_wrc.return_value ):
obj.calc_all_routes_info.return_value = {}
obj.calc_all_routes_info.side_effect = WRCError("test")
yield yield

View File

@ -1,4 +1,6 @@
"""Test the Waze Travel Time config flow.""" """Test the Waze Travel Time config flow."""
import pytest
from homeassistant import config_entries, data_entry_flow from homeassistant import config_entries, data_entry_flow
from homeassistant.components.waze_travel_time.const import ( from homeassistant.components.waze_travel_time.const import (
CONF_AVOID_FERRIES, CONF_AVOID_FERRIES,
@ -21,7 +23,8 @@ from .const import MOCK_CONFIG
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
async def test_minimum_fields(hass, validate_config_entry, bypass_setup): @pytest.mark.usefixtures("validate_config_entry")
async def test_minimum_fields(hass):
"""Test we get the form.""" """Test we get the form."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -45,9 +48,8 @@ async def test_minimum_fields(hass, validate_config_entry, bypass_setup):
} }
async def test_options(hass, validate_config_entry, mock_update): async def test_options(hass):
"""Test options flow.""" """Test options flow."""
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, domain=DOMAIN,
data=MOCK_CONFIG, data=MOCK_CONFIG,
@ -99,7 +101,8 @@ async def test_options(hass, validate_config_entry, mock_update):
} }
async def test_import(hass, validate_config_entry, mock_update): @pytest.mark.usefixtures("validate_config_entry")
async def test_import(hass):
"""Test import for config flow.""" """Test import for config flow."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
@ -139,30 +142,8 @@ async def test_import(hass, validate_config_entry, mock_update):
} }
async def _setup_dupe_import(hass, mock_update): @pytest.mark.usefixtures("validate_config_entry")
"""Set up dupe import.""" async def test_dupe(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_IMPORT},
data={
CONF_ORIGIN: "location1",
CONF_DESTINATION: "location2",
CONF_REGION: "US",
CONF_AVOID_FERRIES: True,
CONF_AVOID_SUBSCRIPTION_ROADS: True,
CONF_AVOID_TOLL_ROADS: True,
CONF_EXCL_FILTER: "exclude",
CONF_INCL_FILTER: "include",
CONF_REALTIME: False,
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
CONF_VEHICLE_TYPE: "taxi",
},
)
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
await hass.async_block_till_done()
async def test_dupe(hass, validate_config_entry, bypass_setup):
"""Test setting up the same entry data twice is OK.""" """Test setting up the same entry data twice is OK."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}
@ -194,7 +175,8 @@ async def test_dupe(hass, validate_config_entry, bypass_setup):
assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
async def test_invalid_config_entry(hass, invalidate_config_entry): @pytest.mark.usefixtures("invalidate_config_entry")
async def test_invalid_config_entry(hass):
"""Test we get the form.""" """Test we get the form."""
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER} DOMAIN, context={"source": config_entries.SOURCE_USER}