Cleanup_google_travel_time_tests (#66868)

pull/67016/head
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."""
from unittest.mock import Mock, patch
from unittest.mock import patch
from googlemaps.exceptions import ApiError
import pytest
from homeassistant.components.google_travel_time.const import DOMAIN
@pytest.fixture(name="validate_config_entry")
def validate_config_entry_fixture():
"""Return valid config entry."""
with patch(
"homeassistant.components.google_travel_time.helpers.Client",
return_value=Mock(),
), patch(
"homeassistant.components.google_travel_time.helpers.distance_matrix",
return_value=None,
):
yield
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()
yield config_entry
@pytest.fixture(name="bypass_setup")
@ -38,21 +44,17 @@ def bypass_platform_setup_fixture():
yield
@pytest.fixture(name="bypass_update")
def bypass_update_fixture():
"""Bypass sensor update."""
with patch("homeassistant.components.google_travel_time.sensor.distance_matrix"):
yield
@pytest.fixture(name="validate_config_entry")
def validate_config_entry_fixture():
"""Return valid config entry."""
with patch("homeassistant.components.google_travel_time.helpers.Client"), patch(
"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")
def invalidate_config_entry_fixture():
def invalidate_config_entry_fixture(validate_config_entry):
"""Return invalid config entry."""
with patch(
"homeassistant.components.google_travel_time.helpers.Client",
return_value=Mock(),
), patch(
"homeassistant.components.google_travel_time.helpers.distance_matrix",
side_effect=ApiError("test"),
):
yield
validate_config_entry.side_effect = ApiError("test")

View File

@ -1,4 +1,6 @@
"""Test the Google Maps Travel Time config flow."""
import pytest
from homeassistant import config_entries, data_entry_flow
from homeassistant.components.google_travel_time.const import (
ARRIVAL_TIME,
@ -25,11 +27,11 @@ from homeassistant.const import (
CONF_UNIT_SYSTEM_IMPERIAL,
)
from tests.common import MockConfigEntry
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."""
result = await hass.config_entries.flow.async_init(
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."""
result = await hass.config_entries.flow.async_init(
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"}
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."""
entry = MockConfigEntry(
domain=DOMAIN,
data=MOCK_CONFIG,
options={
CONF_MODE: "driving",
CONF_ARRIVAL_TIME: "test",
CONF_UNITS: CONF_UNIT_SYSTEM_IMPERIAL,
},
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["step_id"] == "init"
@ -115,7 +121,7 @@ async def test_options_flow(hass, validate_config_entry, bypass_update):
CONF_TRANSIT_ROUTING_PREFERENCE: "less_walking",
}
assert entry.options == {
assert mock_config.options == {
CONF_MODE: "driving",
CONF_LANGUAGE: "en",
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):
"""Test options flow wiith departure time."""
entry = MockConfigEntry(
domain=DOMAIN,
data=MOCK_CONFIG,
@pytest.mark.parametrize(
"data,options",
[(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["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",
}
assert entry.options == {
assert mock_config.options == {
CONF_MODE: "driving",
CONF_LANGUAGE: "en",
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."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}

View File

@ -16,20 +16,6 @@ from .const import MOCK_CONFIG
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")
def mock_update_fixture():
"""Mock an update to the sensor."""