Add script + extra config validators

* Add config validation and extra validators

* Address PR comments
This commit is contained in:
Paulus Schoutsen 2016-04-03 10:19:09 -07:00
parent 7ed5055fa2
commit e140e9b8ab
9 changed files with 404 additions and 88 deletions

View file

@ -28,21 +28,9 @@ def test_longitude():
schema(value)
def test_icon():
"""Test icon validation."""
schema = vol.Schema(cv.icon)
for value in (False, 'work', 'icon:work'):
with pytest.raises(vol.MultipleInvalid):
schema(value)
schema('mdi:work')
def test_platform_config():
"""Test platform config validation."""
for value in (
{'platform': 1},
{},
{'hello': 'world'},
):
@ -92,6 +80,103 @@ def test_entity_ids():
]
def test_event_schema():
"""Test event_schema validation."""
for value in (
{}, None,
{
'event_data': {},
},
{
'event': 'state_changed',
'event_data': 1,
},
):
with pytest.raises(vol.MultipleInvalid):
cv.EVENT_SCHEMA(value)
for value in (
{'event': 'state_changed'},
{'event': 'state_changed', 'event_data': {'hello': 'world'}},
):
cv.EVENT_SCHEMA(value)
def test_icon():
"""Test icon validation."""
schema = vol.Schema(cv.icon)
for value in (False, 'work', 'icon:work'):
with pytest.raises(vol.MultipleInvalid):
schema(value)
schema('mdi:work')
def test_service():
"""Test service validation."""
schema = vol.Schema(cv.service)
with pytest.raises(vol.MultipleInvalid):
schema('invalid_turn_on')
schema('homeassistant.turn_on')
def test_service_schema():
"""Test service_schema validation."""
for value in (
{}, None,
{
'service': 'homeassistant.turn_on',
'service_template': 'homeassistant.turn_on'
},
{
'data': {'entity_id': 'light.kitchen'},
},
{
'service': 'homeassistant.turn_on',
'data': None
},
{
'service': 'homeassistant.turn_on',
'data_template': {
'brightness': '{{ no_end'
}
},
):
with pytest.raises(vol.MultipleInvalid):
cv.SERVICE_SCHEMA(value)
for value in (
{'service': 'homeassistant.turn_on'},
):
cv.SERVICE_SCHEMA(value)
def test_slug():
"""Test slug validation."""
schema = vol.Schema(cv.slug)
for value in (None, 'hello world'):
with pytest.raises(vol.MultipleInvalid):
schema(value)
for value in (12345, 'hello'):
schema(value)
def test_string():
"""Test string validation."""
schema = vol.Schema(cv.string)
with pytest.raises(vol.MultipleInvalid):
schema(None)
for value in (True, 1, 'hello'):
schema(value)
def test_temperature_unit():
"""Test temperature unit validation."""
schema = vol.Schema(cv.temperature_unit)
@ -103,6 +188,24 @@ def test_temperature_unit():
schema('F')
def test_template():
"""Test template validator."""
schema = vol.Schema(cv.template)
for value in (
None, '{{ partial_print }', '{% if True %}Hello'
):
with pytest.raises(vol.MultipleInvalid):
schema(value)
for value in (
1, 'Hello',
'{{ beer }}',
'{% if 1 == 1 %}Hello{% else %}World{% endif %}',
):
schema(value)
def test_time_zone():
"""Test time zone validation."""
schema = vol.Schema(cv.time_zone)
@ -112,3 +215,37 @@ def test_time_zone():
schema('America/Los_Angeles')
schema('UTC')
def test_dict_validator():
"""Test DictValidator."""
schema = vol.Schema(cv.DictValidator(cv.entity_ids, cv.slug))
for value in (
None,
{'invalid slug': 'sensor.temp'},
{'hello world': 'invalid_entity'}
):
with pytest.raises(vol.MultipleInvalid):
schema(value)
for value in (
{},
{'hello_world': 'sensor.temp'},
):
schema(value)
assert schema({'hello_world': 'sensor.temp'}) == \
{'hello_world': ['sensor.temp']}
def test_has_at_least_one_key():
"""Test has_at_least_one_key validator."""
schema = vol.Schema(cv.has_at_least_one_key(['beer', 'soda']))
for value in (None, [], {}, {'wine': None}):
with pytest.raises(vol.MultipleInvalid):
schema(value)
for value in ({'beer': None}, {'soda': None}):
schema(value)