Exemple #1
0
 def test_valfactory__subclass(self):
     """model.valfactory: Given a validation function and a 
     message, the function should return a model.Validator 
     subclass that uses the given values.
     """
     expected = model.Validated
     
     name = 'Eggs'
     def validate(self, value):
         return value
     msg = 'Bad value'
     actual = model.valfactory(name, validate, msg)
     
     self.assertTrue(issubclass(actual, expected))
Exemple #2
0
 def test__rename(self):
     """model.trusted: The decorator should change the 
     storage_name attribute of the given subclass of 
     model.Validated to the expected value.
     """
     expected = '_Spam__attr'
     
     def validate(self, value):
         return value
     Spam = model.valfactory('Spam', validate, 'Bad.')
     class Eggs:
         attr = Spam()
     cls = model.trusted(Eggs)
     actual = Eggs.attr.storage_name
     
     self.assertEqual(expected, actual)
Exemple #3
0
 def test_valfactory__instantiate(self):
     """model.valfactory: Classes created by the factory 
     must be able to be instantiated.
     """
     expected_name = 'Eggs'
     expected_msg = 'Bad value'
     
     def validate(self, value):
         return value
     cls = model.valfactory(expected_name, validate, expected_msg)
     descr = cls()
     actual_name = cls.__name__
     actual_msg = descr.msg
     
     self.assertTrue(isinstance(descr, cls))
     self.assertTrue(isinstance(descr, model.Validated))
     self.assertEqual(expected_name, actual_name)
     self.assertEqual(expected_msg, actual_msg)
Exemple #4
0
 def test_valfactory__validate(self):
     """model.valfactory: Classes created by the factory 
     validate the data as expected.
     """
     expected = 'eggs'
     expected_exc = ValueError
     
     name = 'Eggs'
     def validate(self, value):
         if value != expected:
             raise ValueError(self.msg)
         return value
     msg = f'Not {expected}.'
     Eggs = model.valfactory(name, validate, msg)
     class Bacon:
         attr = Eggs()
     obj = Bacon()
     obj.attr = 'eggs'
     actual = obj.attr
     
     self.assertEqual(expected, actual)
     with self.assertRaises(expected_exc):
         obj.attr = 'spam'
Exemple #5
0
    url = up.ParseResult(*args)
    normal = url.geturl()
    return normal


def val_phone_number(self, value, charset='utf_8', form='NFC'):
    """Is it a valid North American Numbering Plan phone number?"""
    value = val_text(self, value, charset, form)
    pattern = r'^[0-9]{3}-[0-9]{3}-[0-9]{4}$'
    if not match(pattern, value):
        value = sub(r'^[(]([0-9]{3})[)]', r'\1-', value)
        value = sub(r'^([0-9]{3})([0-9]{3})([0-9]{4})$', r'\1-\2-\3', value)
        if not match(pattern, value):
            reason = 'not a valid phone number'
            raise ValueError(self.msg.format(reason))
    return value


def val_whitelist(self, value, whitelist):
    """Validate the value is whitelisted."""
    if value not in whitelist:
        reason = 'not an allowed value'
        raise ValueError(self.msg.format(reason))
    return value


# Validating descriptors.
HttpUrl = valfactory('HttpUrl', val_http_url, 'Invalid HTTP URL ({}).')
Phone = valfactory('Phone', val_phone_number, 'Invalid Phone Number {()}.')
Text = valfactory('Text', val_text, 'Invalid text ({}).')