Cleanup_google_travel_time_tests (#66868)

This commit is contained in:
Kevin Stillhammer 2022-02-22 08:05:12 +01:00 committed by GitHub
parent d554a82875
commit 2cba9b3d7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 70 deletions

View File

@ -1,21 +1,27 @@
"""Fixtures for Google Time Travel tests.""" """Fixtures for Google Time Travel tests."""
from unittest.mock import Mock, patch from unittest.mock import patch
from googlemaps.exceptions import ApiError from googlemaps.exceptions import ApiError
import pytest import pytest
from homeassistant.components.google_travel_time.const import DOMAIN
@pytest.fixture(name="validate_config_entry") from tests.common import MockConfigEntry
def validate_config_entry_fixture():
"""Return valid config entry."""
with patch( @pytest.fixture(name="mock_config")
"homeassistant.components.google_travel_time.helpers.Client", async def mock_config_fixture(hass, data, options):
return_value=Mock(), """Mock a Google Travel Time config entry."""
), patch( config_entry = MockConfigEntry(
"homeassistant.components.google_travel_time.helpers.distance_matrix", domain=DOMAIN,
return_value=None, data=data,
): options=options,
yield 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()
yield config_entry
@pytest.fixture(name="bypass_setup") @pytest.fixture(name="bypass_setup")
@ -38,21 +44,17 @@ def bypass_platform_setup_fixture():
yield yield
@pytest.fixture(name="bypass_update") @pytest.fixture(name="validate_config_entry")
def bypass_update_fixture(): def validate_config_entry_fixture():
"""Bypass sensor update.""" """Return valid config entry."""
with patch("homeassistant.components.google_travel_time.sensor.distance_matrix"): with patch("homeassistant.components.google_travel_time.helpers.Client"), patch(
yield "homeassistant.components.google_travel_time.helpers.distance_matrix"
) as distance_matrix_mock:
distance_matrix_mock.return_value = None
yield distance_matrix_mock
@pytest.fixture(name="invalidate_config_entry") @pytest.fixture(name="invalidate_config_entry")
def invalidate_config_entry_fixture(): def invalidate_config_entry_fixture(validate_config_entry):
"""Return invalid config entry.""" """Return invalid config entry."""
with patch( validate_config_entry.side_effect = ApiError("test")
"homeassistant.components.google_travel_time.helpers.Client",
return_value=Mock(),
), patch(
"homeassistant.components.google_travel_time.helpers.distance_matrix",
side_effect=ApiError("test"),
):
yield

View File

@ -1,4 +1,6 @@
"""Test the Google Maps Travel Time config flow.""" """Test the Google Maps 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.google_travel_time.const import ( from homeassistant.components.google_travel_time.const import (
ARRIVAL_TIME, ARRIVAL_TIME,
@ -25,11 +27,11 @@ from homeassistant.const import (
CONF_UNIT_SYSTEM_IMPERIAL, CONF_UNIT_SYSTEM_IMPERIAL,
) )
from tests.common import MockConfigEntry
from tests.components.google_travel_time.const import MOCK_CONFIG from tests.components.google_travel_time.const import MOCK_CONFIG
async def test_minimum_fields(hass, validate_config_entry, bypass_setup): @pytest.mark.usefixtures("validate_config_entry", "bypass_setup")
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}
@ -52,7 +54,8 @@ async def test_minimum_fields(hass, validate_config_entry, bypass_setup):
} }
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}
@ -68,22 +71,25 @@ async def test_invalid_config_entry(hass, invalidate_config_entry):
assert result2["errors"] == {"base": "cannot_connect"} assert result2["errors"] == {"base": "cannot_connect"}
async def test_options_flow(hass, validate_config_entry, bypass_update): @pytest.mark.parametrize(
"data,options",
[
(
MOCK_CONFIG,
{
CONF_MODE: "driving",
CONF_ARRIVAL_TIME: "test",
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
},
)
],
)
@pytest.mark.usefixtures("validate_config_entry")
async def test_options_flow(hass, mock_config):
"""Test options flow.""" """Test options flow."""
entry = MockConfigEntry( result = await hass.config_entries.options.async_init(
domain=DOMAIN, mock_config.entry_id, data=None
data=MOCK_CONFIG,
options={
CONF_MODE: "driving",
CONF_ARRIVAL_TIME: "test",
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
},
) )
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(entry.entry_id, data=None)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init" assert result["step_id"] == "init"
@ -115,7 +121,7 @@ async def test_options_flow(hass, validate_config_entry, bypass_update):
CONF_TRANSIT_ROUTING_PREFERENCE: "less_walking", CONF_TRANSIT_ROUTING_PREFERENCE: "less_walking",
} }
assert entry.options == { assert mock_config.options == {
CONF_MODE: "driving", CONF_MODE: "driving",
CONF_LANGUAGE: "en", CONF_LANGUAGE: "en",
CONF_AVOID: "tolls", CONF_AVOID: "tolls",
@ -127,17 +133,16 @@ async def test_options_flow(hass, validate_config_entry, bypass_update):
} }
async def test_options_flow_departure_time(hass, validate_config_entry, bypass_update): @pytest.mark.parametrize(
"""Test options flow wiith departure time.""" "data,options",
entry = MockConfigEntry( [(MOCK_CONFIG, {})],
domain=DOMAIN, )
data=MOCK_CONFIG, @pytest.mark.usefixtures("validate_config_entry")
async def test_options_flow_departure_time(hass, mock_config):
"""Test options flow with departure time."""
result = await hass.config_entries.options.async_init(
mock_config.entry_id, data=None
) )
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
result = await hass.config_entries.options.async_init(entry.entry_id, data=None)
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
assert result["step_id"] == "init" assert result["step_id"] == "init"
@ -169,7 +174,7 @@ async def test_options_flow_departure_time(hass, validate_config_entry, bypass_u
CONF_TRANSIT_ROUTING_PREFERENCE: "less_walking", CONF_TRANSIT_ROUTING_PREFERENCE: "less_walking",
} }
assert entry.options == { assert mock_config.options == {
CONF_MODE: "driving", CONF_MODE: "driving",
CONF_LANGUAGE: "en", CONF_LANGUAGE: "en",
CONF_AVOID: "tolls", CONF_AVOID: "tolls",
@ -181,7 +186,8 @@ async def test_options_flow_departure_time(hass, validate_config_entry, bypass_u
} }
async def test_dupe(hass, validate_config_entry, bypass_setup): @pytest.mark.usefixtures("validate_config_entry", "bypass_setup")
async def test_dupe(hass):
"""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}

View File

@ -16,20 +16,6 @@ from .const import MOCK_CONFIG
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@pytest.fixture(name="mock_config")
async def mock_config_fixture(hass, data, options):
"""Mock a Google Travel Time config entry."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data=data,
options=options,
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") @pytest.fixture(name="mock_update")
def mock_update_fixture(): def mock_update_fixture():
"""Mock an update to the sensor.""" """Mock an update to the sensor."""