Add tests for bootstrap config validation

This commit is contained in:
Paulus Schoutsen 2016-03-29 00:17:53 -07:00
parent a35173a5ff
commit 25269cdb6b
3 changed files with 104 additions and 2 deletions

View file

@ -40,7 +40,8 @@ def config_per_platform(config, domain):
platform_config = [platform_config]
for item in platform_config:
yield item.get(CONF_PLATFORM), item
platform = None if item is None else item.get(CONF_PLATFORM)
yield platform, item
def extract_domain_configs(config, domain):

View file

@ -143,10 +143,18 @@ class MockHTTP(object):
class MockModule(object):
"""Representation of a fake module."""
def __init__(self, domain=None, dependencies=[], setup=None):
def __init__(self, domain=None, dependencies=[], setup=None,
config_schema=None, platform_schema=None):
"""Initialize the mock module."""
self.DOMAIN = domain
self.DEPENDENCIES = dependencies
if config_schema is not None:
self.CONFIG_SCHEMA = config_schema
if platform_schema is not None:
self.PLATFORM_SCHEMA = platform_schema
# Setup a mock setup if none given.
if setup is None:
self.setup = lambda hass, config: True

View file

@ -4,11 +4,14 @@ import os
import tempfile
import unittest
import voluptuous as vol
from homeassistant import bootstrap, loader
from homeassistant.const import (__version__, CONF_LATITUDE, CONF_LONGITUDE,
CONF_NAME, CONF_CUSTOMIZE)
import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA
from tests.common import get_test_home_assistant, MockModule
@ -121,3 +124,93 @@ class TestBootstrap(unittest.TestCase):
bootstrap.setup_component(hass, 'comp_a')
self.assertEqual(['comp_a'], hass.config.components)
hass.stop()
def test_validate_component_config(self):
"""Test validating component configuration."""
config_schema = vol.Schema({
'comp_conf': {
'hello': str
}
}, required=True)
loader.set_component(
'comp_conf', MockModule('comp_conf', config_schema=config_schema))
hass = get_test_home_assistant()
assert not bootstrap._setup_component(hass, 'comp_conf', {})
assert not bootstrap._setup_component(hass, 'comp_conf', {
'comp_conf': None
})
assert not bootstrap._setup_component(hass, 'comp_conf', {
'comp_conf': {}
})
assert not bootstrap._setup_component(hass, 'comp_conf', {
'comp_conf': {
'hello': 'world',
'invalid': 'extra',
}
})
assert bootstrap._setup_component(hass, 'comp_conf', {
'comp_conf': {
'hello': 'world',
}
})
hass.stop()
def test_validate_platform_config(self):
"""Test validating platform configuration."""
platform_schema = PLATFORM_SCHEMA.extend({
'hello': str,
}, required=True)
loader.set_component(
'platform_conf',
MockModule('platform_conf', platform_schema=platform_schema))
hass = get_test_home_assistant()
assert not bootstrap._setup_component(hass, 'platform_conf', {
'platform_conf': None
})
assert not bootstrap._setup_component(hass, 'platform_conf', {
'platform_conf': {}
})
assert not bootstrap._setup_component(hass, 'platform_conf', {
'platform_conf': {
'hello': 'world',
'invalid': 'extra',
}
})
assert not bootstrap._setup_component(hass, 'platform_conf', {
'platform_conf': {
'platform': 'whatever',
'hello': 'world',
},
'platform_conf 2': {
'invalid': True
}
})
assert bootstrap._setup_component(hass, 'platform_conf', {
'platform_conf': {
'platform': 'whatever',
'hello': 'world',
}
})
assert bootstrap._setup_component(hass, 'platform_conf', {
'platform_conf': [{
'platform': 'whatever',
'hello': 'world',
}]
})
hass.stop()