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'})
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')}
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'}}
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']}
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'}