def clean(self): """ Adds custom validations to the model's clean() method. """ super(StringParser, self).clean() validators.validate_str_substitution(self.formatter, 1)
def _validate_formatter(self): """ Ensures that the formatter can accomodate the number of source_fields. """ if self.method in ['COPY', 'SUBSTRING']: fields = self.source_fields.split(',') validators.validate_str_substitution(self.formatter, len(fields)) else: # for COUNT and P/A, results will be aggregated into one value validators.validate_str_substitution(self.formatter, 1)
def test_single_specifier_missing(self): """ Test case for the validate_str_substitution function when no specifier is provided for a single value. """ template = 'missing' value_count = 1 msg = 'The formatter should contain one "{}" specifier.' with six.assertRaisesRegex(self, ValidationError, msg): validate_str_substitution(template, value_count)
def test_no_template(self): """ Test case for the validate_str_substitution function when no formatter is provided for multiple values. """ template = '' value_count = 2 msg = 'No template has been provided for formatting multiple fields.' with six.assertRaisesRegex(self, ValidationError, msg): validate_str_substitution(template, value_count)
def test_mult_specifiers_missing(self): """ Test case for the validate_str_substitution function when not enough specifiers are provided for multiple values. """ template = '{0} too few {1}' value_count = 3 msg = ('The formatter contains too few "{}" ' 'specifiers for the number of source fields.') with six.assertRaisesRegex(self, ValidationError, msg): validate_str_substitution(template, value_count)
def test_too_many_specifiers(self): """ Test case for the validate_str_substitution function when there are too many specifiers for a multiple values. """ template = '{0} too {1} many {2}' value_count = 2 msg = ('The number of "{}" specifiers in the formatter ' 'exceeds the number of source fields.') with six.assertRaisesRegex(self, ValidationError, msg): validate_str_substitution(template, value_count)
def test_single_specifier_needed(self): """ Test case for the validate_str_substitution function when there are too many specifiers for a single value. """ template = '{0} one too many {1}' value_count = 1 msg = ('The formatter should only contain one ' '"{}" specifier for the source field.') with six.assertRaisesRegex(self, ValidationError, msg): validate_str_substitution(template, value_count)
def test_no_template_or_value(self): """ Test case for the validate_str_substitution function when there is no template and no values. """ template = None value_count = 0 try: validate_str_substitution(template, value_count) except ValidationError: self.fail('Name raised ValidationError unexpectedly')
def test_valid(self): """ Test case for the validate_str_substitution function for a valid number of specifiers. """ template = '{0} just right {1}' value_count = 2 try: validate_str_substitution(template, value_count) except ValidationError: self.fail('Name raised ValidationError unexpectedly')