def test_validate_validator_invalid(self): def inner(cfg, value): raise KeyError() field = Field(key='key', validator=inner) with pytest.raises(KeyError): field.validate(self.cfg, 'hello')
def test_setdefault_env_exists_valid(self, mock_environ_get): env = mock_environ_get.return_value = object() retval = object() cfg = Config(schema=Schema()) field = Field(env='ASDF', key='field') field.validate = MagicMock(return_value=retval) field.__setdefault__(cfg) field.validate.assert_called_once_with(cfg, env) assert cfg._data == {'field': retval}
def test_setdefault_env_exists_invalid(self, mock_environ_get): env = mock_environ_get.return_value = object() retval = object() cfg = Config(schema=Schema()) field = Field(env='ASDF', key='field') field.validate = MagicMock(side_effect=ValueError()) field._default = retval with pytest.raises(ValidationError): field.__setdefault__(cfg) field.validate.assert_called_once_with(cfg, env)
def test_setdefault_env_exists_invalid_validationerror(self, mock_environ_get): env = mock_environ_get.return_value = object() retval = object() cfg = Config(schema=Schema()) field = Field(env='ASDF', key='field') err = ValidationError(cfg, field, ValueError('asdf')) field.validate = MagicMock(side_effect=err) field._default = retval with pytest.raises(ValidationError) as exc: field.__setdefault__(cfg) assert exc.value is err
def test_validate_validator_valid(self): field = Field(key='key', validator=lambda cfg, value: 'HELLO') assert field.validate(self.cfg, 'asdf') == 'HELLO'
def test_validate_value(self): field = Field(key='key') x = 'hello' assert field.validate(self.cfg, x) is x
def test_not_required(self): field = Field(key='key') assert field.validate(self.cfg, None) is None
def test_required(self): field = Field(required=True, key='key') with pytest.raises(ValueError): field.validate(self.cfg, None)