def read_type(cls, f, user_type_map): optid = read_short(f) try: typeclass = cls.type_codes[optid] except KeyError: raise NotSupportedError("Unknown data type code 0x%04x. Have to skip" " entire result set." % (optid,)) if typeclass in (ListType, SetType): subtype = cls.read_type(f, user_type_map) typeclass = typeclass.apply_parameters((subtype,)) elif typeclass == MapType: keysubtype = cls.read_type(f, user_type_map) valsubtype = cls.read_type(f, user_type_map) typeclass = typeclass.apply_parameters((keysubtype, valsubtype)) elif typeclass == TupleType: num_items = read_short(f) types = tuple(cls.read_type(f, user_type_map) for _ in range(num_items)) typeclass = typeclass.apply_parameters(types) elif typeclass == UserType: ks = read_string(f) udt_name = read_string(f) num_fields = read_short(f) names, types = zip(*((read_string(f), cls.read_type(f, user_type_map)) for _ in range(num_fields))) specialized_type = typeclass.make_udt_class(ks, udt_name, names, types) specialized_type.mapped_class = user_type_map.get(ks, {}).get(udt_name) typeclass = specialized_type elif typeclass == CUSTOM_TYPE: classname = read_string(f) typeclass = lookup_casstype(classname) return typeclass
def test_marshal_platform(self): for proto_ver in protocol_versions: for geo in self.samples: cql_type = lookup_casstype(geo.__class__.__name__ + 'Type') self.assertEqual( cql_type.from_binary(cql_type.to_binary(geo, proto_ver), proto_ver), geo)
def test_marshalling(self): for serializedval, valtype, nativeval in marshalled_value_pairs: marshaller = lookup_casstype(valtype) whatwegot = marshaller.to_binary(nativeval, 1) self.assertEqual(whatwegot, serializedval, msg='Marshaller for %s (%s) failed: marshal(%r) got %r instead of %r' % (valtype, marshaller, nativeval, whatwegot, serializedval)) self.assertEqual(type(whatwegot), type(serializedval), msg='Marshaller for %s (%s) gave wrong type (%s instead of %s)' % (valtype, marshaller, type(whatwegot), type(serializedval)))
def test_empty_wkb(self): for cls in (LineString, Polygon): class_name = cls.__name__ cql_type = lookup_casstype(class_name + 'Type') self.assertEqual( str(cql_type.from_binary(cql_type.to_binary(cls(), 0), 0)), class_name.upper() + " EMPTY") self.assertEqual( str(PointType.from_binary(PointType.to_binary(Point(), 0), 0)), "POINT (nan nan)")
def test_normalized_lookup(self): key_type = lookup_casstype('MapType(UTF8Type, Int32Type)') protocol_version = 3 om = OrderedMapSerializedKey(key_type, protocol_version) key_ascii = {'one': 1} key_unicode = {u'two': 2} om._insert_unchecked(key_ascii, key_type.serialize(key_ascii, protocol_version), object()) om._insert_unchecked(key_unicode, key_type.serialize(key_unicode, protocol_version), object()) # type lookup is normalized by key_type # PYTHON-231 self.assertIs(om[{'one': 1}], om[{u'one': 1}]) self.assertIs(om[{'two': 2}], om[{u'two': 2}]) self.assertIsNot(om[{'one': 1}], om[{'two': 2}])
def test_lookup_casstype(self): """ Ensure lookup_casstype returns the correct classes """ self.assertEqual(lookup_casstype('AsciiType'), dse.cqltypes.AsciiType) self.assertEqual(lookup_casstype('LongType'), dse.cqltypes.LongType) self.assertEqual(lookup_casstype('BytesType'), dse.cqltypes.BytesType) self.assertEqual(lookup_casstype('BooleanType'), dse.cqltypes.BooleanType) self.assertEqual(lookup_casstype('CounterColumnType'), dse.cqltypes.CounterColumnType) self.assertEqual(lookup_casstype('DateType'), dse.cqltypes.DateType) self.assertEqual(lookup_casstype('DecimalType'), dse.cqltypes.DecimalType) self.assertEqual(lookup_casstype('DoubleType'), dse.cqltypes.DoubleType) self.assertEqual(lookup_casstype('FloatType'), dse.cqltypes.FloatType) self.assertEqual(lookup_casstype('InetAddressType'), dse.cqltypes.InetAddressType) self.assertEqual(lookup_casstype('Int32Type'), dse.cqltypes.Int32Type) self.assertEqual(lookup_casstype('UTF8Type'), dse.cqltypes.UTF8Type) self.assertEqual(lookup_casstype('DateType'), dse.cqltypes.DateType) self.assertEqual(lookup_casstype('TimeType'), dse.cqltypes.TimeType) self.assertEqual(lookup_casstype('ByteType'), dse.cqltypes.ByteType) self.assertEqual(lookup_casstype('ShortType'), dse.cqltypes.ShortType) self.assertEqual(lookup_casstype('TimeUUIDType'), dse.cqltypes.TimeUUIDType) self.assertEqual(lookup_casstype('UUIDType'), dse.cqltypes.UUIDType) self.assertEqual(lookup_casstype('IntegerType'), dse.cqltypes.IntegerType) self.assertEqual(lookup_casstype('MapType'), dse.cqltypes.MapType) self.assertEqual(lookup_casstype('ListType'), dse.cqltypes.ListType) self.assertEqual(lookup_casstype('SetType'), dse.cqltypes.SetType) self.assertEqual(lookup_casstype('CompositeType'), dse.cqltypes.CompositeType) self.assertEqual(lookup_casstype('ColumnToCollectionType'), dse.cqltypes.ColumnToCollectionType) self.assertEqual(lookup_casstype('ReversedType'), dse.cqltypes.ReversedType) self.assertEqual(lookup_casstype('DurationType'), dse.cqltypes.DurationType) self.assertEqual(lookup_casstype('DateRangeType'), dse.cqltypes.DateRangeType) self.assertEqual(str(lookup_casstype('unknown')), str(dse.cqltypes.mkUnrecognizedType('unknown'))) self.assertRaises(ValueError, lookup_casstype, 'AsciiType~')