def test_init(self):
     a = OrderedMap(zip(['one', 'three', 'two'], [1, 3, 2]))
     b = OrderedMap([('one', 1), ('three', 3), ('two', 2)])
     c = OrderedMap(a)
     builtin = {'one': 1, 'two': 2, 'three': 3}
     self.assertEqual(a, b)
     self.assertEqual(a, c)
     self.assertEqual(a, builtin)
     self.assertEqual(OrderedMap([(1, 1), (1, 2)]), {1: 2})
    def test_get(self):
        keys = ['first', 'middle', 'last']
        om = OrderedMap(zip(keys, range(len(keys))))

        for v, k in enumerate(keys):
            self.assertEqual(om.get(k), v)
        
        self.assertEqual(om.get('notthere', 'default'), 'default')
        self.assertIsNone(om.get('notthere'))
Beispiel #3
0
 def test_should_insert_as_map_values(self):
     """
     This tests will attempt to insert a point, polygon, or line, as values of a map.
     @since 3.20
     @jira_ticket PYTHON-456
     @test_category dse geometric
     @expected_result geometric types should be able to be inserted and queried as values of a map.
     """
     uuid_key = uuid1()
     prepared = self.session.prepare(
         "INSERT INTO tbl (k, m1) VALUES (?, ?)")
     bound_statement = prepared.bind(
         (uuid_key, OrderedMap(zip([1], [self.original_value]))))
     self.session.execute(bound_statement)
     self.validate('m1', uuid_key,
                   OrderedMap(zip([1], [self.original_value])))
    def test_equal(self):
        d1 = {'one': 1}
        d12 = {'one': 1, 'two': 2}
        od1 = OrderedMap({'one': 1})
        od12 = OrderedMap([('one', 1), ('two', 2)])
        od21 = OrderedMap([('two', 2), ('one', 1)])

        self.assertEqual(od1, d1)
        self.assertEqual(od12, d12)
        self.assertEqual(od21, d12)
        self.assertNotEqual(od1, od12)
        self.assertNotEqual(od12, od1)
        self.assertNotEqual(od12, od21)
        self.assertNotEqual(od1, d12)
        self.assertNotEqual(od12, d1)
        self.assertNotEqual(od1, EMPTY)
    def test_strings(self):
        # changes in 3.x
        d = {'map': 'inner'}
        s = set([1, 2, 3])
        self.assertEqual(
            repr(
                OrderedMap([('two', 2), ('one', 1), (d, 'value'),
                            (s, 'another')])),
            "OrderedMap([('two', 2), ('one', 1), (%r, 'value'), (%r, 'another')])"
            % (d, s))

        self.assertEqual(
            str(
                OrderedMap([('two', 2), ('one', 1), (d, 'value'),
                            (s, 'another')])),
            "{'two': 2, 'one': 1, %r: 'value', %r: 'another'}" % (d, s))
    def test_getitem(self):
        keys = ['first', 'middle', 'last']
        om = OrderedMap(zip(keys, range(len(keys))))

        for v, k in enumerate(keys):
            self.assertEqual(om[k], v)

        with self.assertRaises(KeyError):
            om['notthere']
    def test_iter(self):
        keys = ['first', 'middle', 'last']
        values = list(range(len(keys)))
        items = list(zip(keys, values))
        om = OrderedMap(items)

        itr = iter(om)
        self.assertEqual(sum([1 for _ in itr]), len(keys))
        self.assertRaises(StopIteration, six.next, itr)

        self.assertEqual(list(iter(om)), keys)
        self.assertEqual(list(six.iteritems(om)), items)
        self.assertEqual(list(six.itervalues(om)), values)
Beispiel #8
0
 def deserialize_safe(cls, byts, protocol_version):
     subkeytype, subvaltype = cls.subtypes
     if protocol_version >= 3:
         unpack = int32_unpack
         length = 4
     else:
         unpack = uint16_unpack
         length = 2
     numelements = unpack(byts[:length])
     p = length
     themap = OrderedMap()
     for _ in range(numelements):
         key_len = unpack(byts[p:p + length])
         p += length
         keybytes = byts[p:p + key_len]
         p += key_len
         val_len = unpack(byts[p:p + length])
         p += length
         valbytes = byts[p:p + val_len]
         p += val_len
         key = subkeytype.from_binary(keybytes, protocol_version)
         val = subvaltype.from_binary(valbytes, protocol_version)
         themap._insert(key, val)
     return themap
Beispiel #9
0
 def deserialize_safe(cls, byts, protocol_version):
     subkeytype, subvaltype = cls.subtypes
     if protocol_version >= 3:
         unpack = int32_unpack
         length = 4
     else:
         unpack = uint16_unpack
         length = 2
     numelements = unpack(byts[:length])
     p = length
     themap = OrderedMap()
     for _ in range(numelements):
         key_len = unpack(byts[p:p + length])
         p += length
         keybytes = byts[p:p + key_len]
         p += key_len
         val_len = unpack(byts[p:p + length])
         p += length
         valbytes = byts[p:p + val_len]
         p += val_len
         key = subkeytype.from_binary(keybytes, protocol_version)
         val = subvaltype.from_binary(valbytes, protocol_version)
         themap._insert(key, val)
     return themap
