def test_intersection(self): s12 = set([1, 2]) ss23 = sortedset([2, 3]) self.assertEqual(s12.intersection(ss23), set([2])) self.assertEqual(ss23.intersection(s12), sortedset([2])) self.assertEqual(ss23.intersection(s12, [2], (2, )), sortedset([2])) self.assertEqual(ss23.intersection(s12, [900], (2, )), sortedset())
def test_intersection(self): s12 = set([1, 2]) ss23 = sortedset([2, 3]) self.assertEqual(s12.intersection(ss23), set([2])) self.assertEqual(ss23.intersection(s12), sortedset([2])) self.assertEqual(ss23.intersection(s12, [2], (2,)), sortedset([2])) self.assertEqual(ss23.intersection(s12, [900], (2,)), sortedset())
def test_isdisjoint(self): # set, ss s12 = set([1, 2]) s2 = set([2]) ss1 = sortedset([1]) ss13 = sortedset([1, 3]) ss3 = sortedset([3]) # s ss disjoint self.assertTrue(s2.isdisjoint(ss1)) self.assertTrue(s2.isdisjoint(ss13)) # s ss not disjoint self.assertFalse(s12.isdisjoint(ss1)) self.assertFalse(s12.isdisjoint(ss13)) # ss s disjoint self.assertTrue(ss1.isdisjoint(s2)) self.assertTrue(ss13.isdisjoint(s2)) # ss s not disjoint self.assertFalse(ss1.isdisjoint(s12)) self.assertFalse(ss13.isdisjoint(s12)) # ss ss disjoint self.assertTrue(ss1.isdisjoint(ss3)) self.assertTrue(ss3.isdisjoint(ss1)) # ss ss not disjoint self.assertFalse(ss1.isdisjoint(ss13)) self.assertFalse(ss13.isdisjoint(ss1)) self.assertFalse(ss3.isdisjoint(ss13)) self.assertFalse(ss13.isdisjoint(ss3))
def test_collection_set_ttl(self): """ Test that ttl has a granularity of elements using a set collection. """ self.prepare(default_time_to_live=10) self.session1.execute("ALTER TABLE ttl_table ADD myset set<int>;""") start = time.time() self.session1.execute(""" INSERT INTO ttl_table (key, col1, myset) VALUES (%d, %d, %s); """ % (1, 1, '{1,2,3,4,5}')) self.session1.execute(""" UPDATE ttl_table USING TTL 3 SET myset = myset + {42} WHERE key=1; """) assert_all( self.session1, "SELECT * FROM ttl_table;", [[1, 1, None, None, sortedset([1, 2, 3, 4, 5, 42])]] ) self.smart_sleep(start, 5) assert_all( self.session1, "SELECT * FROM ttl_table;", [[1, 1, None, None, sortedset([1, 2, 3, 4, 5])]] ) self.smart_sleep(start, 12) assert_row_count(self.session1, 'ttl_table', 0)
def test_symmetric_difference(self): s = set([1, 3, 5]) ss = sortedset([2, 3, 4]) ss2 = sortedset([5, 6, 7]) self.assertEqual(ss.symmetric_difference(s), sortedset([1, 2, 4, 5])) self.assertFalse(ss.symmetric_difference(ss)) self.assertEqual(ss.symmetric_difference(s), sortedset([1, 2, 4, 5])) self.assertEqual(ss2.symmetric_difference(ss), sortedset([2, 3, 4, 5, 6, 7]))
def test_equal(self): s1 = set([1]) s12 = set([1, 2]) ss1 = sortedset(s1) ss12 = sortedset(s12) self.assertEqual(ss1, s1) self.assertEqual(ss12, s12) self.assertNotEqual(ss1, ss12) self.assertNotEqual(ss12, ss1) self.assertNotEqual(ss1, s12) self.assertNotEqual(ss12, s1) self.assertNotEqual(ss1, EMPTY)
def test_issuperset(self): s12 = set([1, 2]) ss1 = sortedset([1]) ss13 = sortedset([1, 3]) ss3 = sortedset([3]) self.assertTrue(s12.issuperset(ss1)) self.assertTrue(ss13.issuperset(ss3)) self.assertTrue(ss13.issuperset(ss13)) self.assertFalse(s12.issuperset(ss13)) self.assertFalse(ss1.issuperset(ss3)) self.assertFalse(ss1.issuperset(ss13))
def test_should_insert_as_set(self): """ This tests will attempt to insert a point, polygon, or line, as values of set. @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 a set. """ uuid_key = uuid1() prepared = self.session.prepare("INSERT INTO tbl (k, s) VALUES (?, ?)") bound_statement = prepared.bind((uuid_key, sortedset([self.original_value]))) self.session.execute(bound_statement) self.validate('s', uuid_key, sortedset([self.original_value]))
def test_should_insert_as_set(self): """ This tests will attempt to insert a point, polygon, or line, as values of set. @since 3.20 @jira_ticket PYTHON-456 @test_category dse geometric @expected_result geometric types should be able to be inserted and queried as a set. """ uuid_key = uuid1() prepared = self.session.prepare("INSERT INTO tbl (k, s) VALUES (?, ?)") bound_statement = prepared.bind( (uuid_key, sortedset([self.original_value]))) self.session.execute(bound_statement) self.validate('s', uuid_key, sortedset([self.original_value]))
def test_equal(self): s1 = set([1]) s12 = set([1, 2]) ss1 = sortedset(s1) ss12 = sortedset(s12) self.assertEqual(ss1, s1) self.assertEqual(ss12, s12) self.assertEqual(ss12, s12) self.assertEqual(ss1.__eq__(None), NotImplemented) self.assertNotEqual(ss1, ss12) self.assertNotEqual(ss12, ss1) self.assertNotEqual(ss1, s12) self.assertNotEqual(ss12, s1) self.assertNotEqual(ss1, EMPTY)
def _multi_dc_tokens(self, random=None): t_min = 0 t_max = 1 << 127 if random is None or not random: t_min = -1 << 63 t_max = 1 << 63 for dc_nodes in self.dc_nodes_combinations: all_tokens = sortedset() node_count = 0 generated_tokens = self.call_token_generator( self.cluster.get_install_dir(), random, dc_nodes) self.assertEqual(dc_nodes.__len__(), generated_tokens.__len__()) for n in range(0, dc_nodes.__len__()): nodes = dc_nodes[n] node_count += nodes tokens = generated_tokens[n] self.assertEqual(nodes, tokens.__len__()) for tok in tokens: self.assertTrue( t_min <= tok < t_max, "Generated token %r out of Murmur3Partitioner range %r..%r" % (tok, t_min, t_max - 1)) self.assertTrue( not all_tokens.__contains__(tok), "Duplicate token %r for nodes-counts %r" % (tok, dc_nodes)) all_tokens.add(tok) self.assertEqual( all_tokens.__len__(), node_count, "Number of tokens %r and number of nodes %r does not match for %r" % (all_tokens.__len__(), node_count, dc_nodes))
def test_delitem(self): expected = [1,2,3,4] ss = sortedset(expected) for i in range(len(ss)): self.assertListEqual(list(ss), expected[i:]) del ss[0] with self.assertRaises(IndexError): ss[0]
def test_delitem(self): expected = [1, 2, 3, 4] ss = sortedset(expected) for i in range(len(ss)): self.assertListEqual(list(ss), expected[i:]) del ss[0] with self.assertRaises(IndexError): ss[0]
def test_difference(self): s1 = set([1]) ss12 = sortedset([1, 2]) ss23 = sortedset([2, 3]) self.assertEqual(sortedset().difference(s1), sortedset()) self.assertEqual(ss12.difference(s1), sortedset([2])) self.assertEqual(ss12.difference(ss23), sortedset([1])) self.assertEqual(ss23.difference(ss12), sortedset([3])) self.assertEqual(ss23.difference(s1), sortedset([2, 3]))
def test_union(self): s1 = set([1]) ss12 = sortedset([1, 2]) ss23 = sortedset([2, 3]) self.assertEqual(sortedset().union(s1), sortedset([1])) self.assertEqual(ss12.union(s1), sortedset([1, 2])) self.assertEqual(ss12.union(ss23), sortedset([1, 2, 3])) self.assertEqual(ss23.union(ss12), sortedset([1, 2, 3])) self.assertEqual(ss23.union(s1), sortedset([1, 2, 3]))
def test_pop(self): ss = sortedset([2, 1]) self.assertEqual(ss.pop(), 2) self.assertEqual(ss.pop(), 1) try: ss.pop() self.fail("Error not thrown") except (KeyError, IndexError) as e: pass
def test_remove(self): ss = sortedset([2, 1]) self.assertEqual(len(ss), 2) self.assertRaises(KeyError, ss.remove, 3) self.assertEqual(len(ss), 2) ss.remove(1) self.assertEqual(len(ss), 1) ss.remove(2) self.assertFalse(ss) self.assertRaises(KeyError, ss.remove, 2) self.assertFalse(ss)
def test_copy(self): class comparable(object): def __lt__(self, other): return id(self) < id(other) o = comparable() ss = sortedset([comparable(), o]) ss2 = ss.copy() self.assertNotEqual(id(ss), id(ss2)) self.assertTrue(o in ss) self.assertTrue(o in ss2)
def test_delslice(self): expected = [1, 2, 3, 4, 5] ss = sortedset(expected) del ss[1:3] self.assertListEqual(list(ss), [1, 4, 5]) del ss[-1:] self.assertListEqual(list(ss), [1, 4]) del ss[1:] self.assertListEqual(list(ss), [1]) del ss[:] self.assertFalse(ss) with self.assertRaises(IndexError): del ss[0]
def test_contains(self): input = [5, 4, 3, 2, 1, 1, 1] expected = sorted(set(input)) ss = sortedset(input) for i in expected: self.assertTrue(i in ss) self.assertFalse(i not in ss) hi = max(expected) + 1 lo = min(expected) - 1 self.assertFalse(hi in ss) self.assertFalse(lo in ss)
def test_contains(self): input = [5, 4, 3, 2, 1, 1, 1] expected = sorted(set(input)) ss = sortedset(input) for i in expected: self.assertTrue(i in ss) self.assertFalse(i not in ss) hi = max(expected)+1 lo = min(expected)-1 self.assertFalse(hi in ss) self.assertFalse(lo in ss)
def test_operators(self): ss1 = sortedset([1]) ss12 = sortedset([1, 2]) # __ne__ self.assertFalse(ss12 != ss12) self.assertFalse(ss12 != sortedset([1, 2])) self.assertTrue(ss12 != sortedset()) # __le__ self.assertTrue(ss1 <= ss12) self.assertTrue(ss12 <= ss12) self.assertFalse(ss12 <= ss1) # __lt__ self.assertTrue(ss1 < ss12) self.assertFalse(ss12 < ss12) self.assertFalse(ss12 < ss1) # __ge__ self.assertFalse(ss1 >= ss12) self.assertTrue(ss12 >= ss12) self.assertTrue(ss12 >= ss1) # __gt__ self.assertFalse(ss1 > ss12) self.assertFalse(ss12 > ss12) self.assertTrue(ss12 > ss1) # __and__ self.assertEqual(ss1 & ss12, ss1) self.assertEqual(ss12 & ss12, ss12) self.assertEqual(ss12 & set(), sortedset()) # __or__ self.assertEqual(ss1 | ss12, ss12) self.assertEqual(ss12 | ss12, ss12) self.assertEqual(ss12 | set(), ss12) self.assertEqual(sortedset() | ss1 | ss12, ss12) # __sub__ self.assertEqual(ss1 - ss12, set()) self.assertEqual(ss12 - ss12, set()) self.assertEqual(ss12 - set(), ss12) self.assertEqual(ss12 - ss1, sortedset([2])) # __xor__ self.assertEqual(ss1 ^ ss12, set([2])) self.assertEqual(ss12 ^ ss1, set([2])) self.assertEqual(ss12 ^ ss12, set()) self.assertEqual(ss12 ^ set(), ss12)
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_uncomparable_types(self, items): for perm in permutations(items): ss = sortedset(perm) s = set(perm) self.assertEqual(s, ss) self.assertEqual(ss, ss.union(s)) for x in range(len(ss)): subset = set(s) for _ in range(x): subset.pop() self.assertEqual(ss.difference(subset), s.difference(subset)) self.assertEqual(ss.intersection(subset), s.intersection(subset)) for x in ss: self.assertIn(x, ss) ss.remove(x) self.assertNotIn(x, ss)
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 _multi_dc_tokens(self, random=None): t_min = 0 t_max = 1 << 127 if random is None or not random: t_min = -1 << 63 t_max = 1 << 63 for dc_nodes in self.dc_nodes_combinations: all_tokens = sortedset() node_count = 0 generated_tokens = self.call_token_generator(self.cluster.get_install_dir(), random, dc_nodes) self.assertEqual(dc_nodes.__len__(), generated_tokens.__len__()) for n in range(0, dc_nodes.__len__()): nodes = dc_nodes[n] node_count += nodes tokens = generated_tokens[n] self.assertEqual(nodes, tokens.__len__()) for tok in tokens: self.assertTrue(t_min <= tok < t_max, "Generated token %r out of Murmur3Partitioner range %r..%r" % (tok, t_min, t_max - 1)) self.assertTrue(not all_tokens.__contains__(tok), "Duplicate token %r for nodes-counts %r" % (tok, dc_nodes)) all_tokens.add(tok) self.assertEqual(all_tokens.__len__(), node_count, "Number of tokens %r and number of nodes %r does not match for %r" % (all_tokens.__len__(), node_count, dc_nodes))
def test_mutable_contents(self): ba = bytearray(b'some data here') ss = sortedset([ba, ba]) self.assertEqual(list(ss), [ba])
def test_reduce_pickle(self): ss = sortedset((4, 3, 2, 1)) import pickle s = pickle.dumps(ss) self.assertEqual(pickle.loads(s), ss)
(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) ordered_map_value._insert(u'\\', 0)
def test_repr(self): self.assertEqual(repr(sortedset([1, 2, 3, 4])), "SortedSet([1, 2, 3, 4])")
def test_operators(self): ss1 = sortedset([1]) ss12 = sortedset([1, 2]) # __ne__ self.assertFalse(ss12 != ss12) self.assertFalse(ss12 != sortedset([1, 2])) self.assertTrue(ss12 != sortedset()) # __le__ self.assertTrue(ss1 <= ss12) self.assertTrue(ss12 <= ss12) self.assertFalse(ss12 <= ss1) # __lt__ self.assertTrue(ss1 < ss12) self.assertFalse(ss12 < ss12) self.assertFalse(ss12 < ss1) # __ge__ self.assertFalse(ss1 >= ss12) self.assertTrue(ss12 >= ss12) self.assertTrue(ss12 >= ss1) # __gt__ self.assertFalse(ss1 > ss12) self.assertFalse(ss12 > ss12) self.assertTrue(ss12 > ss1) # __and__ self.assertEqual(ss1 & ss12, ss1) self.assertEqual(ss12 & ss12, ss12) self.assertEqual(ss12 & set(), sortedset()) # __iand__ tmp = sortedset(ss12) tmp &= ss1 self.assertEqual(tmp, ss1) tmp = sortedset(ss1) tmp &= ss12 self.assertEqual(tmp, ss1) tmp = sortedset(ss12) tmp &= ss12 self.assertEqual(tmp, ss12) tmp = sortedset(ss12) tmp &= set() self.assertEqual(tmp, sortedset()) # __rand__ self.assertEqual(set([1]) & ss12, ss1) # __or__ self.assertEqual(ss1 | ss12, ss12) self.assertEqual(ss12 | ss12, ss12) self.assertEqual(ss12 | set(), ss12) self.assertEqual(sortedset() | ss1 | ss12, ss12) # __ior__ tmp = sortedset(ss1) tmp |= ss12 self.assertEqual(tmp, ss12) tmp = sortedset(ss12) tmp |= ss12 self.assertEqual(tmp, ss12) tmp = sortedset(ss12) tmp |= set() self.assertEqual(tmp, ss12) tmp = sortedset() tmp |= ss1 tmp |= ss12 self.assertEqual(tmp, ss12) # __ror__ self.assertEqual(set([1]) | ss12, ss12) # __sub__ self.assertEqual(ss1 - ss12, set()) self.assertEqual(ss12 - ss12, set()) self.assertEqual(ss12 - set(), ss12) self.assertEqual(ss12 - ss1, sortedset([2])) # __isub__ tmp = sortedset(ss1) tmp -= ss12 self.assertEqual(tmp, set()) tmp = sortedset(ss12) tmp -= ss12 self.assertEqual(tmp, set()) tmp = sortedset(ss12) tmp -= set() self.assertEqual(tmp, ss12) tmp = sortedset(ss12) tmp -= ss1 self.assertEqual(tmp, sortedset([2])) # __rsub__ self.assertEqual(set((1, 2, 3)) - ss12, set((3, ))) # __xor__ self.assertEqual(ss1 ^ ss12, set([2])) self.assertEqual(ss12 ^ ss1, set([2])) self.assertEqual(ss12 ^ ss12, set()) self.assertEqual(ss12 ^ set(), ss12) # __ixor__ tmp = sortedset(ss1) tmp ^= ss12 self.assertEqual(tmp, set([2])) tmp = sortedset(ss12) tmp ^= ss1 self.assertEqual(tmp, set([2])) tmp = sortedset(ss12) tmp ^= ss12 self.assertEqual(tmp, set()) tmp = sortedset(ss12) tmp ^= set() self.assertEqual(tmp, ss12) # __rxor__ self.assertEqual(set([1, 2]) ^ ss1, (set([2])))
from uuid import uuid1, uuid4 from cassandra.util import OrderedMap, Date, Time, sortedset from tests.integration import get_server_versions PRIMITIVE_DATATYPES = sortedset([ 'ascii', 'bigint', 'blob', 'boolean', 'decimal', 'double', 'float', 'inet', 'int', 'text', 'timestamp', 'timeuuid', 'uuid', 'varchar', 'varint', ]) COLLECTION_TYPES = sortedset([ 'list', 'set', 'map', ])
def test_reversed(self): expected = range(10) self.assertListEqual(list(reversed(sortedset(expected))), list(reversed(expected)))
def test_clear(self): ss = sortedset([1, 2, 3]) ss.clear() self.assertEqual(len(ss), 0)
def test_operators(self): ss1 = sortedset([1]) ss12 = sortedset([1, 2]) # __ne__ self.assertFalse(ss12 != ss12) self.assertFalse(ss12 != sortedset([1, 2])) self.assertTrue(ss12 != sortedset()) # __le__ self.assertTrue(ss1 <= ss12) self.assertTrue(ss12 <= ss12) self.assertFalse(ss12 <= ss1) # __lt__ self.assertTrue(ss1 < ss12) self.assertFalse(ss12 < ss12) self.assertFalse(ss12 < ss1) # __ge__ self.assertFalse(ss1 >= ss12) self.assertTrue(ss12 >= ss12) self.assertTrue(ss12 >= ss1) # __gt__ self.assertFalse(ss1 > ss12) self.assertFalse(ss12 > ss12) self.assertTrue(ss12 > ss1) # __and__ self.assertEqual(ss1 & ss12, ss1) self.assertEqual(ss12 & ss12, ss12) self.assertEqual(ss12 & set(), sortedset()) # __iand__ tmp = sortedset(ss12) tmp &= ss1 self.assertEqual(tmp, ss1) tmp = sortedset(ss1) tmp &= ss12 self.assertEqual(tmp, ss1) tmp = sortedset(ss12) tmp &= ss12 self.assertEqual(tmp, ss12) tmp = sortedset(ss12) tmp &= set() self.assertEqual(tmp, sortedset()) # __rand__ self.assertEqual(set([1]) & ss12, ss1) # __or__ self.assertEqual(ss1 | ss12, ss12) self.assertEqual(ss12 | ss12, ss12) self.assertEqual(ss12 | set(), ss12) self.assertEqual(sortedset() | ss1 | ss12, ss12) # __ior__ tmp = sortedset(ss1) tmp |= ss12 self.assertEqual(tmp, ss12) tmp = sortedset(ss12) tmp |= ss12 self.assertEqual(tmp, ss12) tmp = sortedset(ss12) tmp |= set() self.assertEqual(tmp, ss12) tmp = sortedset() tmp |= ss1 tmp |= ss12 self.assertEqual(tmp, ss12) # __ror__ self.assertEqual(set([1]) | ss12, ss12) # __sub__ self.assertEqual(ss1 - ss12, set()) self.assertEqual(ss12 - ss12, set()) self.assertEqual(ss12 - set(), ss12) self.assertEqual(ss12 - ss1, sortedset([2])) # __isub__ tmp = sortedset(ss1) tmp -= ss12 self.assertEqual(tmp, set()) tmp = sortedset(ss12) tmp -= ss12 self.assertEqual(tmp, set()) tmp = sortedset(ss12) tmp -= set() self.assertEqual(tmp, ss12) tmp = sortedset(ss12) tmp -= ss1 self.assertEqual(tmp, sortedset([2])) # __rsub__ self.assertEqual(set((1,2,3)) - ss12, set((3,))) # __xor__ self.assertEqual(ss1 ^ ss12, set([2])) self.assertEqual(ss12 ^ ss1, set([2])) self.assertEqual(ss12 ^ ss12, set()) self.assertEqual(ss12 ^ set(), ss12) # __ixor__ tmp = sortedset(ss1) tmp ^= ss12 self.assertEqual(tmp, set([2])) tmp = sortedset(ss12) tmp ^= ss1 self.assertEqual(tmp, set([2])) tmp = sortedset(ss12) tmp ^= ss12 self.assertEqual(tmp, set()) tmp = sortedset(ss12) tmp ^= set() self.assertEqual(tmp, ss12) # __rxor__ self.assertEqual(set([1, 2]) ^ ss1, (set([2])))
def test_reduce_pickle(self): ss = sortedset((4,3,2,1)) import pickle s = pickle.dumps(ss) self.assertEqual(pickle.loads(s), ss)
def test_init(self): input = [5, 4, 3, 2, 1, 1, 1] expected = sorted(set(input)) ss = sortedset(input) self.assertEqual(len(ss), len(expected)) self.assertEqual(list(ss), expected)
def test_getitem(self): ss = sortedset(range(3)) for i in range(len(ss)): self.assertEqual(ss[i], i) with self.assertRaises(IndexError): ss[len(ss)]
def test_can_insert_tuples_all_collection_datatypes(self): """ Ensure tuple subtypes are appropriately handled for maps, sets, and lists. """ if self.cass_version < (2, 1, 0): raise unittest.SkipTest("The tuple type was introduced in Cassandra 2.1") c = Cluster(protocol_version=PROTOCOL_VERSION) s = c.connect(self.keyspace_name) # set the row_factory to dict_factory for programmatic access # set the encoder for tuples for the ability to write tuples s.row_factory = dict_factory s.encoder.mapping[tuple] = s.encoder.cql_encode_tuple values = [] # create list values for datatype in PRIMITIVE_DATATYPES: values.append('v_{0} frozen<tuple<list<{1}>>>'.format(len(values), datatype)) # create set values for datatype in PRIMITIVE_DATATYPES: values.append('v_{0} frozen<tuple<set<{1}>>>'.format(len(values), datatype)) # create map values for datatype in PRIMITIVE_DATATYPES: datatype_1 = datatype_2 = datatype if datatype == 'blob': # unhashable type: 'bytearray' datatype_1 = 'ascii' values.append('v_{0} frozen<tuple<map<{1}, {2}>>>'.format(len(values), datatype_1, datatype_2)) # make sure we're testing all non primitive data types in the future if set(COLLECTION_TYPES) != set(['tuple', 'list', 'map', 'set']): raise NotImplemented('Missing datatype not implemented: {}'.format( set(COLLECTION_TYPES) - set(['tuple', 'list', 'map', 'set']) )) # create table s.execute("CREATE TABLE tuple_non_primative (" "k int PRIMARY KEY, " "%s)" % ', '.join(values)) i = 0 # test tuple<list<datatype>> for datatype in PRIMITIVE_DATATYPES: created_tuple = tuple([[get_sample(datatype)]]) s.execute("INSERT INTO tuple_non_primative (k, v_%s) VALUES (0, %s)", (i, created_tuple)) result = s.execute("SELECT v_%s FROM tuple_non_primative WHERE k=0", (i,))[0] self.assertEqual(created_tuple, result['v_%s' % i]) i += 1 # test tuple<set<datatype>> for datatype in PRIMITIVE_DATATYPES: created_tuple = tuple([sortedset([get_sample(datatype)])]) s.execute("INSERT INTO tuple_non_primative (k, v_%s) VALUES (0, %s)", (i, created_tuple)) result = s.execute("SELECT v_%s FROM tuple_non_primative WHERE k=0", (i,))[0] self.assertEqual(created_tuple, result['v_%s' % i]) i += 1 # test tuple<map<datatype, datatype>> for datatype in PRIMITIVE_DATATYPES: if datatype == 'blob': # unhashable type: 'bytearray' created_tuple = tuple([{get_sample('ascii'): get_sample(datatype)}]) else: created_tuple = tuple([{get_sample(datatype): get_sample(datatype)}]) s.execute("INSERT INTO tuple_non_primative (k, v_%s) VALUES (0, %s)", (i, created_tuple)) result = s.execute("SELECT v_%s FROM tuple_non_primative WHERE k=0", (i,))[0] self.assertEqual(created_tuple, result['v_%s' % i]) i += 1 c.shutdown()