Continue on invalid platforms and new setup_component unit tests (#3736)
This commit is contained in:
1 parent
1b26b5ad14
commit
7b40a641ec
23 files changed
+842
-672
No files matched your search
@@ -57,12 +57,12 @@ class TestDarkSkySetup(unittest.TestCase):
|
||||
|
||||
@requests_mock.Mocker()
|
||||
@patch('forecastio.api.get_forecast', wraps=forecastio.api.get_forecast)
|
||||
def test_setup(self, m, mock_get_forecast):
|
||||
def test_setup(self, mock_req, mock_get_forecast):
|
||||
"""Test for successfully setting up the forecast.io platform."""
|
||||
uri = ('https://api.darksky.net\/forecast\/(\w+)\/'
|
||||
'(-?\d+\.?\d*),(-?\d+\.?\d*)')
|
||||
m.get(re.compile(uri),
|
||||
text=load_fixture('darksky.json'))
|
||||
uri = (r'https://api.(darksky.net|forecast.io)\/forecast\/(\w+)\/'
|
||||
r'(-?\d+\.?\d*),(-?\d+\.?\d*)')
|
||||
mock_req.get(re.compile(uri),
|
||||
text=load_fixture('darksky.json'))
|
||||
darksky.setup_platform(self.hass, self.config, MagicMock())
|
||||
self.assertTrue(mock_get_forecast.called)
|
||||
self.assertEqual(mock_get_forecast.call_count, 1)
|
||||
@@ -4,10 +4,10 @@ from unittest.mock import MagicMock
|
||||
|
||||
import requests_mock
|
||||
|
||||
from homeassistant import core as ha
|
||||
from homeassistant.components.sensor import sleepiq
|
||||
|
||||
from tests.components.test_sleepiq import mock_responses
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
class TestSleepIQSensorSetup(unittest.TestCase):
|
||||
@@ -22,7 +22,7 @@ class TestSleepIQSensorSetup(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Initialize values for this testcase class."""
|
||||
self.hass = ha.HomeAssistant()
|
||||
self.hass = get_test_home_assistant()
|
||||
self.username = 'foo'
|
||||
self.password = 'bar'
|
||||
self.config = {
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
"""The test for the Template sensor platform."""
|
||||
import homeassistant.bootstrap as bootstrap
|
||||
from homeassistant.bootstrap import setup_component
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.common import get_test_home_assistant, assert_setup_component
|
||||
|
||||
|
||||
class TestTemplateSensor:
|
||||
"""Test the Template sensor."""
|
||||
|
||||
hass = None
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Setup things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
@@ -17,17 +20,18 @@ class TestTemplateSensor:
|
||||
|
||||
def test_template(self):
|
||||
"""Test template."""
|
||||
assert bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template':
|
||||
"It {{ states.sensor.test_state.state }}."
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template':
|
||||
"It {{ states.sensor.test_state.state }}."
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
state = self.hass.states.get('sensor.test_template_sensor')
|
||||
assert state.state == 'It .'
|
||||
@@ -39,83 +43,89 @@ class TestTemplateSensor:
|
||||
|
||||
def test_template_syntax_error(self):
|
||||
"""Test templating syntax error."""
|
||||
assert not bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template':
|
||||
"{% if rubbish %}"
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template':
|
||||
"{% if rubbish %}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_template_attribute_missing(self):
|
||||
"""Test missing attribute template."""
|
||||
assert bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template':
|
||||
"It {{ states.sensor.test_state.attributes.missing }}."
|
||||
with assert_setup_component(1):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'value_template': 'It {{ states.sensor.test_state'
|
||||
'.attributes.missing }}.'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
state = self.hass.states.get('sensor.test_template_sensor')
|
||||
assert state.state == 'unknown'
|
||||
|
||||
def test_invalid_name_does_not_create(self):
|
||||
"""Test invalid name."""
|
||||
assert not bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test INVALID sensor': {
|
||||
'value_template':
|
||||
"{{ states.sensor.test_state.state }}"
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test INVALID sensor': {
|
||||
'value_template':
|
||||
"{{ states.sensor.test_state.state }}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_invalid_sensor_does_not_create(self):
|
||||
"""Test invalid sensor."""
|
||||
assert not bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': 'invalid'
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': 'invalid'
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_no_sensors_does_not_create(self):
|
||||
"""Test no sensors."""
|
||||
assert not bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template'
|
||||
}
|
||||
})
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template'
|
||||
}
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_missing_template_does_not_create(self):
|
||||
"""Test missing template."""
|
||||
assert not bootstrap.setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'not_value_template':
|
||||
"{{ states.sensor.test_state.state }}"
|
||||
with assert_setup_component(0):
|
||||
assert setup_component(self.hass, 'sensor', {
|
||||
'sensor': {
|
||||
'platform': 'template',
|
||||
'sensors': {
|
||||
'test_template_sensor': {
|
||||
'not_value_template':
|
||||
"{{ states.sensor.test_state.state }}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
assert self.hass.states.all() == []
|
||||
Reference in new issue
Block a user