From 2cba9b3d7e91d22bd38cc5ef9f9ecdc380c905e1 Mon Sep 17 00:00:00 2001 From: Kevin Stillhammer Date: Tue, 22 Feb 2022 08:05:12 +0100 Subject: [PATCH] Cleanup_google_travel_time_tests (#66868) --- .../components/google_travel_time/conftest.py | 54 +++++++-------- .../google_travel_time/test_config_flow.py | 66 ++++++++++--------- .../google_travel_time/test_sensor.py | 14 ---- 3 files changed, 64 insertions(+), 70 deletions(-) diff --git a/tests/components/google_travel_time/conftest.py b/tests/components/google_travel_time/conftest.py index 7f668383c4b..4ca7c5a9105 100644 --- a/tests/components/google_travel_time/conftest.py +++ b/tests/components/google_travel_time/conftest.py @@ -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") diff --git a/tests/components/google_travel_time/test_config_flow.py b/tests/components/google_travel_time/test_config_flow.py index d81d63e3af1..1426c749552 100644 --- a/tests/components/google_travel_time/test_config_flow.py +++ b/tests/components/google_travel_time/test_config_flow.py @@ -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} diff --git a/tests/components/google_travel_time/test_sensor.py b/tests/components/google_travel_time/test_sensor.py index 6b203f51e98..daedcfef4c1 100644 --- a/tests/components/google_travel_time/test_sensor.py +++ b/tests/components/google_travel_time/test_sensor.py @@ -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."""