Continue on invalid platforms and new setup_component unit tests (#3736)

This commit is contained in:
Johann Kellerman 2016-10-08 20:27:35 +02:00 committed by Paulus Schoutsen
parent 1b26b5ad14
commit 7b40a641ec
23 changed files with 842 additions and 672 deletions

View file

@ -4,10 +4,11 @@ from unittest.mock import MagicMock
import requests_mock
from homeassistant import core as ha
from homeassistant.bootstrap import setup_component
from homeassistant.components.binary_sensor import sleepiq
from tests.components.test_sleepiq import mock_responses
from tests.common import get_test_home_assistant
class TestSleepIQBinarySensorSetup(unittest.TestCase):
@ -22,7 +23,7 @@ class TestSleepIQBinarySensorSetup(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 = {
@ -35,6 +36,9 @@ class TestSleepIQBinarySensorSetup(unittest.TestCase):
"""Test for successfully setting up the SleepIQ platform."""
mock_responses(mock)
setup_component(self.hass, 'sleepiq', {
'sleepiq': self.config})
sleepiq.setup_platform(self.hass,
self.config,
self.add_devices,

View file

@ -9,12 +9,15 @@ from homeassistant.components.binary_sensor import template
from homeassistant.exceptions import TemplateError
from homeassistant.helpers import template as template_hlpr
from tests.common import get_test_home_assistant
from tests.common import get_test_home_assistant, assert_setup_component
class TestBinarySensorTemplate(unittest.TestCase):
"""Test for Binary sensor template platform."""
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()
@ -47,53 +50,53 @@ class TestBinarySensorTemplate(unittest.TestCase):
def test_setup_no_sensors(self):
""""Test setup with no sensors."""
result = bootstrap.setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template'
}
})
self.assertFalse(result)
with assert_setup_component(0):
assert bootstrap.setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template'
}
})
def test_setup_invalid_device(self):
""""Test the setup with invalid devices."""
result = bootstrap.setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'foo bar': {},
},
}
})
self.assertFalse(result)
with assert_setup_component(0):
assert bootstrap.setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'foo bar': {},
},
}
})
def test_setup_invalid_sensor_class(self):
""""Test setup with invalid sensor class."""
result = bootstrap.setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test': {
'value_template': '{{ foo }}',
'sensor_class': 'foobarnotreal',
with assert_setup_component(0):
assert bootstrap.setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test': {
'value_template': '{{ foo }}',
'sensor_class': 'foobarnotreal',
},
},
},
}
})
self.assertFalse(result)
}
})
def test_setup_invalid_missing_template(self):
""""Test setup with invalid and missing template."""
result = bootstrap.setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test': {
'sensor_class': 'motion',
},
with assert_setup_component(0):
assert bootstrap.setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test': {
'sensor_class': 'motion',
},
}
}
}
})
self.assertFalse(result)
})
def test_attributes(self):
""""Test the attributes."""
@ -107,7 +110,9 @@ class TestBinarySensorTemplate(unittest.TestCase):
vs.update()
self.assertFalse(vs.is_on)
# pylint: disable=protected-access
vs._template = template_hlpr.Template("{{ 2 > 1 }}", self.hass)
vs.update()
self.assertTrue(vs.is_on)

View file

@ -1,12 +1,14 @@
"""The test for the Trend sensor platform."""
import homeassistant.bootstrap as bootstrap
from tests.common import get_test_home_assistant
from tests.common import get_test_home_assistant, assert_setup_component
class TestTrendBinarySensor:
"""Test the Trend sensor."""
hass = None
def setup_method(self, method):
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
@ -189,41 +191,46 @@ class TestTrendBinarySensor:
state = self.hass.states.get('binary_sensor.test_trend_sensor')
assert state.state == 'off'
def test_invalid_name_does_not_create(self):
def test_invalid_name_does_not_create(self): \
# pylint: disable=invalid-name
"""Test invalid name."""
assert not bootstrap.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test INVALID sensor': {
'entity_id':
"sensor.test_state"
with assert_setup_component(0):
assert bootstrap.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test INVALID sensor': {
'entity_id':
"sensor.test_state"
}
}
}
}
})
})
assert self.hass.states.all() == []
def test_invalid_sensor_does_not_create(self):
def test_invalid_sensor_does_not_create(self): \
# pylint: disable=invalid-name
"""Test invalid sensor."""
assert not bootstrap.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test_trend_sensor': {
'not_entity_id':
"sensor.test_state"
with assert_setup_component(0):
assert bootstrap.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'template',
'sensors': {
'test_trend_sensor': {
'not_entity_id':
"sensor.test_state"
}
}
}
}
})
})
assert self.hass.states.all() == []
def test_no_sensors_does_not_create(self):
"""Test no sensors."""
assert not bootstrap.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend'
}
})
with assert_setup_component(0):
assert bootstrap.setup_component(self.hass, 'binary_sensor', {
'binary_sensor': {
'platform': 'trend'
}
})
assert self.hass.states.all() == []