def test_can_return_error_results_from_validate(self): self.init_validator(regex='\d+', exception_if_invalid=False, use_match_for_conversion=False) result = self.process('invalid', ensure_valid=False) assert_true(result.contains_errors()) assert_equals(('bad_pattern', ), error_keys(result.errors)) assert_false(result.errors[0].is_critical)
def test_can_handle_non_input(self): schema = SchemaValidator(exception_if_invalid=False) schema.add('first', IntegerValidator(min=10, exception_if_invalid=True)) result = schema.process(None) assert_true(result.contains_errors()) assert_equals({'first': None}, result.value) assert_equals({'first': None}, result.initial_value) assert_equals(('empty',), error_keys(result.errors['first']))
def test_can_return_result_for_empty_input_value(self): validator = ForEach(IntegerValidator(exception_if_invalid=False), exception_if_invalid=False) result = validator.process(None) assert_true(result.contains_errors()) assert_length(1, result.errors) assert_length(1, result.global_errors) # seems to be duplicated? assert_equals(('empty', ), error_keys(result.errors))
def test_can_return_error_results_from_convert(self): self.init_validator(regex='\d+', exception_if_invalid=False, use_match_for_conversion=True) result = self.process('invalid', ensure_valid=False) assert_true(result.contains_errors()) assert_equals(('bad_pattern', ), error_keys(result.errors)) # error in convert should be treated as critical as the input for # .validate() is different assert_true(result.errors[0].is_critical)
def test_can_handle_critical_errors_from_string_conversion(self): # RegexValidator must check if its superclass (StringValidator) detected # an error during ".convert()" - otherwise we will trigger exceptions # as the re module can only work on strings. self.init_validator(regex='\d+', exception_if_invalid=False, use_match_for_conversion=True) result = self.process([], ensure_valid=False) assert_true(result.contains_errors()) assert_equals(('invalid_type', ), error_keys(result.errors)) # error in convert should be treated as critical as the input for # .validate() is different assert_true(result.errors[0].is_critical)
def test_can_gather_empty_errors_correctly(self): schema = SchemaValidator(exception_if_invalid=False) schema.add('first', IntegerValidator(required=True, exception_if_invalid=False)) schema.add('second', IntegerValidator(required=False, exception_if_invalid=False)) input_ = OrderedDict((('first', ''), ('second', ''))) result = schema.process(input_) assert_true(result.contains_errors()) first = result.children['first'] assert_true(first.contains_errors(), message='"first" is required and got an empty string as input') assert_equals(('empty',), error_keys(first.errors)) second = result.children['second'] assert_false(second.contains_errors())
def test_can_add_custom_errors_in_validate_even_if_superclass_already_found_errors( self): self.init_validator( regex='\d+', # this is used to trigger a non-critical error in string ".validate()" min_length=5, # ensure RegexValidator runs its checks in ".validate()" as well use_match_for_conversion=False, # otherwise we can have only a single error exception_if_invalid=False, ) result = self.process('abcd', ensure_valid=False) assert_true(result.contains_errors()) assert_equals(('too_short', 'bad_pattern'), error_keys(result.errors)) assert_no_critical_errors(result.errors)