Geo rss test style (#42038)

pull/41845/head
Julian Engelhardt 2020-10-19 11:59:04 +02:00 committed by GitHub
parent 5a533a8c5d
commit 97b1fffad4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 104 additions and 108 deletions

View File

@ -1,6 +1,5 @@
"""The test for the geo rss events sensor platform."""
import unittest
from unittest import mock
import pytest
from homeassistant.components import sensor
import homeassistant.components.geo_rss_events.sensor as geo_rss_events
@ -10,15 +9,11 @@ from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT,
EVENT_HOMEASSISTANT_START,
)
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from tests.async_mock import MagicMock, patch
from tests.common import (
assert_setup_component,
fire_time_changed,
get_test_home_assistant,
)
from tests.common import assert_setup_component, async_fire_time_changed
URL = "http://geo.rss.local/geo_rss_events.xml"
VALID_CONFIG_WITH_CATEGORIES = {
@ -34,17 +29,18 @@ VALID_CONFIG = {
sensor.DOMAIN: [{"platform": "geo_rss_events", geo_rss_events.CONF_URL: URL}]
}
class TestGeoRssServiceUpdater(unittest.TestCase):
"""Test the GeoRss service updater."""
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
# self.config = VALID_CONFIG_WITHOUT_CATEGORIES
self.addCleanup(self.hass.stop)
@staticmethod
@pytest.fixture
def mock_feed():
"""Pytest fixture for homeassistant.components.geo_rss_events.sensor.GenericFeed."""
with patch(
"homeassistant.components.geo_rss_events.sensor.GenericFeed"
) as mock_feed:
yield mock_feed
def _generate_mock_feed_entry(
external_id, title, distance_to_home, coordinates, category
):
@ -57,14 +53,14 @@ class TestGeoRssServiceUpdater(unittest.TestCase):
feed_entry.category = category
return feed_entry
@mock.patch("homeassistant.components.geo_rss_events.sensor.GenericFeed")
def test_setup(self, mock_feed):
async def test_setup(hass, mock_feed):
"""Test the general setup of the platform."""
# Set up some mock feed entries for this test.
mock_entry_1 = self._generate_mock_feed_entry(
mock_entry_1 = _generate_mock_feed_entry(
"1234", "Title 1", 15.5, (-31.0, 150.0), "Category 1"
)
mock_entry_2 = self._generate_mock_feed_entry(
mock_entry_2 = _generate_mock_feed_entry(
"2345", "Title 2", 20.5, (-31.1, 150.1), "Category 1"
)
mock_feed.return_value.update.return_value = "OK", [mock_entry_1, mock_entry_2]
@ -73,16 +69,16 @@ class TestGeoRssServiceUpdater(unittest.TestCase):
# Patching 'utcnow' to gain more control over the timed update.
with patch("homeassistant.util.dt.utcnow", return_value=utcnow):
with assert_setup_component(1, sensor.DOMAIN):
assert setup_component(self.hass, sensor.DOMAIN, VALID_CONFIG)
assert await async_setup_component(hass, sensor.DOMAIN, VALID_CONFIG)
# Artificially trigger update.
self.hass.bus.fire(EVENT_HOMEASSISTANT_START)
hass.bus.fire(EVENT_HOMEASSISTANT_START)
# Collect events.
self.hass.block_till_done()
await hass.async_block_till_done()
all_states = self.hass.states.all()
all_states = hass.states.async_all()
assert len(all_states) == 1
state = self.hass.states.get("sensor.event_service_any")
state = hass.states.get("sensor.event_service_any")
assert state is not None
assert state.name == "Event Service Any"
assert int(state.state) == 2
@ -97,22 +93,22 @@ class TestGeoRssServiceUpdater(unittest.TestCase):
# Simulate an update - empty data, but successful update,
# so no changes to entities.
mock_feed.return_value.update.return_value = "OK_NO_DATA", None
fire_time_changed(self.hass, utcnow + geo_rss_events.SCAN_INTERVAL)
self.hass.block_till_done()
async_fire_time_changed(hass, utcnow + geo_rss_events.SCAN_INTERVAL)
await hass.async_block_till_done()
all_states = self.hass.states.all()
all_states = hass.states.async_all()
assert len(all_states) == 1
state = self.hass.states.get("sensor.event_service_any")
state = hass.states.get("sensor.event_service_any")
assert int(state.state) == 2
# Simulate an update - empty data, removes all entities
mock_feed.return_value.update.return_value = "ERROR", None
fire_time_changed(self.hass, utcnow + 2 * geo_rss_events.SCAN_INTERVAL)
self.hass.block_till_done()
async_fire_time_changed(hass, utcnow + 2 * geo_rss_events.SCAN_INTERVAL)
await hass.async_block_till_done()
all_states = self.hass.states.all()
all_states = hass.states.async_all()
assert len(all_states) == 1
state = self.hass.states.get("sensor.event_service_any")
state = hass.states.get("sensor.event_service_any")
assert int(state.state) == 0
assert state.attributes == {
ATTR_FRIENDLY_NAME: "Event Service Any",
@ -120,31 +116,31 @@ class TestGeoRssServiceUpdater(unittest.TestCase):
ATTR_ICON: "mdi:alert",
}
@mock.patch("homeassistant.components.geo_rss_events.sensor.GenericFeed")
def test_setup_with_categories(self, mock_feed):
async def test_setup_with_categories(hass, mock_feed):
"""Test the general setup of the platform."""
# Set up some mock feed entries for this test.
mock_entry_1 = self._generate_mock_feed_entry(
mock_entry_1 = _generate_mock_feed_entry(
"1234", "Title 1", 15.5, (-31.0, 150.0), "Category 1"
)
mock_entry_2 = self._generate_mock_feed_entry(
mock_entry_2 = _generate_mock_feed_entry(
"2345", "Title 2", 20.5, (-31.1, 150.1), "Category 1"
)
mock_feed.return_value.update.return_value = "OK", [mock_entry_1, mock_entry_2]
with assert_setup_component(1, sensor.DOMAIN):
assert setup_component(
self.hass, sensor.DOMAIN, VALID_CONFIG_WITH_CATEGORIES
assert await async_setup_component(
hass, sensor.DOMAIN, VALID_CONFIG_WITH_CATEGORIES
)
# Artificially trigger update.
self.hass.bus.fire(EVENT_HOMEASSISTANT_START)
hass.bus.fire(EVENT_HOMEASSISTANT_START)
# Collect events.
self.hass.block_till_done()
await hass.async_block_till_done()
all_states = self.hass.states.all()
all_states = hass.states.async_all()
assert len(all_states) == 1
state = self.hass.states.get("sensor.event_service_category_1")
state = hass.states.get("sensor.event_service_category_1")
assert state is not None
assert state.name == "Event Service Category 1"
assert int(state.state) == 2