Beispiel #10
0
def get_collection_sample(collection_type, datatype):
    """
    Helper method to access created sample data for collection types
    """

    if collection_type == 'list':
        return [get_sample(datatype), get_sample(datatype)]
    elif collection_type == 'set':
        return sortedset([get_sample(datatype)])
    elif collection_type == 'map':
        return OrderedMap([(get_sample(datatype), get_sample(datatype))])
    elif collection_type == 'tuple':
        return (get_sample(datatype),)
    else:
        raise Exception('Missing handling of non-primitive type {0}.'.format(collection_type))
    def test_keys(self):
        keys = ['first', 'middle', 'last']
        om = OrderedMap(zip(keys, range(len(keys))))

        self.assertListEqual(list(om.keys()), keys)
 def test_len(self):
     self.assertEqual(len(OrderedMap()), 0)
     self.assertEqual(len(OrderedMap([(1, 1)])), 1)
 def test_mutable_keys(self):
     d = {'1': 1}
     s = set([1, 2, 3])
     om = OrderedMap([(d, 'dict'), (s, 'set')])
    (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)', OrderedMap()),
    (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')]),
)

ordered_map_value = OrderedMap([(u'\u307fbob', 199), (u'', -1), (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',
Beispiel #15
0
    def test_can_insert_nested_collections(self):
        """
        Test for inserting various types of nested COLLECTION_TYPES into tables and UDTs
        """

        if self.cass_version < (2, 1, 3):
            raise unittest.SkipTest(
                "Support for nested collections was introduced in Cassandra 2.1.3"
            )

        c = TestCluster()
        s = c.connect(self.keyspace_name, wait_for_all_pools=True)
        s.encoder.mapping[tuple] = s.encoder.cql_encode_tuple

        name = self._testMethodName

        s.execute("""
            CREATE TYPE %s (
                m frozen<map<int,text>>,
                t tuple<int,text>,
                l frozen<list<int>>,
                s frozen<set<int>>
            )""" % name)
        s.execute("""
            CREATE TYPE %s_nested (
                m frozen<map<int,text>>,
                t tuple<int,text>,
                l frozen<list<int>>,
                s frozen<set<int>>,
                u frozen<%s>
            )""" % (name, name))
        s.execute("""
            CREATE TABLE %s (
                k int PRIMARY KEY,
                map_map map<frozen<map<int,int>>, frozen<map<int,int>>>,
                map_set map<frozen<set<int>>, frozen<set<int>>>,
                map_list map<frozen<list<int>>, frozen<list<int>>>,
                map_tuple map<frozen<tuple<int, int>>, frozen<tuple<int>>>,
                map_udt map<frozen<%s_nested>, frozen<%s>>,
            )""" % (name, name, name))

        validate = partial(self.insert_select_column, s, name)
        validate(
            'map_map',
            OrderedMap([({
                1: 1,
                2: 2
            }, {
                3: 3,
                4: 4
            }), ({
                5: 5,
                6: 6
            }, {
                7: 7,
                8: 8
            })]))
        validate(
            'map_set',
            OrderedMap([(set((1, 2)), set((3, 4))), (set((5, 6)), set(
                (7, 8)))]))
        validate('map_list', OrderedMap([([1, 2], [3, 4]), ([5, 6], [7, 8])]))
        validate('map_tuple', OrderedMap([((1, 2), (3, )), ((4, 5), (6, ))]))

        value = nested_collection_udt({
            1: 'v1',
            2: 'v2'
        }, (3, 'v3'), [4, 5, 6, 7], set((8, 9, 10)))
        key = nested_collection_udt_nested(value.m, value.t, value.l, value.s,
                                           value)
        key2 = nested_collection_udt_nested({3: 'v3'}, value.t, value.l,
                                            value.s, value)
        validate('map_udt', OrderedMap([(key, value), (key2, value)]))

        c.shutdown()
 def test_popitem(self):
     item = (1, 2)
     om = OrderedMap((item,))
     self.assertEqual(om.popitem(), item)
     self.assertRaises(KeyError, om.popitem)
    def test_items(self):
        keys = ['first', 'middle', 'last']
        items = list(zip(keys, range(len(keys))))
        om = OrderedMap(items)

        self.assertListEqual(list(om.items()), items)
    def test_values(self):
        keys = ['first', 'middle', 'last']
        values = list(range(len(keys)))
        om = OrderedMap(zip(keys, values))

        self.assertListEqual(list(om.values()), values)