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')
python类Schema()的实例源码
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')
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)
def test_ordered_dict_key_validator():
"""Test ordered_dict key validator."""
schema = vol.Schema(cv.ordered_dict(cv.match_all, cv.string))
with pytest.raises(vol.Invalid):
schema({None: 1})
schema({'hello': 'world'})
schema = vol.Schema(cv.ordered_dict(cv.match_all, int))
with pytest.raises(vol.Invalid):
schema({'hello': 1})
schema({1: 'works'})
def test_ordered_dict_value_validator():
"""Test ordered_dict validator."""
schema = vol.Schema(cv.ordered_dict(cv.string))
with pytest.raises(vol.Invalid):
schema({'hello': None})
schema({'hello': 'world'})
schema = vol.Schema(cv.ordered_dict(int))
with pytest.raises(vol.Invalid):
schema({'hello': 'world'})
schema({'hello': 5})
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')
TestEnum['value1']
def url(value: Any) -> str:
"""Validate an URL."""
url_in = str(value)
if urlparse(url_in).scheme in ['http', 'https']:
return vol.Schema(vol.Url())(url_in)
raise vol.Invalid('invalid url')
def _state_variable_create_schema(self, type_info):
# construct validators
validators = []
data_type = type_info['data_type_python']
validators.append(data_type)
if 'allowed_values' in type_info:
allowed_values = type_info['allowed_values']
in_ = vol.In(allowed_values) # coerce allowed values? assume always string for now
validators.append(in_)
if 'allowed_value_range' in type_info:
min_ = type_info['allowed_value_range'].get('min', None)
max_ = type_info['allowed_value_range'].get('max', None)
min_ = data_type(min_)
max_ = data_type(max_)
range_ = vol.Range(min=min_, max=max_)
validators.append(range_)
# construct key
key = vol.Required('value')
if 'default_value' in type_info:
default_value = type_info['default_value']
if data_type == bool:
default_value = default_value == '1'
else:
default_value = data_type(default_value)
key.default = default_value
return vol.Schema({key: vol.All(*validators)})
def test_can_set_default_manifest_file(self):
with tempfile.TemporaryDirectory() as d:
with (Path(d) / 'default.pp').open('w') as f:
f.write('dummy pp file')
config = {'manifests_path': d}
try:
final_config = Schema(PuppetProvisioner.schema)(config)
except Invalid as e:
pytest.fail("schema validation didn't pass: {}".format(e))
assert final_config == {
'manifest_file': 'default.pp',
'manifests_path': d}
def test_can_set_default_environment(self):
with tempfile.TemporaryDirectory() as d:
os.mkdir(str(Path(d) / 'production'))
config = {'environment_path': d}
try:
final_config = Schema(PuppetProvisioner.schema)(config)
except Invalid as e:
pytest.fail("schema validation didn't pass: {}".format(e))
assert final_config == {
'environment': 'production',
'environment_path': d}
def test_can_set_default_mode_and_manifest_file(self, mock_isfile, mock_isdir):
config = {}
try:
final_config = Schema(PuppetProvisioner.schema)(config)
except Invalid as e:
pytest.fail("schema validation didn't pass: {}".format(e))
assert final_config == {
'manifests_path': 'manifests',
'manifest_file': 'default.pp'}
def action_model(value):
"""Validates the data against action_model schema."""
action_model_schema = voluptuous.Schema({
"action": voluptuous.And(basestring, lambda o: not o.startswith("_"))
}, required=True)
return action_model_schema(value)
def banana_model(value):
"""Validates the data against the banana_model schema."""
banana_model_schema = voluptuous.Schema({
"content": basestring
}, required=True)
return banana_model_schema(value)
def validate_config(_config):
lingam_schema = voluptuous.Schema({
"module": voluptuous.And(basestring, vu.NoSpaceCharacter()),
"threshold": float
}, required=True)
return lingam_schema(_config)
def validate_config(_config):
svm_schema = voluptuous.Schema({
"module": voluptuous.And(basestring, vu.NoSpaceCharacter()),
"nb_samples": voluptuous.Or(float, int)
}, required=True)
return svm_schema(_config)
def validate_config(_config):
log_reg_schema = voluptuous.Schema({
'module': voluptuous.And(
basestring, NoSpaceCharacter()),
'nb_samples': voluptuous.Or(float, int)
}, required=True)
return log_reg_schema(_config)
def validate_config(_config):
svc_schema = voluptuous.Schema({
'module': voluptuous.And(
basestring, NoSpaceCharacter()),
'nb_samples': voluptuous.Or(float, int)
}, required=True)
return svc_schema(_config)
def validate_config(_config):
elliptic_schema = voluptuous.Schema({
'module': voluptuous.And(
basestring, NoSpaceCharacter()),
'nb_samples': voluptuous.Or(float, int)
}, required=True)
return elliptic_schema(_config)
def validate_config(_config):
decisiontree_schema = voluptuous.Schema({
'module': voluptuous.And(
basestring, NoSpaceCharacter()),
'nb_samples': voluptuous.Or(float, int)
}, required=True)
return decisiontree_schema(_config)
def validate_config(_config):
randomforest_schema = voluptuous.Schema({
'module': voluptuous.And(
basestring, NoSpaceCharacter()),
'nb_samples': voluptuous.Or(float, int)
}, required=True)
return randomforest_schema(_config)