Exemple #1
0
def test_required():
    c = ConfigEntry(required=True, key_name='test')
    c.set_type_hint('test', int)
    validator = c.set_validator({}, CFG_LOWER)
    with pytest.raises(voluptuous.MultipleInvalid):
        voluptuous.Schema(validator)({})
    voluptuous.Schema(validator)({'test': 5})

    c = ConfigEntry(required=False, key_name='test')
    c.set_type_hint('test', str)
    validator = c.set_validator({}, CFG_LOWER)
    voluptuous.Schema(validator)({})
    voluptuous.Schema(validator)({'test': 'my_str'})
Exemple #2
0
def test_path_objects():

    c = ConfigEntry(required=True, default=Path('asdf'))
    c.set_type_hint('test', Path)
    validator = c.set_validator({}, DEFAULT_CONFIGURATION)

    # yaml doesnt provide path -> convert to str
    ret = voluptuous.Schema(validator)({'test': '/my/path'})
    assert ret == {'test': Path('/my/path')}
Exemple #3
0
def test_dict_validator():

    c = ConfigEntry(required=True,
                    default_factory=lambda: {'test_key': 'test_val'})
    c.set_type_hint('test', typing.Dict[str, str])
    validator = c.set_validator({}, DEFAULT_CONFIGURATION)

    ret = voluptuous.Schema(validator)({})
    assert ret == {'test': {'test_key': 'test_val'}}
Exemple #4
0
def test_list_validator():

    c = ConfigEntry(required=True, default_factory=lambda: ['test'])
    c.set_type_hint('test', typing.List[str])
    validator = c.set_validator({}, DEFAULT_CONFIGURATION)

    assert {'test': [str]} == validator

    ret = voluptuous.Schema(validator)({})
    assert ret == {'test': ['test']}
Exemple #5
0
def test_default_validator():
    c = ConfigEntry(required=True, default=5)
    c.set_type_hint('test', int)
    validator = c.set_validator({}, DEFAULT_CONFIGURATION)

    ret = voluptuous.Schema(validator)({})
    assert ret == {'test': 5}

    ret = voluptuous.Schema(validator)({'test': 7})
    assert ret == {'test': 7}

    c = ConfigEntry(required=True, default='asdf')
    c.set_type_hint('test', str)
    validator = c.set_validator({}, DEFAULT_CONFIGURATION)

    ret = voluptuous.Schema(validator)({})
    assert ret == {'test': 'asdf'}

    ret = voluptuous.Schema(validator)({'test': 'ASDF'})
    assert ret == {'test': 'ASDF'}