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)
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))
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)
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)
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')
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)