2016-09-12 05:25:01 +00:00
|
|
|
"""Test config validators."""
|
2016-11-25 05:52:10 +00:00
|
|
|
from datetime import timedelta, datetime, date
|
2016-09-28 04:29:55 +00:00
|
|
|
import enum
|
2016-09-01 13:35:00 +00:00
|
|
|
import os
|
2016-10-22 09:05:00 +00:00
|
|
|
from socket import _GLOBAL_DEFAULT_TIMEOUT
|
2017-01-05 19:33:22 +00:00
|
|
|
from unittest.mock import Mock, patch
|
2016-04-04 19:18:58 +00:00
|
|
|
|
2016-03-28 01:48:51 +00:00
|
|
|
import pytest
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
2016-03-28 01:48:51 +00:00
|
|
|
|
2016-04-04 04:38:58 +00:00
|
|
|
def test_boolean():
|
|
|
|
"""Test boolean validation."""
|
|
|
|
schema = vol.Schema(cv.boolean)
|
|
|
|
|
|
|
|
for value in ('T', 'negative', 'lock'):
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
for value in ('true', 'On', '1', 'YES', 'enable', 1, True):
|
|
|
|
assert schema(value)
|
|
|
|
|
|
|
|
for value in ('false', 'Off', '0', 'NO', 'disable', 0, False):
|
|
|
|
assert not schema(value)
|
|
|
|
|
|
|
|
|
2016-03-28 01:48:51 +00:00
|
|
|
def test_latitude():
|
|
|
|
"""Test latitude validation."""
|
|
|
|
schema = vol.Schema(cv.latitude)
|
|
|
|
|
|
|
|
for value in ('invalid', None, -91, 91, '-91', '91', '123.01A'):
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
for value in ('-89', 89, '12.34'):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
|
|
|
|
def test_longitude():
|
|
|
|
"""Test longitude validation."""
|
|
|
|
schema = vol.Schema(cv.longitude)
|
|
|
|
|
|
|
|
for value in ('invalid', None, -181, 181, '-181', '181', '123.01A'):
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
for value in ('-179', 179, '12.34'):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
|
2016-08-17 03:55:29 +00:00
|
|
|
def test_port():
|
2016-08-19 11:41:01 +00:00
|
|
|
"""Test TCP/UDP network port."""
|
2016-08-17 03:55:29 +00:00
|
|
|
schema = vol.Schema(cv.port)
|
|
|
|
|
2016-08-19 11:41:01 +00:00
|
|
|
for value in ('invalid', None, -1, 0, 80000, '81000'):
|
2016-08-17 03:55:29 +00:00
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
for value in ('1000', 21, 24574):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
|
2016-09-01 13:35:00 +00:00
|
|
|
def test_isfile():
|
|
|
|
"""Validate that the value is an existing file."""
|
|
|
|
schema = vol.Schema(cv.isfile)
|
|
|
|
|
2016-10-18 03:16:36 +00:00
|
|
|
fake_file = 'this-file-does-not.exist'
|
|
|
|
assert not os.path.isfile(fake_file)
|
2016-09-01 13:35:00 +00:00
|
|
|
|
2016-10-18 03:16:36 +00:00
|
|
|
for value in ('invalid', None, -1, 0, 80000, fake_file):
|
2016-09-01 13:35:00 +00:00
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
schema(value)
|
|
|
|
|
2016-10-18 03:16:36 +00:00
|
|
|
# patching methods that allow us to fake a file existing
|
|
|
|
# with write access
|
|
|
|
with patch('os.path.isfile', Mock(return_value=True)), \
|
|
|
|
patch('os.access', Mock(return_value=True)):
|
|
|
|
schema('test.txt')
|
2016-09-01 13:35:00 +00:00
|
|
|
|
|
|
|
|
2016-08-19 11:41:01 +00:00
|
|
|
def test_url():
|
|
|
|
"""Test URL."""
|
|
|
|
schema = vol.Schema(cv.url)
|
|
|
|
|
|
|
|
for value in ('invalid', None, 100, 'htp://ha.io', 'http//ha.io',
|
|
|
|
'http://??,**', 'https://??,**'):
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
for value in ('http://localhost', 'https://localhost/test/index.html',
|
|
|
|
'http://home-assistant.io', 'http://home-assistant.io/test/',
|
|
|
|
'https://community.home-assistant.io/'):
|
|
|
|
assert schema(value)
|
|
|
|
|
|
|
|
|
2016-03-28 01:48:51 +00:00
|
|
|
def test_platform_config():
|
|
|
|
"""Test platform config validation."""
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-03-28 01:48:51 +00:00
|
|
|
{},
|
|
|
|
{'hello': 'world'},
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-03-28 01:48:51 +00:00
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
cv.PLATFORM_SCHEMA(value)
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-03-28 01:48:51 +00:00
|
|
|
{'platform': 'mqtt'},
|
|
|
|
{'platform': 'mqtt', 'beer': 'yes'},
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-03-28 01:48:51 +00:00
|
|
|
cv.PLATFORM_SCHEMA(value)
|
|
|
|
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
def test_ensure_list():
|
|
|
|
"""Test ensure_list."""
|
|
|
|
schema = vol.Schema(cv.ensure_list)
|
|
|
|
assert [] == schema(None)
|
|
|
|
assert [1] == schema(1)
|
|
|
|
assert [1] == schema([1])
|
|
|
|
assert ['1'] == schema('1')
|
|
|
|
assert ['1'] == schema(['1'])
|
|
|
|
assert [{'1': '2'}] == schema({'1': '2'})
|
|
|
|
|
|
|
|
|
2016-03-28 01:48:51 +00:00
|
|
|
def test_entity_id():
|
|
|
|
"""Test entity ID validation."""
|
|
|
|
schema = vol.Schema(cv.entity_id)
|
|
|
|
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema('invalid_entity')
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
assert schema('sensor.LIGHT') == 'sensor.light'
|
2016-03-28 01:48:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_entity_ids():
|
|
|
|
"""Test entity ID validation."""
|
|
|
|
schema = vol.Schema(cv.entity_ids)
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-03-28 01:48:51 +00:00
|
|
|
'invalid_entity',
|
|
|
|
'sensor.light,sensor_invalid',
|
|
|
|
['invalid_entity'],
|
|
|
|
['sensor.light', 'sensor_invalid'],
|
|
|
|
['sensor.light,sensor_invalid'],
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-03-28 01:48:51 +00:00
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-03-28 01:48:51 +00:00
|
|
|
[],
|
|
|
|
['sensor.light'],
|
|
|
|
'sensor.light'
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-03-28 01:48:51 +00:00
|
|
|
schema(value)
|
|
|
|
|
2016-04-10 22:20:20 +00:00
|
|
|
assert schema('sensor.LIGHT, light.kitchen ') == [
|
2016-03-28 01:48:51 +00:00
|
|
|
'sensor.light', 'light.kitchen'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2017-01-22 19:19:50 +00:00
|
|
|
def test_ensure_list_csv():
|
|
|
|
"""Test ensure_list_csv."""
|
|
|
|
schema = vol.Schema(cv.ensure_list_csv)
|
|
|
|
|
|
|
|
options = (
|
|
|
|
None,
|
|
|
|
12,
|
|
|
|
[],
|
|
|
|
['string'],
|
|
|
|
'string1,string2'
|
|
|
|
)
|
|
|
|
for value in options:
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
assert schema('string1, string2 ') == [
|
|
|
|
'string1', 'string2'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
def test_event_schema():
|
|
|
|
"""Test event_schema validation."""
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-04-03 17:19:09 +00:00
|
|
|
{}, None,
|
|
|
|
{
|
|
|
|
'event_data': {},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'event': 'state_changed',
|
|
|
|
'event_data': 1,
|
|
|
|
},
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-04-03 17:19:09 +00:00
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
cv.EVENT_SCHEMA(value)
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-04-03 17:19:09 +00:00
|
|
|
{'event': 'state_changed'},
|
|
|
|
{'event': 'state_changed', 'event_data': {'hello': 'world'}},
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-04-03 17:19:09 +00:00
|
|
|
cv.EVENT_SCHEMA(value)
|
|
|
|
|
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
def test_platform_validator():
|
|
|
|
"""Test platform validation."""
|
2017-01-07 00:47:25 +00:00
|
|
|
hass = None
|
2016-04-04 19:18:58 +00:00
|
|
|
|
2017-01-07 00:47:25 +00:00
|
|
|
try:
|
|
|
|
hass = get_test_home_assistant()
|
2016-04-04 19:18:58 +00:00
|
|
|
|
2017-01-07 00:47:25 +00:00
|
|
|
schema = vol.Schema(cv.platform_validator('light'))
|
|
|
|
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema('platform_that_does_not_exist')
|
2016-04-04 19:18:58 +00:00
|
|
|
|
2017-01-07 00:47:25 +00:00
|
|
|
schema('hue')
|
|
|
|
finally:
|
|
|
|
if hass is not None:
|
|
|
|
hass.stop()
|
2016-04-04 19:18:58 +00:00
|
|
|
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
2016-04-21 22:52:20 +00:00
|
|
|
def test_time_period():
|
|
|
|
"""Test time_period validation."""
|
|
|
|
schema = vol.Schema(cv.time_period)
|
2016-04-04 19:18:58 +00:00
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-10-08 01:08:33 +00:00
|
|
|
None, '', 'hello:world', '12:', '12:34:56:78',
|
2016-04-21 22:52:20 +00:00
|
|
|
{}, {'wrong_key': -10}
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-10-08 01:08:33 +00:00
|
|
|
'8:20', '23:59', '-8:20', '-23:59:59', '-48:00', {'minutes': 5}, 1, '5'
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-04-04 19:18:58 +00:00
|
|
|
schema(value)
|
|
|
|
|
2016-10-08 01:08:33 +00:00
|
|
|
assert timedelta(seconds=180) == schema('180')
|
2016-04-04 19:18:58 +00:00
|
|
|
assert timedelta(hours=23, minutes=59) == schema('23:59')
|
|
|
|
assert -1 * timedelta(hours=1, minutes=15) == schema('-1:15')
|
|
|
|
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
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."""
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-04-03 17:19:09 +00:00
|
|
|
{}, 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'
|
|
|
|
}
|
|
|
|
},
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-04-03 17:19:09 +00:00
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
cv.SERVICE_SCHEMA(value)
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-04-03 17:19:09 +00:00
|
|
|
{'service': 'homeassistant.turn_on'},
|
2016-04-04 19:18:58 +00:00
|
|
|
{
|
|
|
|
'service': 'homeassistant.turn_on',
|
|
|
|
'entity_id': 'light.kitchen',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
'service': 'homeassistant.turn_on',
|
|
|
|
'entity_id': ['light.kitchen', 'light.ceiling'],
|
|
|
|
},
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-04-03 17:19:09 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2016-03-28 01:48:51 +00:00
|
|
|
def test_temperature_unit():
|
|
|
|
"""Test temperature unit validation."""
|
|
|
|
schema = vol.Schema(cv.temperature_unit)
|
|
|
|
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema('K')
|
|
|
|
|
|
|
|
schema('C')
|
|
|
|
schema('F')
|
|
|
|
|
|
|
|
|
2016-10-25 04:49:49 +00:00
|
|
|
def test_x10_address():
|
|
|
|
"""Test x10 addr validator."""
|
|
|
|
schema = vol.Schema(cv.x10_address)
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
schema('Q1')
|
|
|
|
schema('q55')
|
|
|
|
schema('garbage_addr')
|
|
|
|
|
|
|
|
schema('a1')
|
|
|
|
schema('C11')
|
|
|
|
|
|
|
|
|
2016-04-03 17:19:09 +00:00
|
|
|
def test_template():
|
|
|
|
"""Test template validator."""
|
|
|
|
schema = vol.Schema(cv.template)
|
|
|
|
|
2016-09-08 16:19:47 +00:00
|
|
|
for value in (None, '{{ partial_print }', '{% if True %}Hello', ['test']):
|
2016-09-28 04:29:55 +00:00
|
|
|
with pytest.raises(vol.Invalid,
|
|
|
|
message='{} not considered invalid'.format(value)):
|
2016-09-08 16:19:47 +00:00
|
|
|
schema(value)
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-09-08 16:19:47 +00:00
|
|
|
1, 'Hello',
|
|
|
|
'{{ beer }}',
|
|
|
|
'{% if 1 == 1 %}Hello{% else %}World{% endif %}',
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-09-08 16:19:47 +00:00
|
|
|
schema(value)
|
|
|
|
|
|
|
|
|
|
|
|
def test_template_complex():
|
|
|
|
"""Test template_complex validator."""
|
|
|
|
schema = vol.Schema(cv.template_complex)
|
|
|
|
|
|
|
|
for value in (None, '{{ partial_print }', '{% if True %}Hello'):
|
2016-04-03 17:19:09 +00:00
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-04-03 17:19:09 +00:00
|
|
|
1, 'Hello',
|
|
|
|
'{{ beer }}',
|
|
|
|
'{% if 1 == 1 %}Hello{% else %}World{% endif %}',
|
2016-11-19 05:47:59 +00:00
|
|
|
{'test': 1, 'test2': '{{ beer }}'},
|
2016-09-08 16:19:47 +00:00
|
|
|
['{{ beer }}', 1]
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-04-03 17:19:09 +00:00
|
|
|
schema(value)
|
|
|
|
|
|
|
|
|
2016-03-28 01:48:51 +00:00
|
|
|
def test_time_zone():
|
|
|
|
"""Test time zone validation."""
|
|
|
|
schema = vol.Schema(cv.time_zone)
|
|
|
|
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema('America/Do_Not_Exist')
|
|
|
|
|
|
|
|
schema('America/Los_Angeles')
|
|
|
|
schema('UTC')
|
2016-04-03 17:19:09 +00:00
|
|
|
|
|
|
|
|
2017-09-28 21:57:49 +00:00
|
|
|
def test_date():
|
|
|
|
"""Test date validation."""
|
|
|
|
schema = vol.Schema(cv.date)
|
|
|
|
|
|
|
|
for value in ['Not a date', '23:42', '2016-11-23T18:59:08']:
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
schema(datetime.now().date())
|
|
|
|
schema('2016-11-23')
|
|
|
|
|
|
|
|
|
|
|
|
def test_time():
|
|
|
|
"""Test date validation."""
|
|
|
|
schema = vol.Schema(cv.time)
|
|
|
|
|
|
|
|
for value in ['Not a time', '2016-11-23', '2016-11-23T18:59:08']:
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
schema(datetime.now().time())
|
|
|
|
schema('23:42:00')
|
|
|
|
schema('23:42')
|
|
|
|
|
|
|
|
|
2016-11-25 05:52:10 +00:00
|
|
|
def test_datetime():
|
|
|
|
"""Test date time validation."""
|
|
|
|
schema = vol.Schema(cv.datetime)
|
|
|
|
for value in [date.today(), 'Wrong DateTime', '2016-11-23']:
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
schema(datetime.now())
|
|
|
|
schema('2016-11-23T18:59:08')
|
|
|
|
|
|
|
|
|
2016-04-04 19:18:58 +00:00
|
|
|
def test_key_dependency():
|
|
|
|
"""Test key_dependency validator."""
|
|
|
|
schema = vol.Schema(cv.key_dependency('beer', 'soda'))
|
2016-04-03 17:19:09 +00:00
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-04-04 19:18:58 +00:00
|
|
|
{'beer': None}
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-04-03 17:19:09 +00:00
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
options = (
|
2016-04-04 19:18:58 +00:00
|
|
|
{'beer': None, 'soda': None},
|
|
|
|
{'soda': None}, {}
|
2017-01-05 19:33:22 +00:00
|
|
|
)
|
|
|
|
for value in options:
|
2016-04-03 17:19:09 +00:00
|
|
|
schema(value)
|
|
|
|
|
|
|
|
|
|
|
|
def test_has_at_least_one_key():
|
|
|
|
"""Test has_at_least_one_key validator."""
|
2016-04-04 19:18:58 +00:00
|
|
|
schema = vol.Schema(cv.has_at_least_one_key('beer', 'soda'))
|
2016-04-03 17:19:09 +00:00
|
|
|
|
|
|
|
for value in (None, [], {}, {'wine': None}):
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
for value in ({'beer': None}, {'soda': None}):
|
|
|
|
schema(value)
|
2017-01-22 19:19:50 +00:00
|
|
|
|
|
|
|
|
2017-09-28 21:57:49 +00:00
|
|
|
def test_has_at_least_one_key_value():
|
|
|
|
"""Test has_at_least_one_key_value validator."""
|
|
|
|
schema = vol.Schema(cv.has_at_least_one_key_value(('drink', 'beer'),
|
|
|
|
('drink', 'soda'),
|
|
|
|
('food', 'maultaschen')))
|
|
|
|
|
|
|
|
for value in (None, [], {}, {'wine': None}, {'drink': 'water'}):
|
|
|
|
with pytest.raises(vol.MultipleInvalid):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
for value in ({'drink': 'beer'}, {'food': 'maultaschen'},
|
|
|
|
{'drink': 'soda', 'food': 'maultaschen'}):
|
|
|
|
schema(value)
|
|
|
|
|
|
|
|
|
2016-09-28 04:29:55 +00:00
|
|
|
def test_enum():
|
|
|
|
"""Test enum validator."""
|
|
|
|
class TestEnum(enum.Enum):
|
|
|
|
"""Test enum."""
|
|
|
|
|
|
|
|
value1 = "Value 1"
|
|
|
|
value2 = "Value 2"
|
|
|
|
|
|
|
|
schema = vol.Schema(cv.enum(TestEnum))
|
|
|
|
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
schema('value3')
|
|
|
|
|
2016-10-22 09:05:00 +00:00
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
def test_socket_timeout(): # pylint: disable=invalid-name
|
2016-10-22 09:05:00 +00:00
|
|
|
"""Test socket timeout validator."""
|
2017-01-05 19:33:22 +00:00
|
|
|
TEST_CONF_TIMEOUT = 'timeout' # pylint: disable=invalid-name
|
2016-10-22 09:05:00 +00:00
|
|
|
|
|
|
|
schema = vol.Schema(
|
|
|
|
{vol.Required(TEST_CONF_TIMEOUT, default=None): cv.socket_timeout})
|
|
|
|
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
schema({TEST_CONF_TIMEOUT: 0.0})
|
|
|
|
|
|
|
|
with pytest.raises(vol.Invalid):
|
|
|
|
schema({TEST_CONF_TIMEOUT: -1})
|
|
|
|
|
|
|
|
assert _GLOBAL_DEFAULT_TIMEOUT == schema({TEST_CONF_TIMEOUT:
|
|
|
|
None})[TEST_CONF_TIMEOUT]
|
|
|
|
|
2017-01-05 19:33:22 +00:00
|
|
|
assert schema({TEST_CONF_TIMEOUT: 1})[TEST_CONF_TIMEOUT] == 1.0
|