def test_custom_exception(self, mocker): stub = mocker.stub(name='custom_on_error') string_field = StringField(choices=['foo'], on_error=stub) with pytest.raises(ConfigError): string_field.validate('bar', 'foo') stub.assert_called_once_with('bar', 'foo', ANY)
def test_choices(self): string_field = StringField(choices=['foo', 'bar']) with pytest.raises(ConfigError): string_field.validate('baz') string_field.validate('bar')
def test_expects_string(self): string_field = StringField() with pytest.raises(ConfigError): string_field.validate(b"foo") with pytest.raises(ConfigError): string_field.validate({}) with pytest.raises(ConfigError): string_field.validate(42) string_field.validate("foo")
def test_regex(self): string_field = StringField(regex=r'foo\d*') string_field.validate('foo') string_field.validate('foo42') with pytest.raises(ConfigError): string_field.validate('baz')
def test_case_sensitive(self): string_field = StringField(choices=['foo', 'bar'], case_sensitive=False) string_field.validate('foo') string_field.validate('FOO') string_field = StringField(choices=['foo', 'bar'], case_sensitive=True) string_field.validate('foo') with pytest.raises(ConfigError): string_field.validate('FOO')
def test_custom_validator(self, mocker): stub = mocker.stub(name='custom_validator') string_field = StringField(choices=['foo'], additional_validator=stub) string_field.validate('foo', 'baz') stub.assert_called_once_with('foo', 'baz')