From 573e17472b3b602f40e8cf2271af62df77dead7c Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Wed, 19 Jan 2022 11:52:24 -0700 Subject: [PATCH] Clean up AirVisual tests (#64402) * Simplify AirVisual tests * First fixture * More * Fixture * More --- tests/components/airvisual/conftest.py | 52 ++ .../components/airvisual/test_config_flow.py | 462 ++++++++---------- 2 files changed, 269 insertions(+), 245 deletions(-) create mode 100644 tests/components/airvisual/conftest.py diff --git a/tests/components/airvisual/conftest.py b/tests/components/airvisual/conftest.py new file mode 100644 index 00000000000..cd242b93ca8 --- /dev/null +++ b/tests/components/airvisual/conftest.py @@ -0,0 +1,52 @@ +"""Define test fixtures for AirVisual.""" +from unittest.mock import patch + +import pytest + +from homeassistant.components.airvisual.const import DOMAIN +from homeassistant.const import 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): + """Define a config entry fixture.""" + entry = MockConfigEntry( + domain=DOMAIN, + unique_id=unique_id, + data=config, + options={CONF_SHOW_ON_MAP: True}, + ) + entry.add_to_hass(hass) + return entry + + +@pytest.fixture(name="config") +def config_fixture(hass): + """Define a config entry data fixture.""" + return {} + + +@pytest.fixture(name="setup_airvisual") +async def setup_airvisual_fixture(hass, config): + """Define a fixture to set up AirVisual.""" + with patch("pyairvisual.air_quality.AirQuality.city"), patch( + "pyairvisual.air_quality.AirQuality.nearest_city" + ), patch("pyairvisual.node.NodeSamba.async_connect"), patch( + "pyairvisual.node.NodeSamba.async_get_latest_measurements" + ), patch( + "pyairvisual.node.NodeSamba.async_disconnect" + ), patch.object( + hass.config_entries, "async_forward_entry_setup" + ): + assert await async_setup_component(hass, DOMAIN, {DOMAIN: config}) + await hass.async_block_till_done() + yield + + +@pytest.fixture(name="unique_id") +def unique_id_fixture(hass): + """Define a config entry unique ID fixture.""" + return "51.528308, -0.3817765" diff --git a/tests/components/airvisual/test_config_flow.py b/tests/components/airvisual/test_config_flow.py index b4974223830..8e367fb8fc5 100644 --- a/tests/components/airvisual/test_config_flow.py +++ b/tests/components/airvisual/test_config_flow.py @@ -7,6 +7,7 @@ from pyairvisual.errors import ( NodeProError, NotFoundError, ) +import pytest from homeassistant import data_entry_flow from homeassistant.components.airvisual.const import ( @@ -29,167 +30,128 @@ from homeassistant.const import ( CONF_SHOW_ON_MAP, CONF_STATE, ) -from homeassistant.setup import async_setup_component - -from tests.common import MockConfigEntry -async def test_duplicate_error(hass): - """Test that errors are shown when duplicate entries are added.""" - geography_conf = { - CONF_API_KEY: "abcde12345", - CONF_LATITUDE: 51.528308, - CONF_LONGITUDE: -0.3817765, - } - - MockConfigEntry( - domain=DOMAIN, unique_id="51.528308, -0.3817765", data=geography_conf - ).add_to_hass(hass) - - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_USER}, - data={"type": INTEGRATION_TYPE_GEOGRAPHY_COORDS}, - ) - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input=geography_conf - ) - - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT - assert result["reason"] == "already_configured" - - node_pro_conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PASSWORD: "12345"} - - MockConfigEntry( - domain=DOMAIN, unique_id="192.168.1.100", data=node_pro_conf - ).add_to_hass(hass) - - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data={"type": "AirVisual Node/Pro"} - ) - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input=node_pro_conf - ) - - assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT - assert result["reason"] == "already_configured" - - -async def test_invalid_identifier_geography_api_key(hass): - """Test that an invalid API key throws an error.""" - with patch( - "pyairvisual.air_quality.AirQuality.nearest_city", - side_effect=InvalidKeyError, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_USER}, - data={"type": INTEGRATION_TYPE_GEOGRAPHY_COORDS}, - ) - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={ +@pytest.mark.parametrize( + "config,data,unique_id", + [ + ( + { CONF_API_KEY: "abcde12345", CONF_LATITUDE: 51.528308, CONF_LONGITUDE: -0.3817765, }, - ) - - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM - assert result["errors"] == {CONF_API_KEY: "invalid_api_key"} + { + "type": INTEGRATION_TYPE_GEOGRAPHY_COORDS, + }, + "51.528308, -0.3817765", + ), + ( + { + CONF_IP_ADDRESS: "192.168.1.100", + CONF_PASSWORD: "12345", + }, + { + "type": INTEGRATION_TYPE_NODE_PRO, + }, + "192.168.1.100", + ), + ], +) +async def test_duplicate_error(hass, config, config_entry, data): + """Test that errors are shown when duplicate entries are added.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data=data + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=config + ) + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["reason"] == "already_configured" -async def test_invalid_identifier_geography_name(hass): - """Test that an invalid location name throws an error.""" - with patch( - "pyairvisual.air_quality.AirQuality.city", - side_effect=NotFoundError, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_USER}, - data={"type": INTEGRATION_TYPE_GEOGRAPHY_NAME}, - ) - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={ +@pytest.mark.parametrize( + "data,exc,errors,integration_type", + [ + ( + { CONF_API_KEY: "abcde12345", CONF_CITY: "Beijing", CONF_STATE: "Beijing", CONF_COUNTRY: "China", }, - ) - - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM - assert result["errors"] == {CONF_CITY: "location_not_found"} - - -async def test_invalid_identifier_geography_unknown(hass): - """Test that an unknown identifier issue throws an error.""" - with patch( - "pyairvisual.air_quality.AirQuality.city", - side_effect=AirVisualError, - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_USER}, - data={"type": INTEGRATION_TYPE_GEOGRAPHY_NAME}, - ) - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={ + InvalidKeyError, + {CONF_API_KEY: "invalid_api_key"}, + INTEGRATION_TYPE_GEOGRAPHY_NAME, + ), + ( + { CONF_API_KEY: "abcde12345", CONF_CITY: "Beijing", CONF_STATE: "Beijing", CONF_COUNTRY: "China", }, - ) - - assert result["type"] == data_entry_flow.RESULT_TYPE_FORM - assert result["errors"] == {"base": "unknown"} - - -async def test_invalid_identifier_node_pro(hass): - """Test that an invalid Node/Pro identifier shows an error.""" - node_pro_conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PASSWORD: "my_password"} - - with patch( - "pyairvisual.node.NodeSamba.async_connect", - side_effect=NodeProError, - ): + NotFoundError, + {CONF_CITY: "location_not_found"}, + INTEGRATION_TYPE_GEOGRAPHY_NAME, + ), + ( + { + CONF_API_KEY: "abcde12345", + CONF_CITY: "Beijing", + CONF_STATE: "Beijing", + CONF_COUNTRY: "China", + }, + AirVisualError, + {"base": "unknown"}, + INTEGRATION_TYPE_GEOGRAPHY_NAME, + ), + ( + { + CONF_IP_ADDRESS: "192.168.1.100", + CONF_PASSWORD: "my_password", + }, + NodeProError, + {CONF_IP_ADDRESS: "cannot_connect"}, + INTEGRATION_TYPE_NODE_PRO, + ), + ], +) +async def test_errors(hass, data, exc, errors, integration_type): + """Test that an exceptions show an error.""" + with patch("pyairvisual.air_quality.AirQuality.city", side_effect=exc): result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data={"type": "AirVisual Node/Pro"} + DOMAIN, context={"source": SOURCE_USER}, data={"type": integration_type} ) result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input=node_pro_conf + result["flow_id"], user_input=data ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM - assert result["errors"] == {CONF_IP_ADDRESS: "cannot_connect"} + assert result["errors"] == errors -async def test_migration(hass): +@pytest.mark.parametrize( + "config,unique_id", + [ + ( + { + CONF_API_KEY: "abcde12345", + CONF_GEOGRAPHIES: [ + {CONF_LATITUDE: 51.528308, CONF_LONGITUDE: -0.3817765}, + { + CONF_CITY: "Beijing", + CONF_STATE: "Beijing", + CONF_COUNTRY: "China", + }, + ], + }, + "abcde12345", + ) + ], +) +async def test_migration(hass, config, config_entry, setup_airvisual, unique_id): """Test migrating from version 1 to the current version.""" - conf = { - CONF_API_KEY: "abcde12345", - CONF_GEOGRAPHIES: [ - {CONF_LATITUDE: 51.528308, CONF_LONGITUDE: -0.3817765}, - {CONF_CITY: "Beijing", CONF_STATE: "Beijing", CONF_COUNTRY: "China"}, - ], - } - - entry = MockConfigEntry(domain=DOMAIN, version=1, unique_id="abcde12345", data=conf) - entry.add_to_hass(hass) - - assert len(hass.config_entries.async_entries(DOMAIN)) == 1 - - with patch("pyairvisual.air_quality.AirQuality.city"), patch( - "pyairvisual.air_quality.AirQuality.nearest_city" - ), patch.object(hass.config_entries, "async_forward_entry_setup"): - assert await async_setup_component(hass, DOMAIN, {DOMAIN: conf}) - await hass.async_block_till_done() - config_entries = hass.config_entries.async_entries(DOMAIN) - assert len(config_entries) == 2 assert config_entries[0].unique_id == "51.528308, -0.3817765" @@ -212,27 +174,26 @@ async def test_migration(hass): } -async def test_options_flow(hass): +@pytest.mark.parametrize( + "config,unique_id", + [ + ( + { + CONF_API_KEY: "abcde12345", + CONF_LATITUDE: 51.528308, + CONF_LONGITUDE: -0.3817765, + }, + "51.528308, -0.3817765", + ) + ], +) +async def test_options_flow(hass, config_entry): """Test config flow options.""" - geography_conf = { - CONF_API_KEY: "abcde12345", - CONF_LATITUDE: 51.528308, - CONF_LONGITUDE: -0.3817765, - } - - entry = MockConfigEntry( - domain=DOMAIN, - unique_id="51.528308, -0.3817765", - data=geography_conf, - options={CONF_SHOW_ON_MAP: True}, - ) - entry.add_to_hass(hass) - with patch( "homeassistant.components.airvisual.async_setup_entry", return_value=True ): - await hass.config_entries.async_setup(entry.entry_id) - result = await hass.config_entries.options.async_init(entry.entry_id) + await hass.config_entries.async_setup(config_entry.entry_id) + result = await hass.config_entries.options.async_init(config_entry.entry_id) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "init" @@ -242,112 +203,123 @@ async def test_options_flow(hass): ) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - assert entry.options == {CONF_SHOW_ON_MAP: False} + assert config_entry.options == {CONF_SHOW_ON_MAP: False} -async def test_step_geography_by_coords(hass): +@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.""" - conf = { - CONF_API_KEY: "abcde12345", - CONF_LATITUDE: 51.528308, - CONF_LONGITUDE: -0.3817765, - } + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data={"type": INTEGRATION_TYPE_GEOGRAPHY_COORDS}, + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=config + ) - with patch( - "homeassistant.components.airvisual.async_setup_entry", return_value=True - ), patch("pyairvisual.air_quality.AirQuality.nearest_city"): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_USER}, - data={"type": INTEGRATION_TYPE_GEOGRAPHY_COORDS}, - ) - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input=conf - ) - - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - assert result["title"] == "Cloud API (51.528308, -0.3817765)" - assert result["data"] == { - CONF_API_KEY: "abcde12345", - CONF_LATITUDE: 51.528308, - CONF_LONGITUDE: -0.3817765, - CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_COORDS, - } - - -async def test_step_geography_by_name(hass): - """Test setting up a geography entry by city/state/country.""" - conf = { - CONF_API_KEY: "abcde12345", - CONF_CITY: "Beijing", - CONF_STATE: "Beijing", - CONF_COUNTRY: "China", - } - - with patch( - "homeassistant.components.airvisual.async_setup_entry", return_value=True - ), patch("pyairvisual.air_quality.AirQuality.city"): - result = await hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_USER}, - data={"type": INTEGRATION_TYPE_GEOGRAPHY_NAME}, - ) - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input=conf - ) - - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - assert result["title"] == "Cloud API (Beijing, Beijing, China)" - assert result["data"] == { - CONF_API_KEY: "abcde12345", - CONF_CITY: "Beijing", - CONF_STATE: "Beijing", - CONF_COUNTRY: "China", - CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_NAME, - } - - -async def test_step_node_pro(hass): - """Test the Node/Pro step.""" - conf = {CONF_IP_ADDRESS: "192.168.1.100", CONF_PASSWORD: "my_password"} - - with patch( - "homeassistant.components.airvisual.async_setup_entry", return_value=True - ), patch("pyairvisual.node.NodeSamba.async_connect"), patch( - "pyairvisual.node.NodeSamba.async_get_latest_measurements" - ), patch( - "pyairvisual.node.NodeSamba.async_disconnect" - ): - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_USER}, data={"type": "AirVisual Node/Pro"} - ) - result = await hass.config_entries.flow.async_configure( - result["flow_id"], user_input=conf - ) - assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY - assert result["title"] == "Node/Pro (192.168.1.100)" - assert result["data"] == { - CONF_IP_ADDRESS: "192.168.1.100", - CONF_PASSWORD: "my_password", - CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_NODE_PRO, - } - - -async def test_step_reauth(hass): - """Test that the reauth step works.""" - entry_data = { + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["title"] == "Cloud API (51.528308, -0.3817765)" + assert result["data"] == { CONF_API_KEY: "abcde12345", CONF_LATITUDE: 51.528308, CONF_LONGITUDE: -0.3817765, CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_COORDS, } - MockConfigEntry( - domain=DOMAIN, unique_id="51.528308, -0.3817765", data=entry_data - ).add_to_hass(hass) +@pytest.mark.parametrize( + "config", + [ + ( + { + CONF_API_KEY: "abcde12345", + CONF_CITY: "Beijing", + CONF_STATE: "Beijing", + CONF_COUNTRY: "China", + } + ) + ], +) +async def test_step_geography_by_name(hass, config, setup_airvisual): + """Test setting up a geography entry by city/state/country.""" result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": SOURCE_REAUTH}, data=entry_data + DOMAIN, + context={"source": SOURCE_USER}, + data={"type": INTEGRATION_TYPE_GEOGRAPHY_NAME}, + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=config + ) + + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["title"] == "Cloud API (Beijing, Beijing, China)" + assert result["data"] == { + CONF_API_KEY: "abcde12345", + CONF_CITY: "Beijing", + CONF_STATE: "Beijing", + CONF_COUNTRY: "China", + CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_GEOGRAPHY_NAME, + } + + +@pytest.mark.parametrize( + "config", + [ + ( + { + CONF_IP_ADDRESS: "192.168.1.100", + CONF_PASSWORD: "my_password", + } + ), + ], +) +async def test_step_node_pro(hass, config, setup_airvisual): + """Test the Node/Pro step.""" + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data={"type": "AirVisual Node/Pro"} + ) + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input=config + ) + assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY + assert result["title"] == "Node/Pro (192.168.1.100)" + assert result["data"] == { + CONF_IP_ADDRESS: "192.168.1.100", + CONF_PASSWORD: "my_password", + CONF_INTEGRATION_TYPE: INTEGRATION_TYPE_NODE_PRO, + } + + +@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( + DOMAIN, context={"source": SOURCE_REAUTH}, data=config_entry.data ) assert result["step_id"] == "reauth_confirm"