def test_contains(self): keys = ['first', 'middle', 'last'] om = OrderedMap() om = OrderedMap(zip(keys, range(len(keys)))) for k in keys: self.assertTrue(k in om) self.assertFalse(k not in om) self.assertTrue('notthere' not in om) self.assertFalse('notthere' in om)
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_should_insert_as_map_keys(self): """ This tests will attempt to insert a point, polygon, or line, as keys of a map. @since 1.0.0 @jira_ticket PYTHON-456 @test_category dse geometric @expected_result geometric types should be able to be inserted and queried as keys of a map. """ uuid_key = uuid1() prepared = self.session.prepare( "INSERT INTO tbl (k, m0) VALUES (?, ?)") bound_statement = prepared.bind( (uuid_key, OrderedMap(zip([self.original_value], [1])))) self.session.execute(bound_statement) self.validate('m0', uuid_key, OrderedMap(zip([self.original_value], [1])))
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'))
def test_equal(self): d1 = {'one': 1} d12 = {'one': 1, 'two': 2} om1 = OrderedMap({'one': 1}) om12 = OrderedMap([('one', 1), ('two', 2)]) om21 = OrderedMap([('two', 2), ('one', 1)]) self.assertEqual(om1, d1) self.assertEqual(om12, d12) self.assertEqual(om21, d12) self.assertNotEqual(om1, om12) self.assertNotEqual(om12, om1) self.assertNotEqual(om12, om21) self.assertNotEqual(om1, d12) self.assertNotEqual(om12, d1) self.assertNotEqual(om1, EMPTY) self.assertFalse(OrderedMap([('three', 3), ('four', 4)]) == d12)
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_delitem(self): om = OrderedMap({1: 1, 2: 2}) self.assertRaises(KeyError, om.__delitem__, 3) del om[1] self.assertEqual(om, {2: 2}) del om[2] self.assertFalse(om) self.assertRaises(KeyError, om.__delitem__, 1)
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)
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_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}) d = OrderedMap({'': 3}, key1='v1', key2='v2') self.assertEqual(d[''], 3) self.assertEqual(d['key1'], 'v1') self.assertEqual(d['key2'], 'v2') with self.assertRaises(TypeError): OrderedMap('too', 'many', 'args')
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 = Cluster(protocol_version=PROTOCOL_VERSION) 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_len(self): self.assertEqual(len(OrderedMap()), 0) self.assertEqual(len(OrderedMap([(1, 1)])), 1)
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)
def test_keys(self): keys = ['first', 'middle', 'last'] om = OrderedMap(zip(keys, range(len(keys)))) self.assertListEqual(list(om.keys()), keys)
def test_mutable_keys(self): d = {'1': 1} s = set([1, 2, 3]) om = OrderedMap([(d, 'dict'), (s, 'set')])
def test_popitem(self): item = (1, 2) om = OrderedMap((item, )) self.assertEqual(om.popitem(), item) self.assertRaises(KeyError, om.popitem)