mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
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:
parent
fc6d6594ab
commit
b2811cff51
53
tests/components/flunearyou/conftest.py
Normal file
53
tests/components/flunearyou/conftest.py
Normal 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"
|
10
tests/components/flunearyou/fixtures/cdc_data.json
Normal file
10
tests/components/flunearyou/fixtures/cdc_data.json
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"level": "Low",
|
||||
"level2": "None",
|
||||
"week_date": "2020-05-16",
|
||||
"name": "Washington State",
|
||||
"fill": {
|
||||
"color": "#00B7B6",
|
||||
"opacity": 0.7
|
||||
}
|
||||
}
|
@ -1,44 +1,29 @@
|
||||
"""Define tests for the flunearyou config flow."""
|
||||
from unittest.mock import patch
|
||||
|
||||
from pyflunearyou.errors import FluNearYouError
|
||||
import pytest
|
||||
|
||||
from homeassistant import data_entry_flow
|
||||
from homeassistant.components.flunearyou import DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_duplicate_error(hass):
|
||||
async def test_duplicate_error(hass, config, config_entry, setup_flunearyou):
|
||||
"""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(
|
||||
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["reason"] == "already_configured"
|
||||
|
||||
|
||||
async def test_general_error(hass):
|
||||
"""Test that an error is shown on a library error."""
|
||||
conf = {CONF_LATITUDE: "51.528308", CONF_LONGITUDE: "-0.3817765"}
|
||||
|
||||
with patch(
|
||||
"pyflunearyou.cdc.CdcReport.status_by_coordinates",
|
||||
side_effect=FluNearYouError,
|
||||
):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
||||
)
|
||||
assert result["errors"] == {"base": "unknown"}
|
||||
@pytest.mark.parametrize("data_cdc", [FluNearYouError])
|
||||
async def test_errors(hass, config, data_cdc, setup_flunearyou):
|
||||
"""Test that exceptions show the appropriate error."""
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
)
|
||||
assert result["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_show_form(hass):
|
||||
@ -51,20 +36,14 @@ async def test_show_form(hass):
|
||||
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."""
|
||||
conf = {CONF_LATITUDE: "51.528308", CONF_LONGITUDE: "-0.3817765"}
|
||||
|
||||
with patch(
|
||||
"homeassistant.components.flunearyou.async_setup_entry", return_value=True
|
||||
), patch("pyflunearyou.cdc.CdcReport.status_by_coordinates"):
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=conf
|
||||
)
|
||||
|
||||
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",
|
||||
}
|
||||
result = await hass.config_entries.flow.async_init(
|
||||
DOMAIN, context={"source": SOURCE_USER}, data=config
|
||||
)
|
||||
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,
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user