Skip to content

Commit

Permalink
Mock all invocations of coronavirus.get_cases (home-assistant#32487)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Mar 5, 2020
1 parent 81810dd commit 81f99ef
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
17 changes: 17 additions & 0 deletions tests/components/coronavirus/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Test helpers."""

from asynctest import Mock, patch
import pytest


@pytest.fixture(autouse=True)
def mock_cases():
"""Mock coronavirus cases."""
with patch(
"coronavirus.get_cases",
return_value=[
Mock(country="Netherlands", confirmed=10, recovered=8, deaths=1, current=1),
Mock(country="Germany", confirmed=1, recovered=0, deaths=0, current=0),
],
) as mock_get_cases:
yield mock_get_cases
16 changes: 4 additions & 12 deletions tests/components/coronavirus/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Test the Coronavirus config flow."""
from asynctest import patch

from homeassistant import config_entries, setup
from homeassistant.components.coronavirus.const import DOMAIN, OPTION_WORLDWIDE

Expand All @@ -14,20 +12,14 @@ async def test_form(hass):
assert result["type"] == "form"
assert result["errors"] == {}

with patch("coronavirus.get_cases", return_value=[],), patch(
"homeassistant.components.coronavirus.async_setup", return_value=True
) as mock_setup, patch(
"homeassistant.components.coronavirus.async_setup_entry", return_value=True,
) as mock_setup_entry:
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {"country": OPTION_WORLDWIDE},
)
result2 = await hass.config_entries.flow.async_configure(
result["flow_id"], {"country": OPTION_WORLDWIDE},
)
assert result2["type"] == "create_entry"
assert result2["title"] == "Worldwide"
assert result2["result"].unique_id == OPTION_WORLDWIDE
assert result2["data"] == {
"country": OPTION_WORLDWIDE,
}
await hass.async_block_till_done()
assert len(mock_setup.mock_calls) == 1
assert len(mock_setup_entry.mock_calls) == 1
assert len(hass.states.async_all()) == 4
13 changes: 2 additions & 11 deletions tests/components/coronavirus/test_init.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Test init of Coronavirus integration."""
from asynctest import Mock, patch

from homeassistant.components.coronavirus.const import DOMAIN, OPTION_WORLDWIDE
from homeassistant.helpers import entity_registry
from homeassistant.setup import async_setup_component
Expand Down Expand Up @@ -33,15 +31,8 @@ async def test_migration(hass):
),
},
)
with patch(
"coronavirus.get_cases",
return_value=[
Mock(country="Netherlands", confirmed=10, recovered=8, deaths=1, current=1),
Mock(country="Germany", confirmed=1, recovered=0, deaths=0, current=0),
],
):
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()

ent_reg = await entity_registry.async_get_registry(hass)

Expand Down

0 comments on commit 81f99ef

Please sign in to comment.