Example #1
0
    def test_required_validation(self):
        """ Tests that validation raise on none and blank values if value required. """
        Ascii(required=True).validate('k')

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

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

        # With min_length set.
        Ascii(required=True, min_length=0).validate('k')
        Ascii(required=True, min_length=1).validate('k')

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

        # With max_length set.
        Ascii(required=True, max_length=1).validate('k')

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

        with self.assertRaises(ValueError):
            Ascii(required=True, max_length=0)
Example #2
0
def _str_to_column(type_str, key_type=None, value_type=None, column_def={}):
    '''
    Converts name of Cassandra types to driver class wrapper for
    that type.
    '''
    type_str = type_str.lower()

    if type_str == 'integer':
        return Integer(**column_def)
    elif type_str == 'text':
        return Text(**column_def)
    elif type_str == 'ascii':
        return Ascii(**column_def)
    elif type_str == 'bigint':
        return BigInt(**column_def)
    elif type_str == 'blob':
        return Blob(**column_def)
    elif type_str == 'bytes':
        return Bytes(**column_def)
    elif type_str == 'boolean':
        return Boolean(**column_def)
    elif type_str == 'counter':
        return Counter(**column_def)
    elif type_str == 'date':
        return Date(**column_def)
    elif type_str == 'datetime':
        return DateTime(**column_def)
    elif type_str == 'decimal':
        return Decimal(**column_def)
    elif type_str == 'double':
        return Double(**column_def)
    elif type_str == 'float':
        return Float(**column_def)
    elif type_str == 'list':
        _assert_type_exception(value_type, "list type requires value_type")
        return List(value_type=value_type, **column_def)
    elif type_str == 'map':
        _assert_type_exception(key_type, "list type requires key_type")
        _assert_type_exception(value_type, "list type requires value_type")
        return Map(key_type=key_type, value_type=value_type, **column_def)
    elif type_str == 'set':
        _assert_type_exception(value_type, "set type requires value_type")
        return Set(value_type=value_type, **column_def)
    elif type_str == 'smallint':
        return SmallInt(**column_def)
    elif type_str == 'time':
        return Time(**column_def)
    elif type_str == 'timeuuid':
        return TimeUUID(**column_def)
    elif type_str == 'timestamp':
        return TimeUUID(**column_def)
    elif type_str == 'tinyint':
        return TinyInt(**column_def)
    elif type_str == 'uuid':
        return UUID(**column_def)
    elif type_str == 'varint':
        return VarInt(**column_def)
    else:
        raise Exception('Type {} is not defined.'.format(type_str))
Example #3
0
    def test_length_range(self):
        Ascii(min_length=0, max_length=0)
        Ascii(min_length=0, max_length=1)
        Ascii(min_length=10, max_length=10)
        Ascii(min_length=10, max_length=11)

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

        with self.assertRaises(ValueError):
            Ascii(min_length=1, max_length=0)
Example #4
0
    def test_type_checking(self):
        Ascii().validate('string')
        Ascii().validate(u'unicode')
        Ascii().validate(bytearray('bytearray', encoding='ascii'))

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

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

        Ascii().validate("!#$%&\'()*+,-./")

        with self.assertRaises(ValidationError):
            Ascii().validate('Beyonc' + chr(233))

        if sys.version_info < (3, 1):
            with self.assertRaises(ValidationError):
                Ascii().validate(u'Beyonc' + unichr(233))
Example #5
0
 def test_non_required_validation(self):
     """ Tests that validation is ok on none and blank values if required is False. """
     Ascii().validate('')
     Ascii().validate(None)
Example #6
0
 def test_unaltering_validation(self):
     """ Test the validation step doesn't re-interpret values. """
     self.assertEqual(Ascii().validate(''), '')
     self.assertEqual(Ascii().validate(None), None)
     self.assertEqual(Ascii().validate('yo'), 'yo')
Example #7
0
    def test_max_length(self):
        """ Test arbitrary maximal lengths requirements. """
        Ascii(max_length=0).validate('')
        Ascii(max_length=0).validate(None)

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

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

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

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

        with self.assertRaises(ValueError):
            Ascii(max_length=-1)
Example #8
0
    def test_min_length(self):
        """ Test arbitrary minimal lengths requirements. """
        Ascii(min_length=0).validate('')
        Ascii(min_length=0).validate(None)
        Ascii(min_length=0).validate('kevin')

        Ascii(min_length=1).validate('k')

        Ascii(min_length=5).validate('kevin')
        Ascii(min_length=5).validate('kevintastic')

        with self.assertRaises(ValidationError):
            Ascii(min_length=1).validate('')

        with self.assertRaises(ValidationError):
            Ascii(min_length=1).validate(None)

        with self.assertRaises(ValidationError):
            Ascii(min_length=6).validate('')

        with self.assertRaises(ValidationError):
            Ascii(min_length=6).validate(None)

        with self.assertRaises(ValidationError):
            Ascii(min_length=6).validate('kevin')

        with self.assertRaises(ValueError):
            Ascii(min_length=-1)