core/tests/components/test_updater.py

82 lines
2.8 KiB
Python
Raw Normal View History

2016-03-09 09:25:50 +00:00
"""The tests for the Updater component."""
2015-11-29 20:13:06 +00:00
import unittest
from unittest.mock import patch
import requests
from homeassistant.components import updater
import homeassistant.util.dt as dt_util
2016-02-14 23:08:23 +00:00
from tests.common import fire_time_changed, get_test_home_assistant
2015-11-29 20:13:06 +00:00
NEW_VERSION = '10000.0'
# We need to use a 'real' looking version number to load the updater component
MOCK_CURRENT_VERSION = '10.0'
2015-11-29 20:13:06 +00:00
class TestUpdater(unittest.TestCase):
2016-03-09 09:25:50 +00:00
"""Test the Updater component."""
2015-11-29 20:13:06 +00:00
def setUp(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Setup things to be run when tests are started."""
2016-02-14 23:08:23 +00:00
self.hass = get_test_home_assistant()
2015-11-29 20:13:06 +00:00
def tearDown(self): # pylint: disable=invalid-name
2016-03-09 09:25:50 +00:00
"""Stop everything that was started."""
2015-11-29 20:13:06 +00:00
self.hass.stop()
@patch('homeassistant.components.updater.get_newest_version')
def test_new_version_shows_entity_on_start(self, mock_get_newest_version):
2016-03-09 09:25:50 +00:00
"""Test if new entity is created if new version is available."""
2015-11-29 20:13:06 +00:00
mock_get_newest_version.return_value = NEW_VERSION
updater.CURRENT_VERSION = MOCK_CURRENT_VERSION
2015-11-29 20:13:06 +00:00
self.assertTrue(updater.setup(self.hass, {
'updater': None
}))
self.assertTrue(self.hass.states.is_state(updater.ENTITY_ID,
NEW_VERSION))
@patch('homeassistant.components.updater.get_newest_version')
def test_no_entity_on_same_version(self, mock_get_newest_version):
2016-03-09 09:25:50 +00:00
"""Test if no entity is created if same version."""
mock_get_newest_version.return_value = MOCK_CURRENT_VERSION
updater.CURRENT_VERSION = MOCK_CURRENT_VERSION
2015-11-29 20:13:06 +00:00
self.assertTrue(updater.setup(self.hass, {
'updater': None
}))
self.assertIsNone(self.hass.states.get(updater.ENTITY_ID))
mock_get_newest_version.return_value = NEW_VERSION
fire_time_changed(self.hass,
dt_util.utcnow().replace(hour=0, minute=0, second=0))
self.hass.block_till_done()
2015-11-29 20:13:06 +00:00
self.assertTrue(self.hass.states.is_state(updater.ENTITY_ID,
NEW_VERSION))
@patch('homeassistant.components.updater.requests.get')
def test_errors_while_fetching_new_version(self, mock_get):
2016-03-09 09:25:50 +00:00
"""Test for errors while fetching the new version."""
2015-11-29 20:13:06 +00:00
mock_get.side_effect = requests.RequestException
self.assertIsNone(updater.get_newest_version())
mock_get.side_effect = ValueError
self.assertIsNone(updater.get_newest_version())
mock_get.side_effect = KeyError
self.assertIsNone(updater.get_newest_version())
def test_updater_disabled_on_dev(self):
"""Test if the updater component is disabled on dev."""
updater.CURRENT_VERSION = MOCK_CURRENT_VERSION + 'dev'
self.assertFalse(updater.setup(self.hass, {
'updater': None
}))