Clean up Flu Near You tests (#64575)

* Clean up Flu Near You tests

* Docstring

* More fixtures

* Revert "More fixtures"

This reverts commit 30f079b6266ef6cb14417ca895da1ae937c87abe.
This commit is contained in:
Aaron Bach 2022-01-20 19:25:49 -07:00 committed by GitHub
parent fc6d6594ab
commit b2811cff51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 83 additions and 41 deletions

View File

@ -0,0 +1,53 @@
"""Define fixtures for Flu Near You tests."""
import json
from unittest.mock import patch
import pytest
from homeassistant.components.flunearyou.const import DOMAIN
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, load_fixture
@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)
entry.add_to_hass(hass)
return entry
@pytest.fixture(name="config")
def config_fixture(hass):
"""Define a config entry data fixture."""
return {
CONF_LATITUDE: 51.528308,
CONF_LONGITUDE: -0.3817765,
}
@pytest.fixture(name="data_cdc", scope="session")
def data_cdc_fixture():
"""Define CDC data."""
return json.loads(load_fixture("cdc_data.json", "flunearyou"))
@pytest.fixture(name="setup_flunearyou")
async def setup_flunearyou_fixture(hass, config, data_cdc):
"""Define a fixture to set up Flu Near You."""
with patch(
"pyflunearyou.cdc.CdcReport.status_by_coordinates", side_effect=data_cdc
), patch("pyflunearyou.user.UserReport.status_by_coordinates"), patch(
"homeassistant.components.flunearyou.PLATFORMS", []
):
assert await async_setup_component(hass, 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"

View File

@ -0,0 +1,10 @@
{
"level": "Low",
"level2": "None",
"week_date": "2020-05-16",
"name": "Washington State",
"fill": {
"color": "#00B7B6",
"opacity": 0.7
}
}

View File

@ -1,44 +1,29 @@
"""Define tests for the flunearyou config flow.""" """Define tests for the flunearyou config flow."""
from unittest.mock import patch
from pyflunearyou.errors import FluNearYouError from pyflunearyou.errors import FluNearYouError
import pytest
from homeassistant import data_entry_flow from homeassistant import data_entry_flow
from homeassistant.components.flunearyou import DOMAIN from homeassistant.components.flunearyou import DOMAIN
from homeassistant.config_entries import SOURCE_USER from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
from tests.common import MockConfigEntry
async def test_duplicate_error(hass, config, config_entry, setup_flunearyou):
async def test_duplicate_error(hass):
"""Test that an error is shown when duplicates are added.""" """Test that an error is shown when duplicates are added."""
conf = {CONF_LATITUDE: "51.528308", CONF_LONGITUDE: "-0.3817765"}
MockConfigEntry(
domain=DOMAIN, unique_id="51.528308, -0.3817765", data=conf
).add_to_hass(hass)
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf DOMAIN, context={"source": SOURCE_USER}, data=config
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
async def test_general_error(hass): @pytest.mark.parametrize("data_cdc", [FluNearYouError])
"""Test that an error is shown on a library error.""" async def test_errors(hass, config, data_cdc, setup_flunearyou):
conf = {CONF_LATITUDE: "51.528308", CONF_LONGITUDE: "-0.3817765"} """Test that exceptions show the appropriate error."""
result = await hass.config_entries.flow.async_init(
with patch( DOMAIN, context={"source": SOURCE_USER}, data=config
"pyflunearyou.cdc.CdcReport.status_by_coordinates", )
side_effect=FluNearYouError, assert result["errors"] == {"base": "unknown"}
):
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=conf
)
assert result["errors"] == {"base": "unknown"}
async def test_show_form(hass): async def test_show_form(hass):
@ -51,20 +36,14 @@ async def test_show_form(hass):
assert result["step_id"] == "user" assert result["step_id"] == "user"
async def test_step_user(hass): async def test_step_user(hass, config, setup_flunearyou):
"""Test that the user step works.""" """Test that the user step works."""
conf = {CONF_LATITUDE: "51.528308", CONF_LONGITUDE: "-0.3817765"} result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_USER}, data=config
with patch( )
"homeassistant.components.flunearyou.async_setup_entry", return_value=True assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
), patch("pyflunearyou.cdc.CdcReport.status_by_coordinates"): assert result["title"] == "51.528308, -0.3817765"
result = await hass.config_entries.flow.async_init( assert result["data"] == {
DOMAIN, context={"source": SOURCE_USER}, data=conf CONF_LATITUDE: 51.528308,
) CONF_LONGITUDE: -0.3817765,
}
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
assert result["title"] == "51.528308, -0.3817765"
assert result["data"] == {
CONF_LATITUDE: "51.528308",
CONF_LONGITUDE: "-0.3817765",
}