def test_matching_range(self): with not_raises(ValidationError): Length(min=1, max=5)([1]) with not_raises(ValidationError): Length(min=1, max=5)([1, 2, 3]) with not_raises(ValidationError): Length(min=1, max=5)([1, 2, 3, 4, 5])
def test_customzing_min_error_message(self): message = 'Length {length} of {data} should be at least {min}' with raises(ValidationError) as exc_info: Length(min=1, error_messages={'min': message})([]) assert exc_info.value.messages == message.format(data=[], length=0, min=1)
def test_customizing_all_error_messages_at_once(self): message = 'Value is invalid' with raises(ValidationError) as exc_info: Length(exact=1, error=message)([]) assert exc_info.value.messages == message with raises(ValidationError) as exc_info: Length(min=1, error=message)([]) assert exc_info.value.messages == message with raises(ValidationError) as exc_info: Length(max=1, error=message)([1, 2]) == message assert exc_info.value.messages == message with raises(ValidationError) as exc_info: Length(min=1, max=5, error=message)([]) == message assert exc_info.value.messages == message
def test_customzing_range_error_message(self): message = 'Length {length} of {data} should be between {min} and {max}' with raises(ValidationError) as exc_info: Length(min=1, max=5, error_messages={'range': message})([]) assert exc_info.value.messages == \ message.format(data=[], length=0, min=1, max=5)
def test_raising_ValidationError_when_matching_range_and_given_value_is_greater( self): with raises(ValidationError) as exc_info: Length(min=1, max=5)([1, 2, 3, 4, 5, 6]) assert exc_info.value.messages == \ Length.default_error_messages['range'].format(min=1, max=5)
def test_customzing_max_error_message(self): message = 'Length {length} of {data} should be at most {max}' with raises(ValidationError) as exc_info: Length(max=1, error_messages={'max': message})([1, 2]) assert exc_info.value.messages == \ message.format(data=[1, 2], length=2, max=1)
def test_raising_ValidationError_when_matching_max_value_and_given_value_is_greater( self): with raises(ValidationError) as exc_info: Length(max=1)([1, 2]) assert exc_info.value.messages == \ Length.default_error_messages['max'].format(max=1)
def test_matching_max_value(self): with not_raises(ValidationError): Length(max=1)([1]) with not_raises(ValidationError): Length(max=1)([])
def test_raising_ValidationError_when_matching_min_value_and_given_value_is_less( self): with raises(ValidationError) as exc_info: Length(min=1)([]) assert exc_info.value.messages == \ Length.default_error_messages['min'].format(min=1)
def test_matching_min_value(self): with not_raises(ValidationError): Length(min=1)([1]) with not_raises(ValidationError): Length(min=1)([1, 2])
def test_customizing_exact_error_message(self): message = 'Value {data} length is not {exact}' with raises(ValidationError) as exc_info: Length(exact=3, error_messages={'exact': message})([1, 2]) assert exc_info.value.messages == message.format(data=[1, 2], exact=3)
def test_raising_ValidationError_when_matching_exact_value_and_given_value_does_not_match( self): with raises(ValidationError) as exc_info: Length(exact=3)([1, 2]) assert exc_info.value.messages == \ Length.default_error_messages['exact'].format(exact=3)
def test_matching_exact_value(self): with not_raises(ValidationError): Length(exact=2)([1, 2])