From 4d72e41a3e88f696d255dc73e4f4e8ec88b1874f Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Wed, 19 Jan 2022 14:32:52 -0700 Subject: [PATCH] Perform some more AirVisual test cleanup (#64470) * Perform some more AirVisual test cleanup * Smarter startup patch * Simplify --- tests/components/airvisual/conftest.py | 36 ++++++++++---- .../components/airvisual/test_config_flow.py | 48 +++---------------- 2 files changed, 35 insertions(+), 49 deletions(-) diff --git a/tests/components/airvisual/conftest.py b/tests/components/airvisual/conftest.py index cd242b93ca8..119a188066d 100644 --- a/tests/components/airvisual/conftest.py +++ b/tests/components/airvisual/conftest.py @@ -3,30 +3,50 @@ from unittest.mock import patch import pytest -from homeassistant.components.airvisual.const import DOMAIN -from homeassistant.const import CONF_SHOW_ON_MAP +from homeassistant.components.airvisual.const import ( + CONF_INTEGRATION_TYPE, + DOMAIN, + INTEGRATION_TYPE_GEOGRAPHY_COORDS, +) +from homeassistant.const import ( + CONF_API_KEY, + CONF_LATITUDE, + CONF_LONGITUDE, + CONF_SHOW_ON_MAP, +) from homeassistant.setup import async_setup_component from tests.common import MockConfigEntry @pytest.fixture(name="config_entry") -def config_entry_fixture(hass, config, unique_id): +def config_entry_fixture(hass, config, config_entry_version, unique_id): """Define a config entry fixture.""" entry = MockConfigEntry( domain=DOMAIN, unique_id=unique_id, - data=config, + data={CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_COORDS, **config}, options={CONF_SHOW_ON_MAP: True}, + version=config_entry_version, ) entry.add_to_hass(hass) return entry +@pytest.fixture(name="config_entry_version") +def config_entry_version_fixture(): + """Define a config entry version fixture.""" + return 2 + + @pytest.fixture(name="config") def config_fixture(hass): """Define a config entry data fixture.""" - return {} + return { + CONF_API_KEY: "abcde12345", + CONF_LATITUDE: 51.528308, + CONF_LONGITUDE: -0.3817765, + } @pytest.fixture(name="setup_airvisual") @@ -38,10 +58,10 @@ async def setup_airvisual_fixture(hass, config): "pyairvisual.node.NodeSamba.async_get_latest_measurements" ), patch( "pyairvisual.node.NodeSamba.async_disconnect" - ), patch.object( - hass.config_entries, "async_forward_entry_setup" + ), patch( + "homeassistant.components.airvisual.PLATFORMS", [] ): - assert await async_setup_component(hass, DOMAIN, {DOMAIN: config}) + assert await async_setup_component(hass, DOMAIN, config) await hass.async_block_till_done() yield diff --git a/tests/components/airvisual/test_config_flow.py b/tests/components/airvisual/test_config_flow.py index 8e367fb8fc5..253cc94f889 100644 --- a/tests/components/airvisual/test_config_flow.py +++ b/tests/components/airvisual/test_config_flow.py @@ -131,7 +131,7 @@ async def test_errors(hass, data, exc, errors, integration_type): @pytest.mark.parametrize( - "config,unique_id", + "config,config_entry_version,unique_id", [ ( { @@ -145,6 +145,7 @@ async def test_errors(hass, data, exc, errors, integration_type): }, ], }, + 1, "abcde12345", ) ], @@ -175,17 +176,8 @@ async def test_migration(hass, config, config_entry, setup_airvisual, unique_id) @pytest.mark.parametrize( - "config,unique_id", - [ - ( - { - CONF_API_KEY: "abcde12345", - CONF_LATITUDE: 51.528308, - CONF_LONGITUDE: -0.3817765, - }, - "51.528308, -0.3817765", - ) - ], + "unique_id", + [("51.528308, -0.3817765",)], ) async def test_options_flow(hass, config_entry): """Test config flow options.""" @@ -206,18 +198,6 @@ async def test_options_flow(hass, config_entry): assert config_entry.options == {CONF_SHOW_ON_MAP: False} -@pytest.mark.parametrize( - "config", - [ - ( - { - CONF_API_KEY: "abcde12345", - CONF_LATITUDE: 51.528308, - CONF_LONGITUDE: -0.3817765, - } - ) - ], -) async def test_step_geography_by_coords(hass, config, setup_airvisual): """Test setting up a geography entry by latitude/longitude.""" result = await hass.config_entries.flow.async_init( @@ -302,20 +282,6 @@ async def test_step_node_pro(hass, config, setup_airvisual): } -@pytest.mark.parametrize( - "config,unique_id", - [ - ( - { - CONF_API_KEY: "abcde12345", - CONF_LATITUDE: 51.528308, - CONF_LONGITUDE: -0.3817765, - CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_COORDS, - }, - "51.528308, -0.3817765", - ) - ], -) async def test_step_reauth(hass, config_entry): """Test that the reauth step works.""" result = await hass.config_entries.flow.async_init( @@ -329,9 +295,9 @@ async def test_step_reauth(hass, config_entry): new_api_key = "defgh67890" - with patch( - "homeassistant.components.airvisual.async_setup_entry", return_value=True - ), patch("pyairvisual.air_quality.AirQuality.nearest_city", return_value=True): + with patch("homeassistant.config_entries.ConfigEntries.async_reload"), patch( + "pyairvisual.air_quality.AirQuality.nearest_city" + ): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_API_KEY: new_api_key} )