Convert nsw rural fire tests to async (#18112)

This commit is contained in:
Adam Mills 2018-11-02 09:03:05 -04:00 committed by Martin Hjelmare
parent 58c7ee649d
commit 0a301f7dcb

View File

@ -1,8 +1,6 @@
"""The tests for the geojson platform.""" """The tests for the geojson platform."""
import datetime import datetime
import unittest from asynctest.mock import patch, MagicMock
from unittest import mock
from unittest.mock import patch, MagicMock
from homeassistant.components import geo_location from homeassistant.components import geo_location
from homeassistant.components.geo_location import ATTR_SOURCE from homeassistant.components.geo_location import ATTR_SOURCE
@ -13,9 +11,8 @@ from homeassistant.components.geo_location.nsw_rural_fire_service_feed import \
from homeassistant.const import CONF_URL, EVENT_HOMEASSISTANT_START, \ from homeassistant.const import CONF_URL, EVENT_HOMEASSISTANT_START, \
CONF_RADIUS, ATTR_LATITUDE, ATTR_LONGITUDE, ATTR_FRIENDLY_NAME, \ CONF_RADIUS, ATTR_LATITUDE, ATTR_LONGITUDE, ATTR_FRIENDLY_NAME, \
ATTR_UNIT_OF_MEASUREMENT, ATTR_ATTRIBUTION ATTR_UNIT_OF_MEASUREMENT, ATTR_ATTRIBUTION
from homeassistant.setup import setup_component from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant, assert_setup_component, \ from tests.common import assert_setup_component, async_fire_time_changed
fire_time_changed
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
URL = 'http://geo.json.local/geo_json_events.json' URL = 'http://geo.json.local/geo_json_events.json'
@ -30,61 +27,50 @@ CONFIG = {
} }
class TestGeoJsonPlatform(unittest.TestCase): def _generate_mock_feed_entry(external_id, title, distance_to_home,
"""Test the geojson platform.""" coordinates, category=None, location=None,
attribution=None, publication_date=None,
council_area=None, status=None,
entry_type=None, fire=True, size=None,
responsible_agency=None):
"""Construct a mock feed entry for testing purposes."""
feed_entry = MagicMock()
feed_entry.external_id = external_id
feed_entry.title = title
feed_entry.distance_to_home = distance_to_home
feed_entry.coordinates = coordinates
feed_entry.category = category
feed_entry.location = location
feed_entry.attribution = attribution
feed_entry.publication_date = publication_date
feed_entry.council_area = council_area
feed_entry.status = status
feed_entry.type = entry_type
feed_entry.fire = fire
feed_entry.size = size
feed_entry.responsible_agency = responsible_agency
return feed_entry
def setUp(self):
"""Initialize values for this testcase class."""
self.hass = get_test_home_assistant()
def tearDown(self): async def test_setup(hass):
"""Stop everything that was started.""" """Test the general setup of the platform."""
self.hass.stop() # Set up some mock feed entries for this test.
with patch('geojson_client.nsw_rural_fire_service_feed.'
@staticmethod 'NswRuralFireServiceFeed') as mock_feed:
def _generate_mock_feed_entry(external_id, title, distance_to_home, mock_entry_1 = _generate_mock_feed_entry(
coordinates, category=None, location=None,
attribution=None, publication_date=None,
council_area=None, status=None,
entry_type=None, fire=True, size=None,
responsible_agency=None):
"""Construct a mock feed entry for testing purposes."""
feed_entry = MagicMock()
feed_entry.external_id = external_id
feed_entry.title = title
feed_entry.distance_to_home = distance_to_home
feed_entry.coordinates = coordinates
feed_entry.category = category
feed_entry.location = location
feed_entry.attribution = attribution
feed_entry.publication_date = publication_date
feed_entry.council_area = council_area
feed_entry.status = status
feed_entry.type = entry_type
feed_entry.fire = fire
feed_entry.size = size
feed_entry.responsible_agency = responsible_agency
return feed_entry
@mock.patch('geojson_client.nsw_rural_fire_service_feed.'
'NswRuralFireServiceFeed')
def test_setup(self, 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(
'1234', 'Title 1', 15.5, (-31.0, 150.0), category='Category 1', '1234', 'Title 1', 15.5, (-31.0, 150.0), category='Category 1',
location='Location 1', attribution='Attribution 1', location='Location 1', attribution='Attribution 1',
publication_date=datetime.datetime(2018, 9, 22, 8, 0, publication_date=datetime.datetime(2018, 9, 22, 8, 0,
tzinfo=datetime.timezone.utc), tzinfo=datetime.timezone.utc),
council_area='Council Area 1', status='Status 1', council_area='Council Area 1', status='Status 1',
entry_type='Type 1', size='Size 1', responsible_agency='Agency 1') entry_type='Type 1', size='Size 1', responsible_agency='Agency 1')
mock_entry_2 = self._generate_mock_feed_entry('2345', 'Title 2', 20.5, mock_entry_2 = _generate_mock_feed_entry('2345', 'Title 2', 20.5,
(-31.1, 150.1), (-31.1, 150.1),
fire=False) fire=False)
mock_entry_3 = self._generate_mock_feed_entry('3456', 'Title 3', 25.5, mock_entry_3 = _generate_mock_feed_entry('3456', 'Title 3', 25.5,
(-31.2, 150.2)) (-31.2, 150.2))
mock_entry_4 = self._generate_mock_feed_entry('4567', 'Title 4', 12.5, mock_entry_4 = _generate_mock_feed_entry('4567', 'Title 4', 12.5,
(-31.3, 150.3)) (-31.3, 150.3))
mock_feed.return_value.update.return_value = 'OK', [mock_entry_1, mock_feed.return_value.update.return_value = 'OK', [mock_entry_1,
mock_entry_2, mock_entry_2,
mock_entry_3] mock_entry_3]
@ -93,16 +79,17 @@ class TestGeoJsonPlatform(unittest.TestCase):
# Patching 'utcnow' to gain more control over the timed update. # Patching 'utcnow' to gain more control over the timed update.
with patch('homeassistant.util.dt.utcnow', return_value=utcnow): with patch('homeassistant.util.dt.utcnow', return_value=utcnow):
with assert_setup_component(1, geo_location.DOMAIN): with assert_setup_component(1, geo_location.DOMAIN):
assert setup_component(self.hass, geo_location.DOMAIN, CONFIG) assert await async_setup_component(
hass, geo_location.DOMAIN, CONFIG)
# Artificially trigger update. # Artificially trigger update.
self.hass.bus.fire(EVENT_HOMEASSISTANT_START) hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
# Collect events. # 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) == 3 assert len(all_states) == 3
state = self.hass.states.get("geo_location.title_1") state = hass.states.get("geo_location.title_1")
assert state is not None assert state is not None
assert state.name == "Title 1" assert state.name == "Title 1"
assert state.attributes == { assert state.attributes == {
@ -121,7 +108,7 @@ class TestGeoJsonPlatform(unittest.TestCase):
ATTR_SOURCE: 'nsw_rural_fire_service_feed'} ATTR_SOURCE: 'nsw_rural_fire_service_feed'}
assert round(abs(float(state.state)-15.5), 7) == 0 assert round(abs(float(state.state)-15.5), 7) == 0
state = self.hass.states.get("geo_location.title_2") state = hass.states.get("geo_location.title_2")
assert state is not None assert state is not None
assert state.name == "Title 2" assert state.name == "Title 2"
assert state.attributes == { assert state.attributes == {
@ -132,7 +119,7 @@ class TestGeoJsonPlatform(unittest.TestCase):
ATTR_SOURCE: 'nsw_rural_fire_service_feed'} ATTR_SOURCE: 'nsw_rural_fire_service_feed'}
assert round(abs(float(state.state)-20.5), 7) == 0 assert round(abs(float(state.state)-20.5), 7) == 0
state = self.hass.states.get("geo_location.title_3") state = hass.states.get("geo_location.title_3")
assert state is not None assert state is not None
assert state.name == "Title 3" assert state.name == "Title 3"
assert state.attributes == { assert state.attributes == {
@ -147,28 +134,28 @@ class TestGeoJsonPlatform(unittest.TestCase):
# one outdated entry # one outdated entry
mock_feed.return_value.update.return_value = 'OK', [ mock_feed.return_value.update.return_value = 'OK', [
mock_entry_1, mock_entry_4, mock_entry_3] mock_entry_1, mock_entry_4, mock_entry_3]
fire_time_changed(self.hass, utcnow + SCAN_INTERVAL) async_fire_time_changed(hass, utcnow + SCAN_INTERVAL)
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) == 3 assert len(all_states) == 3
# Simulate an update - empty data, but successful update, # Simulate an update - empty data, but successful update,
# so no changes to entities. # so no changes to entities.
mock_feed.return_value.update.return_value = 'OK_NO_DATA', None mock_feed.return_value.update.return_value = 'OK_NO_DATA', None
# mock_restdata.return_value.data = None # mock_restdata.return_value.data = None
fire_time_changed(self.hass, utcnow + async_fire_time_changed(hass, utcnow +
2 * SCAN_INTERVAL) 2 * SCAN_INTERVAL)
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) == 3 assert len(all_states) == 3
# Simulate an update - empty data, removes all entities # Simulate an update - empty data, removes all entities
mock_feed.return_value.update.return_value = 'ERROR', None mock_feed.return_value.update.return_value = 'ERROR', None
fire_time_changed(self.hass, utcnow + async_fire_time_changed(hass, utcnow +
2 * SCAN_INTERVAL) 2 * SCAN_INTERVAL)
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) == 0 assert len(all_states) == 0