Ejemplo n.º 1
0
    def test_required_validation(self):
        """ Tests that validation raise on none and blank values if value required. """
        Text(required=True).validate('b')

        with self.assertRaises(ValidationError):
            Text(required=True).validate('')

        with self.assertRaises(ValidationError):
            Text(required=True).validate(None)

        # With min_length set.
        Text(required=True, min_length=0).validate('b')
        Text(required=True, min_length=1).validate('b')

        with self.assertRaises(ValidationError):
            Text(required=True, min_length=2).validate('b')

        # With max_length set.
        Text(required=True, max_length=1).validate('b')

        with self.assertRaises(ValidationError):
            Text(required=True, max_length=2).validate('blake')

        with self.assertRaises(ValueError):
            Text(required=True, max_length=0)
Ejemplo n.º 2
0
    def test_type_checking(self):
        Text().validate('string')
        Text().validate(u'unicode')
        Text().validate(bytearray('bytearray', encoding='ascii'))

        with self.assertRaises(ValidationError):
            Text().validate(5)

        with self.assertRaises(ValidationError):
            Text().validate(True)

        Text().validate("!#$%&\'()*+,-./")
        Text().validate('Beyonc' + chr(233))
        if sys.version_info < (3, 1):
            Text().validate(u'Beyonc' + unichr(233))
Ejemplo n.º 3
0
    def test_length_range(self):
        Text(min_length=0, max_length=0)
        Text(min_length=0, max_length=1)
        Text(min_length=10, max_length=10)
        Text(min_length=10, max_length=11)

        with self.assertRaises(ValueError):
            Text(min_length=10, max_length=9)

        with self.assertRaises(ValueError):
            Text(min_length=1, max_length=0)
Ejemplo n.º 4
0
 def test_non_required_validation(self):
     """ Tests that validation is ok on none and blank values if required is False """
     Text().validate('')
     Text().validate(None)
Ejemplo n.º 5
0
 def test_unaltering_validation(self):
     """ Test the validation step doesn't re-interpret values. """
     self.assertEqual(Text().validate(''), '')
     self.assertEqual(Text().validate(None), None)
     self.assertEqual(Text().validate('yo'), 'yo')
Ejemplo n.º 6
0
    def test_max_length(self):
        """ Test arbitrary maximal lengths requirements. """
        Text(max_length=0).validate('')
        Text(max_length=0).validate(None)

        Text(max_length=1).validate('')
        Text(max_length=1).validate(None)
        Text(max_length=1).validate('b')

        Text(max_length=5).validate('')
        Text(max_length=5).validate(None)
        Text(max_length=5).validate('b')
        Text(max_length=5).validate('blake')

        with self.assertRaises(ValidationError):
            Text(max_length=0).validate('b')

        with self.assertRaises(ValidationError):
            Text(max_length=5).validate('blaketastic')

        with self.assertRaises(ValueError):
            Text(max_length=-1)