def convert_map_type(cls, value: OrderedMapSerializedKey) -> List[Dict[str, Any]]: """ Converts a map to a repeated RECORD that contains two fields: 'key' and 'value', each will be converted to its corresponding data type in BQ. """ converted_map = [] for k, v in zip(value.keys(), value.values()): converted_map.append({'key': cls.convert_value(k), 'value': cls.convert_value(v)}) return converted_map
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_init(self): om = OrderedMapSerializedKey(UTF8Type, 2) self.assertEqual(om, {})
(b'\x7f\xff\xff\xff\xff\xff\xff\xff', 'LongType', 9223372036854775807), (b'\x80\x00\x00\x00\x00\x00\x00\x00', 'LongType', -9223372036854775808), (b'', 'LongType', None), (b'', 'InetAddressType', None), (b'A46\xa9', 'InetAddressType', '65.52.54.169'), (b'*\x00\x13(\xe1\x02\xcc\xc0\x00\x00\x00\x00\x00\x00\x01"', 'InetAddressType', '2a00:1328:e102:ccc0::122'), (b'\xe3\x81\xbe\xe3\x81\x97\xe3\x81\xa6', 'UTF8Type', u'\u307e\u3057\u3066'), (b'\xe3\x81\xbe\xe3\x81\x97\xe3\x81\xa6' * 1000, 'UTF8Type', u'\u307e\u3057\u3066' * 1000), (b'', 'UTF8Type', u''), (b'\xff' * 16, 'UUIDType', UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')), (b'I\x15~\xfc\xef<\x9d\xe3\x16\x98\xaf\x80\x1f\xb4\x0b*', 'UUIDType', UUID('49157efc-ef3c-9de3-1698-af801fb40b2a')), (b'', 'UUIDType', None), (b'', 'MapType(AsciiType, BooleanType)', None), (b'', 'ListType(FloatType)', None), (b'', 'SetType(LongType)', None), (b'\x00\x00', 'MapType(DecimalType, BooleanType)', OrderedMapSerializedKey(DecimalType, 0)), (b'\x00\x00', 'ListType(FloatType)', []), (b'\x00\x00', 'SetType(IntegerType)', sortedset()), (b'\x00\x01\x00\x10\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0', 'ListType(TimeUUIDType)', [UUID(bytes=b'\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0')]), (b'\x80\x00\x00\x01', 'SimpleDateType', Date(1)), (b'\x7f\xff\xff\xff', 'SimpleDateType', Date('1969-12-31')), (b'\x00\x00\x00\x00\x00\x00\x00\x01', 'TimeType', Time(1)), (b'\x7f', 'ByteType', 127), (b'\x80', 'ByteType', -128), (b'\x7f\xff', 'ShortType', 32767), (b'\x80\x00', 'ShortType', -32768) ) ordered_map_value = OrderedMapSerializedKey(UTF8Type, 2) ordered_map_value._insert(u'\u307fbob', 199) ordered_map_value._insert(u'', -1)
(b'*\x00\x13(\xe1\x02\xcc\xc0\x00\x00\x00\x00\x00\x00\x01"', 'InetAddressType', '2a00:1328:e102:ccc0::122'), (b'\xe3\x81\xbe\xe3\x81\x97\xe3\x81\xa6', 'UTF8Type', u'\u307e\u3057\u3066'), (b'\xe3\x81\xbe\xe3\x81\x97\xe3\x81\xa6' * 1000, 'UTF8Type', u'\u307e\u3057\u3066' * 1000), (b'', 'UTF8Type', u''), (b'\xff' * 16, 'UUIDType', UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')), (b'I\x15~\xfc\xef<\x9d\xe3\x16\x98\xaf\x80\x1f\xb4\x0b*', 'UUIDType', UUID('49157efc-ef3c-9de3-1698-af801fb40b2a')), (b'', 'UUIDType', None), (b'', 'MapType(AsciiType, BooleanType)', None), (b'', 'ListType(FloatType)', None), (b'', 'SetType(LongType)', None), (b'\x00\x00', 'MapType(DecimalType, BooleanType)', OrderedMapSerializedKey(DecimalType, 0)), (b'\x00\x00', 'ListType(FloatType)', []), (b'\x00\x00', 'SetType(IntegerType)', sortedset()), (b'\x00\x01\x00\x10\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0', 'ListType(TimeUUIDType)', [UUID(bytes=b'\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0')]), (b'\x80\x00\x00\x01', 'SimpleDateType', Date(1)), (b'\x7f\xff\xff\xff', 'SimpleDateType', Date('1969-12-31')), (b'\x00\x00\x00\x00\x00\x00\x00\x01', 'TimeType', Time(1)), (b'\x7f', 'ByteType', 127), (b'\x80', 'ByteType', -128), (b'\x7f\xff', 'ShortType', 32767), (b'\x80\x00', 'ShortType', -32768)) ordered_map_value = OrderedMapSerializedKey(UTF8Type, 2) ordered_map_value._insert(u'\u307fbob', 199)
(b'', 'ListType(FloatType)', None), (b'', 'SetType(LongType)', None), (b'\x00\x00', 'MapType(DecimalType, BooleanType)', OrderedMapSerializedKey(DecimalType, 0)), (b'\x00\x00', 'ListType(FloatType)', []), (b'\x00\x00', 'SetType(IntegerType)', sortedset()), (b'\x00\x01\x00\x10\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0', 'ListType(TimeUUIDType)', [UUID(bytes=b'\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0')]), (b'\x80\x00\x00\x01', 'SimpleDateType', Date(1)), (b'\x7f\xff\xff\xff', 'SimpleDateType', Date('1969-12-31')), (b'\x00\x00\x00\x00\x00\x00\x00\x01', 'TimeType', Time(1)), (b'\x7f', 'ByteType', 127), (b'\x80', 'ByteType', -128), (b'\x7f\xff', 'ShortType', 32767), (b'\x80\x00', 'ShortType', -32768) ) ordered_map_value = OrderedMapSerializedKey(UTF8Type, 2) ordered_map_value._insert(u'\u307fbob', 199) ordered_map_value._insert(u'', -1) ordered_map_value._insert(u'\\', 0) # these following entries work for me right now, but they're dependent on # vagaries of internal python ordering for unordered types marshalled_value_pairs_unsafe = ( (b'\x00\x03\x00\x06\xe3\x81\xbfbob\x00\x04\x00\x00\x00\xc7\x00\x00\x00\x04\xff\xff\xff\xff\x00\x01\\\x00\x04\x00\x00\x00\x00', 'MapType(UTF8Type, Int32Type)', ordered_map_value), (b'\x00\x02\x00\x08@\x01\x99\x99\x99\x99\x99\x9a\x00\x08@\x14\x00\x00\x00\x00\x00\x00', 'SetType(DoubleType)', sortedset([2.2, 5.0])), (b'\x00', 'IntegerType', 0), ) if platform.python_implementation() == 'CPython': # Only run tests for entries which depend on internal python ordering under # CPython
(b"", "ListType(FloatType)", None), (b"", "SetType(LongType)", None), (b"\x00\x00", "MapType(DecimalType, BooleanType)", OrderedMapSerializedKey(DecimalType, 0)), (b"\x00\x00", "ListType(FloatType)", []), (b"\x00\x00", "SetType(IntegerType)", sortedset()), ( b'\x00\x01\x00\x10\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0', "ListType(TimeUUIDType)", [UUID(bytes=b'\xafYC\xa3\xea<\x11\xe1\xabc\xc4,\x03"y\xf0')], ), (b"\x80\x00\x00\x01", "SimpleDateType", Date(1)), (b"\x7f\xff\xff\xff", "SimpleDateType", Date("1969-12-31")), (b"\x00\x00\x00\x00\x00\x00\x00\x01", "TimeType", Time(1)), ) ordered_map_value = OrderedMapSerializedKey(UTF8Type, 2) ordered_map_value._insert(u"\u307fbob", 199) ordered_map_value._insert(u"", -1) ordered_map_value._insert(u"\\", 0) # these following entries work for me right now, but they're dependent on # vagaries of internal python ordering for unordered types marshalled_value_pairs_unsafe = ( ( b"\x00\x03\x00\x06\xe3\x81\xbfbob\x00\x04\x00\x00\x00\xc7\x00\x00\x00\x04\xff\xff\xff\xff\x00\x01\\\x00\x04\x00\x00\x00\x00", "MapType(UTF8Type, Int32Type)", ordered_map_value, ), ( b"\x00\x02\x00\x08@\x01\x99\x99\x99\x99\x99\x9a\x00\x08@\x14\x00\x00\x00\x00\x00\x00", "SetType(DoubleType)",