class TestSuperColumnFamilyMap(unittest.TestCase): def setUp(self): self.map = ColumnFamilyMap(TestUTF8, pool, SCF) def tearDown(self): for scols in self.map.get_range(): for instance in scols.values(): self.map.remove(instance) def instance(self, super_column): instance = TestUTF8() instance.key = uuid.uuid4() instance.super_column = super_column instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) return instance def test_super(self): instance = self.instance('super1') assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) res = self.map.get(instance.key)[instance.super_column] assert_equal(res, instance) assert_equal(self.map.multiget([instance.key])[instance.key][instance.super_column], instance) assert_equal(list(self.map.get_range(start=instance.key, finish=instance.key)), [{instance.super_column: instance}]) def test_super_remove(self): instance1 = self.instance('super1') assert_raises(NotFoundException, self.map.get, instance1.key) self.map.insert(instance1) instance2 = self.instance('super2') self.map.insert(instance2) self.map.remove(instance2) assert_equal(len(self.map.get(instance1.key)), 1) assert_equal(self.map.get(instance1.key)[instance1.super_column], instance1) def test_batch_insert_super(self): instances = [] for i in range(3): instance = self.instance('super_batch%s' % (i + 1)) instances.append(instance) for i in instances: assert_raises(NotFoundException, self.map.get, i.key) self.map.batch_insert(instances) for i in instances: result = self.map.get(i.key) get_instance = result[i.super_column] assert_equal(len(result), 1) assert_equal(get_instance.key, i.key) assert_equal(get_instance.super_column, i.super_column) assert_equal(get_instance.strcol, i.strcol)
class TestSuperColumnFamilyMap: def setUp(self): self.map = ColumnFamilyMap(TestUTF8, scf) def tearDown(self): for key, columns in scf.get_range(): scf.remove(key) def instance(self, key, super_column): instance = TestUTF8() instance.key = key instance.super_column = super_column instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) instance.intstrcol = 8 instance.floatstrcol = 4.6 instance.datetimestrcol = datetime.now().replace(microsecond=0) return instance def test_super(self): instance = self.instance('TestSuperColumnFamilyMap.test_super', 'super1') assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) res = self.map.get(instance.key)[instance.super_column] assert_equal(res, instance) assert_equal( self.map.multiget([instance.key ])[instance.key][instance.super_column], instance) assert_equal( list(self.map.get_range(start=instance.key, finish=instance.key)), [{ instance.super_column: instance }]) def test_super_remove(self): instance1 = self.instance('TestSuperColumnFamilyMap.test_super_remove', 'super1') assert_raises(NotFoundException, self.map.get, instance1.key) self.map.insert(instance1) instance2 = self.instance('TestSuperColumnFamilyMap.test_super_remove', 'super2') self.map.insert(instance2) self.map.remove(instance2) assert_equal(len(self.map.get(instance1.key)), 1) assert_equal( self.map.get(instance1.key)[instance1.super_column], instance1)
class TestSuperColumnFamilyMap: def setUp(self): credentials = {'username': '******', 'password': '******'} self.pool = ConnectionPool(keyspace='Keyspace1', credentials=credentials) self.cf = ColumnFamily(self.pool, 'Super2') self.map = ColumnFamilyMap(TestUTF8, self.cf) def tearDown(self): for key, columns in self.cf.get_range(): self.cf.remove(key) def instance(self, key, super_column): instance = TestUTF8() instance.key = key instance.super_column = super_column instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) instance.intstrcol = 8 instance.floatstrcol = 4.6 instance.datetimestrcol = datetime.now().replace(microsecond=0) return instance def test_super(self): instance = self.instance('TestSuperColumnFamilyMap.test_super', 'super1') assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) res = self.map.get(instance.key)[instance.super_column] assert_equal(res, instance) assert_equal(self.map.multiget([instance.key])[instance.key][instance.super_column], instance) assert_equal(list(self.map.get_range(start=instance.key, finish=instance.key)), [{instance.super_column: instance}])
class TestSuperColumnFamilyMap: def setUp(self): self.map = ColumnFamilyMap(TestUTF8, scf) def tearDown(self): for key, columns in scf.get_range(): scf.remove(key) def instance(self, key, super_column): instance = TestUTF8() instance.key = key instance.super_column = super_column instance.strcol = "1" instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) instance.intstrcol = 8 instance.floatstrcol = 4.6 instance.datetimestrcol = datetime.now().replace(microsecond=0) return instance def test_super(self): instance = self.instance("TestSuperColumnFamilyMap.test_super", "super1") assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) res = self.map.get(instance.key)[instance.super_column] assert_equal(res, instance) assert_equal(self.map.multiget([instance.key])[instance.key][instance.super_column], instance) assert_equal( list(self.map.get_range(start=instance.key, finish=instance.key)), [{instance.super_column: instance}] ) def test_super_remove(self): instance1 = self.instance("TestSuperColumnFamilyMap.test_super_remove", "super1") assert_raises(NotFoundException, self.map.get, instance1.key) self.map.insert(instance1) instance2 = self.instance("TestSuperColumnFamilyMap.test_super_remove", "super2") self.map.insert(instance2) self.map.remove(instance2) assert_equal(len(self.map.get(instance1.key)), 1) assert_equal(self.map.get(instance1.key)[instance1.super_column], instance1)
class TestSuperColumnFamilyMap(unittest.TestCase): def setUp(self): self.map = ColumnFamilyMap(TestUTF8, pool, SCF) def tearDown(self): for scols in self.map.get_range(): for instance in scols.values(): self.map.remove(instance) def instance(self, key, super_column): instance = TestUTF8() instance.key = key instance.super_column = super_column instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) return instance def test_super(self): instance = self.instance('TestSuperColumnFamilyMap.test_super', 'super1') assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) res = self.map.get(instance.key)[instance.super_column] assert_equal(res, instance) assert_equal(self.map.multiget([instance.key])[instance.key][instance.super_column], instance) assert_equal(list(self.map.get_range(start=instance.key, finish=instance.key)), [{instance.super_column: instance}]) def test_super_remove(self): instance1 = self.instance('TestSuperColumnFamilyMap.test_super_remove', 'super1') assert_raises(NotFoundException, self.map.get, instance1.key) self.map.insert(instance1) instance2 = self.instance('TestSuperColumnFamilyMap.test_super_remove', 'super2') self.map.insert(instance2) self.map.remove(instance2) assert_equal(len(self.map.get(instance1.key)), 1) assert_equal(self.map.get(instance1.key)[instance1.super_column], instance1)
class TestSuperColumnFamilyMap: def setUp(self): self.client = connect_thread_local() self.client.login('Keyspace1', {'username': '******', 'password': '******'}) self.cf = ColumnFamily(self.client, 'Keyspace1', 'Super2', write_consistency_level=ConsistencyLevel.ONE, timestamp=self.timestamp, super=True) self.map = ColumnFamilyMap(TestUTF8, self.cf) try: self.timestamp_n = int(self.cf.get('meta')['meta']['timestamp']) except NotFoundException: self.timestamp_n = 0 self.clear() def tearDown(self): self.cf.insert('meta', {'meta': {'timestamp': str(self.timestamp_n)}}) # Since the timestamp passed to Cassandra will be in the same second # with the default timestamp function, causing problems with removing # and inserting (Cassandra doesn't know which is later), we supply our own def timestamp(self): self.timestamp_n += 1 return self.timestamp_n def clear(self): for key, columns in self.cf.get_range(include_timestamp=True): for subcolumns in columns.itervalues(): for value, timestamp in subcolumns.itervalues(): self.timestamp_n = max(self.timestamp_n, timestamp) self.cf.remove(key) def instance(self, key, super_column): instance = TestUTF8() instance.key = key instance.super_column = super_column instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) instance.intstrcol = 8 instance.floatstrcol = 4.6 instance.datetimestrcol = datetime.now().replace(microsecond=0) return instance def test_super(self): instance = self.instance('TestSuperColumnFamilyMap.test_super', 'super1') assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) assert self.map.get(instance.key)[instance.super_column] == instance assert self.map.multiget([instance.key])[instance.key][instance.super_column] == instance assert list(self.map.get_range(start=instance.key, finish=instance.key)) == [{instance.super_column: instance}]
class TestSuperColumnFamilyMap: def setUp(self): credentials = {'username': '******', 'password': '******'} self.client = connect_thread_local('Keyspace1', credentials=credentials) self.cf = ColumnFamily(self.client, 'Super2', write_consistency_level=ConsistencyLevel.ONE, timestamp=self.timestamp, super=True) self.map = ColumnFamilyMap(TestUTF8, self.cf) try: self.timestamp_n = int(self.cf.get('meta')['meta']['timestamp']) except NotFoundException: self.timestamp_n = 0 self.clear() def tearDown(self): self.cf.insert('meta', {'meta': {'timestamp': str(self.timestamp_n)}}) # Since the timestamp passed to Cassandra will be in the same second # with the default timestamp function, causing problems with removing # and inserting (Cassandra doesn't know which is later), we supply our own def timestamp(self): self.timestamp_n += 1 return self.timestamp_n def clear(self): for key, columns in self.cf.get_range(include_timestamp=True): for subcolumns in columns.itervalues(): for value, timestamp in subcolumns.itervalues(): self.timestamp_n = max(self.timestamp_n, timestamp) self.cf.remove(key) def instance(self, key, super_column): instance = TestUTF8() instance.key = key instance.super_column = super_column instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) instance.intstrcol = 8 instance.floatstrcol = 4.6 instance.datetimestrcol = datetime.now().replace(microsecond=0) return instance def test_super(self): instance = self.instance('TestSuperColumnFamilyMap.test_super', 'super1') assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) assert self.map.get(instance.key)[instance.super_column] == instance assert self.map.multiget([instance.key])[instance.key][instance.super_column] == instance assert list(self.map.get_range(start=instance.key, finish=instance.key)) == [{instance.super_column: instance}]
class TestColumnFamilyMap(unittest.TestCase): def setUp(self): self.map = ColumnFamilyMap(TestUTF8, pool, CF) self.indexed_map = ColumnFamilyMap(TestIndex, pool, INDEXED_CF) self.empty_map = ColumnFamilyMap(TestEmpty, pool, CF, raw_columns=True) def tearDown(self): for instance in self.map.get_range(): self.map.remove(instance) for instance in self.indexed_map.get_range(): self.indexed_map.remove(instance) def instance(self): instance = TestUTF8() instance.key = uuid.uuid4() instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) return instance def test_empty(self): key = uuid.uuid4() assert_raises(NotFoundException, self.map.get, key) assert_equal(len(self.map.multiget([key])), 0) def test_insert_get(self): instance = self.instance() assert_raises(NotFoundException, self.map.get, instance.key) ts = self.map.insert(instance) assert_true(isinstance(ts, (int, long))) assert_equal(self.map.get(instance.key), instance) def test_insert_get_omitting_columns(self): """ When omitting columns, pycassa should not try to insert the CassandraType instance on a ColumnFamilyMap object """ instance2 = TestUTF8() instance2.key = uuid.uuid4() instance2.strcol = 'lol' instance2.intcol = 2 assert_raises(NotFoundException, self.map.get, instance2.key) self.map.insert(instance2) ret_inst = self.map.get(instance2.key) assert_equal(ret_inst.key, instance2.key) assert_equal(ret_inst.strcol, instance2.strcol) assert_equal(ret_inst.intcol, instance2.intcol) ## these lines are commented out because, though they should work, wont ## because CassandraTypes are not descriptors when used on a ColumnFamilyMap ## instance, they are merely class attributes that are overwritten at runtime # assert_equal(ret_inst.floatcol, instance2.floatcol) # assert_equal(ret_inst.datetimecol, instance2.datetimecol) # assert_equal(self.map.get(instance2.key), instance2) def test_insert_get_indexed_slices(self): instance1 = TestIndex() instance1.key = 'key1' instance1.birthdate = 1L self.indexed_map.insert(instance1) instance2 = TestIndex() instance2.key = 'key2' instance2.birthdate = 1L self.indexed_map.insert(instance2) instance3 = TestIndex() instance3.key = 'key3' instance3.birthdate = 2L self.indexed_map.insert(instance3) expr = index.create_index_expression(column_name='birthdate', value=2L) clause = index.create_index_clause([expr]) result = self.indexed_map.get_indexed_slices(index_clause=clause) count = 0 for instance in result: assert_equal(instance, instance3) count += 1 assert_equal(count, 1) def test_insert_multiget(self): instance1 = self.instance() instance2 = self.instance() missing_key = uuid.uuid4() self.map.insert(instance1) self.map.insert(instance2) rows = self.map.multiget([instance1.key, instance2.key, missing_key]) assert_equal(len(rows), 2) assert_equal(rows[instance1.key], instance1) assert_equal(rows[instance2.key], instance2) assert_true(missing_key not in rows) def test_insert_get_range(self): if sys_man.describe_partitioner() == 'RandomPartitioner': raise SkipTest('Cannot use RandomPartitioner for this test') instances = [self.instance() for i in range(5)] instances = sorted(instances, key=lambda instance: instance.key) for instance in instances: self.map.insert(instance) rows = list(self.map.get_range(start=instances[0].key, finish=instances[-1].key)) assert_equal(len(rows), len(instances)) assert_equal(rows, instances) def test_remove(self): instance = self.instance() self.map.insert(instance) self.map.remove(instance) assert_raises(NotFoundException, self.map.get, instance.key) def test_does_not_insert_extra_column(self): instance = self.instance() instance.othercol = 'Test' self.map.insert(instance) get_instance = self.map.get(instance.key) assert_equal(get_instance.strcol, instance.strcol) assert_equal(get_instance.intcol, instance.intcol) assert_equal(get_instance.floatcol, instance.floatcol) assert_equal(get_instance.datetimecol, instance.datetimecol) assert_raises(AttributeError, getattr, get_instance, 'othercol') def test_has_defaults(self): key = uuid.uuid4() ColumnFamily.insert(self.map, key, {'strcol': '1'}) instance = self.map.get(key) assert_equal(instance.intcol, TestUTF8.intcol.default) assert_equal(instance.floatcol, TestUTF8.floatcol.default) assert_equal(instance.datetimecol, TestUTF8.datetimecol.default) def test_batch_insert(self): instances = [] for i in range(3): instance = TestUTF8() instance.key = uuid.uuid4() instance.strcol = 'instance%s' % (i + 1) instances.append(instance) for i in instances: assert_raises(NotFoundException, self.map.get, i.key) self.map.batch_insert(instances) for i in instances: get_instance = self.map.get(i.key) assert_equal(get_instance.key, i.key) assert_equal(get_instance.strcol, i.strcol)
class TestColumnFamilyMap: def setUp(self): self.map = ColumnFamilyMap(TestUTF8, cf) self.indexed_map = ColumnFamilyMap(TestIndex, indexed_cf) self.empty_map = ColumnFamilyMap(TestEmpty, cf, raw_columns=True) def tearDown(self): for key, columns in cf.get_range(): cf.remove(key) for key, columns in indexed_cf.get_range(): cf.remove(key) def instance(self, key): instance = TestUTF8() instance.key = key instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) instance.intstrcol = 8 instance.floatstrcol = 4.6 instance.datetimestrcol = datetime.now().replace(microsecond=0) return instance def test_empty(self): key = 'TestColumnFamilyMap.test_empty' assert_raises(NotFoundException, self.map.get, key) assert_equal(len(self.map.multiget([key])), 0) def test_insert_get(self): instance = self.instance('TestColumnFamilyMap.test_insert_get') assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) assert_equal(self.map.get(instance.key), instance) assert_equal(self.empty_map.get(instance.key).raw_columns['intstrcol'], str(instance.intstrcol)) def test_insert_get_indexed_slices(self): instance1 = TestIndex() instance1.key = 'key1' instance1.birthdate = 1L self.indexed_map.insert(instance1) instance2 = TestIndex() instance2.key = 'key2' instance2.birthdate = 1L self.indexed_map.insert(instance2) instance3 = TestIndex() instance3.key = 'key3' instance3.birthdate = 2L self.indexed_map.insert(instance3) expr = index.create_index_expression(column_name='birthdate', value=2L) clause = index.create_index_clause([expr]) result = self.indexed_map.get_indexed_slices(index_clause=clause) assert_equal(len(result), 1) assert_equal(result.get('key3'), instance3) def test_insert_multiget(self): instance1 = self.instance('TestColumnFamilyMap.test_insert_multiget1') instance2 = self.instance('TestColumnFamilyMap.test_insert_multiget2') missing_key = 'TestColumnFamilyMap.test_insert_multiget3' self.map.insert(instance1) self.map.insert(instance2) rows = self.map.multiget([instance1.key, instance2.key, missing_key]) assert_equal(len(rows), 2) assert_equal(rows[instance1.key], instance1) assert_equal(rows[instance2.key], instance2) assert_true(missing_key not in rows) assert_equal(self.empty_map.multiget([instance1.key])[instance1.key].raw_columns['intstrcol'], str(instance1.intstrcol)) def test_insert_get_range(self): if sys_man.describe_partitioner() == 'RandomPartitioner': raise SkipTest('Cannot use RandomPartitioner for this test') instances = [] for i in xrange(5): instance = self.instance('TestColumnFamilyMap.test_insert_get_range%s' % i) instances.append(instance) for instance in instances: self.map.insert(instance) rows = list(self.map.get_range(start=instances[0].key, finish=instances[-1].key)) assert_equal(len(rows), len(instances)) assert_equal(rows, instances) assert_equal(list(self.empty_map.get_range(start=instances[0].key, finish=instances[0].key))[0].raw_columns['intstrcol'], str(instances[0].intstrcol)) def test_remove(self): instance = self.instance('TestColumnFamilyMap.test_remove') self.map.insert(instance) self.map.remove(instance) assert_raises(NotFoundException, self.map.get, instance.key) def test_does_not_insert_extra_column(self): instance = self.instance('TestColumnFamilyMap.test_does_not_insert_extra_column') instance.othercol = 'Test' self.map.insert(instance) get_instance = self.map.get(instance.key) assert_equal(get_instance.strcol, instance.strcol) assert_equal(get_instance.intcol, instance.intcol) assert_equal(get_instance.floatcol, instance.floatcol) assert_equal(get_instance.datetimecol, instance.datetimecol) assert_raises(AttributeError, getattr, get_instance, 'othercol') def test_has_defaults(self): key = 'TestColumnFamilyMap.test_has_defaults' cf.insert(key, {'strcol': '1'}) instance = self.map.get(key) assert_equal(instance.intcol, TestUTF8.intcol.default) assert_equal(instance.floatcol, TestUTF8.floatcol.default) assert_equal(instance.datetimecol, TestUTF8.datetimecol.default) assert_equal(instance.intstrcol, TestUTF8.intstrcol.default) assert_equal(instance.floatstrcol, TestUTF8.floatstrcol.default) assert_equal(instance.datetimestrcol, TestUTF8.datetimestrcol.default)
class TestColumnFamilyMap: def setUp(self): self.map = ColumnFamilyMap(TestUTF8, pool, CF) self.indexed_map = ColumnFamilyMap(TestIndex, pool, INDEXED_CF) self.empty_map = ColumnFamilyMap(TestEmpty, pool, CF, raw_columns=True) def tearDown(self): for instance in self.map.get_range(): self.map.remove(instance) for instance in self.indexed_map.get_range(): self.indexed_map.remove(instance) def instance(self, key): instance = TestUTF8() instance.key = key instance.strcol = "1" instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) return instance def test_empty(self): key = "TestColumnFamilyMap.test_empty" assert_raises(NotFoundException, self.map.get, key) assert_equal(len(self.map.multiget([key])), 0) def test_insert_get(self): instance = self.instance("TestColumnFamilyMap.test_insert_get") assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) assert_equal(self.map.get(instance.key), instance) def test_insert_get_indexed_slices(self): instance1 = TestIndex() instance1.key = "key1" instance1.birthdate = 1L self.indexed_map.insert(instance1) instance2 = TestIndex() instance2.key = "key2" instance2.birthdate = 1L self.indexed_map.insert(instance2) instance3 = TestIndex() instance3.key = "key3" instance3.birthdate = 2L self.indexed_map.insert(instance3) expr = index.create_index_expression(column_name="birthdate", value=2L) clause = index.create_index_clause([expr]) result = self.indexed_map.get_indexed_slices(index_clause=clause) assert_equal(len(result), 1) assert_equal(result.get("key3"), instance3) def test_insert_multiget(self): instance1 = self.instance("TestColumnFamilyMap.test_insert_multiget1") instance2 = self.instance("TestColumnFamilyMap.test_insert_multiget2") missing_key = "TestColumnFamilyMap.test_insert_multiget3" self.map.insert(instance1) self.map.insert(instance2) rows = self.map.multiget([instance1.key, instance2.key, missing_key]) assert_equal(len(rows), 2) assert_equal(rows[instance1.key], instance1) assert_equal(rows[instance2.key], instance2) assert_true(missing_key not in rows) def test_insert_get_range(self): if sys_man.describe_partitioner() == "RandomPartitioner": raise SkipTest("Cannot use RandomPartitioner for this test") instances = [] for i in xrange(5): instance = self.instance("TestColumnFamilyMap.test_insert_get_range%s" % i) instances.append(instance) for instance in instances: self.map.insert(instance) rows = list(self.map.get_range(start=instances[0].key, finish=instances[-1].key)) assert_equal(len(rows), len(instances)) assert_equal(rows, instances) def test_remove(self): instance = self.instance("TestColumnFamilyMap.test_remove") self.map.insert(instance) self.map.remove(instance) assert_raises(NotFoundException, self.map.get, instance.key) def test_does_not_insert_extra_column(self): instance = self.instance("TestColumnFamilyMap.test_does_not_insert_extra_column") instance.othercol = "Test" self.map.insert(instance) get_instance = self.map.get(instance.key) assert_equal(get_instance.strcol, instance.strcol) assert_equal(get_instance.intcol, instance.intcol) assert_equal(get_instance.floatcol, instance.floatcol) assert_equal(get_instance.datetimecol, instance.datetimecol) assert_raises(AttributeError, getattr, get_instance, "othercol") def test_has_defaults(self): key = "TestColumnFamilyMap.test_has_defaults" ColumnFamily.insert(self.map, key, {"strcol": "1"}) instance = self.map.get(key) assert_equal(instance.intcol, TestUTF8.intcol.default) assert_equal(instance.floatcol, TestUTF8.floatcol.default) assert_equal(instance.datetimecol, TestUTF8.datetimecol.default)
class TestColumnFamilyMap: def setUp(self): self.map = ColumnFamilyMap(TestUTF8, cf) self.indexed_map = ColumnFamilyMap(TestIndex, indexed_cf) self.empty_map = ColumnFamilyMap(TestEmpty, cf, raw_columns=True) def tearDown(self): for key, columns in cf.get_range(): cf.remove(key) for key, columns in indexed_cf.get_range(): cf.remove(key) def instance(self, key): instance = TestUTF8() instance.key = key instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) instance.intstrcol = 8 instance.floatstrcol = 4.6 instance.datetimestrcol = datetime.now().replace(microsecond=0) return instance def test_empty(self): key = 'TestColumnFamilyMap.test_empty' assert_raises(NotFoundException, self.map.get, key) assert_equal(len(self.map.multiget([key])), 0) def test_insert_get(self): instance = self.instance('TestColumnFamilyMap.test_insert_get') assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) assert_equal(self.map.get(instance.key), instance) assert_equal( self.empty_map.get(instance.key).raw_columns['intstrcol'], str(instance.intstrcol)) def test_insert_get_indexed_slices(self): instance1 = TestIndex() instance1.key = 'key1' instance1.birthdate = 1L self.indexed_map.insert(instance1) instance2 = TestIndex() instance2.key = 'key2' instance2.birthdate = 1L self.indexed_map.insert(instance2) instance3 = TestIndex() instance3.key = 'key3' instance3.birthdate = 2L self.indexed_map.insert(instance3) expr = index.create_index_expression(column_name='birthdate', value=2L) clause = index.create_index_clause([expr]) result = self.indexed_map.get_indexed_slices(index_clause=clause) assert_equal(len(result), 1) assert_equal(result.get('key3'), instance3) def test_insert_multiget(self): instance1 = self.instance('TestColumnFamilyMap.test_insert_multiget1') instance2 = self.instance('TestColumnFamilyMap.test_insert_multiget2') missing_key = 'TestColumnFamilyMap.test_insert_multiget3' self.map.insert(instance1) self.map.insert(instance2) rows = self.map.multiget([instance1.key, instance2.key, missing_key]) assert_equal(len(rows), 2) assert_equal(rows[instance1.key], instance1) assert_equal(rows[instance2.key], instance2) assert_true(missing_key not in rows) assert_equal( self.empty_map.multiget( [instance1.key])[instance1.key].raw_columns['intstrcol'], str(instance1.intstrcol)) def test_insert_get_range(self): if sys_man.describe_partitioner() == 'RandomPartitioner': raise SkipTest('Cannot use RandomPartitioner for this test') instances = [] for i in xrange(5): instance = self.instance( 'TestColumnFamilyMap.test_insert_get_range%s' % i) instances.append(instance) for instance in instances: self.map.insert(instance) rows = list( self.map.get_range(start=instances[0].key, finish=instances[-1].key)) assert_equal(len(rows), len(instances)) assert_equal(rows, instances) assert_equal( list( self.empty_map.get_range( start=instances[0].key, finish=instances[0].key))[0].raw_columns['intstrcol'], str(instances[0].intstrcol)) def test_remove(self): instance = self.instance('TestColumnFamilyMap.test_remove') self.map.insert(instance) self.map.remove(instance) assert_raises(NotFoundException, self.map.get, instance.key) def test_does_not_insert_extra_column(self): instance = self.instance( 'TestColumnFamilyMap.test_does_not_insert_extra_column') instance.othercol = 'Test' self.map.insert(instance) get_instance = self.map.get(instance.key) assert_equal(get_instance.strcol, instance.strcol) assert_equal(get_instance.intcol, instance.intcol) assert_equal(get_instance.floatcol, instance.floatcol) assert_equal(get_instance.datetimecol, instance.datetimecol) assert_raises(AttributeError, getattr, get_instance, 'othercol') def test_has_defaults(self): key = 'TestColumnFamilyMap.test_has_defaults' cf.insert(key, {'strcol': '1'}) instance = self.map.get(key) assert_equal(instance.intcol, TestUTF8.intcol.default) assert_equal(instance.floatcol, TestUTF8.floatcol.default) assert_equal(instance.datetimecol, TestUTF8.datetimecol.default) assert_equal(instance.intstrcol, TestUTF8.intstrcol.default) assert_equal(instance.floatstrcol, TestUTF8.floatstrcol.default) assert_equal(instance.datetimestrcol, TestUTF8.datetimestrcol.default)
class TestColumnFamilyMap: def setUp(self): self.client = connect() self.client.login('Keyspace1', { 'username': '******', 'password': '******' }) self.cf = ColumnFamily(self.client, 'Keyspace1', 'Standard2', write_consistency_level=ConsistencyLevel.ONE, timestamp=self.timestamp) self.map = ColumnFamilyMap(TestUTF8, self.cf) self.empty_map = ColumnFamilyMap(TestEmpty, self.cf, raw_columns=True) try: self.timestamp_n = int(self.cf.get('meta')['timestamp']) except NotFoundException: self.timestamp_n = 0 self.clear() def tearDown(self): self.cf.insert('meta', {'timestamp': str(self.timestamp_n)}) # Since the timestamp passed to Cassandra will be in the same second # with the default timestamp function, causing problems with removing # and inserting (Cassandra doesn't know which is later), we supply our own def timestamp(self): self.timestamp_n += 1 return self.timestamp_n def clear(self): for key, columns in self.cf.get_range(include_timestamp=True): for value, timestamp in columns.itervalues(): self.timestamp_n = max(self.timestamp_n, timestamp) self.cf.remove(key) def instance(self, key): instance = TestUTF8() instance.key = key instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) instance.intstrcol = 8 instance.floatstrcol = 4.6 instance.datetimestrcol = datetime.now().replace(microsecond=0) return instance def test_will_not_insert_none(self): for column in ('strcol', 'intcol', 'floatcol', 'datetimecol', 'intstrcol', 'floatstrcol', 'datetimestrcol'): instance = self.instance( 'TestColumnFamilyMap.test_will_not_insert_none') setattr(instance, column, None) assert_raises(TypeError, self.map.insert, instance) def test_empty(self): key = 'TestColumnFamilyMap.test_empty' assert_raises(NotFoundException, self.map.get, key) assert len(self.map.multiget([key])) == 0 def test_insert_get(self): instance = self.instance('TestColumnFamilyMap.test_insert_get') assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) assert self.map.get(instance.key) == instance assert self.empty_map.get( instance.key).raw_columns['intstrcol'] == str(instance.intstrcol) def test_insert_multiget(self): instance1 = self.instance('TestColumnFamilyMap.test_insert_multiget1') instance2 = self.instance('TestColumnFamilyMap.test_insert_multiget2') missing_key = 'TestColumnFamilyMap.test_insert_multiget3' self.map.insert(instance1) self.map.insert(instance2) rows = self.map.multiget([instance1.key, instance2.key, missing_key]) assert len(rows) == 2 assert rows[instance1.key] == instance1 assert rows[instance2.key] == instance2 assert missing_key not in rows assert self.empty_map.multiget([ instance1.key ])[instance1.key].raw_columns['intstrcol'] == str(instance1.intstrcol) def test_insert_get_count(self): instance = self.instance('TestColumnFamilyMap.test_insert_get_count') self.map.insert(instance) assert self.map.get_count(instance.key) == 7 def test_insert_get_range(self): instances = [] for i in xrange(5): instance = self.instance( 'TestColumnFamilyMap.test_insert_get_range%s' % i) instances.append(instance) for instance in instances: self.map.insert(instance) rows = list( self.map.get_range(start=instances[0].key, finish=instances[-1].key)) assert len(rows) == len(instances) assert rows == instances assert list( self.empty_map.get_range( start=instances[0].key, finish=instances[0].key))[0].raw_columns['intstrcol'] == str( instances[0].intstrcol) def test_remove(self): instance = self.instance('TestColumnFamilyMap.test_remove') self.map.insert(instance) self.map.remove(instance) assert_raises(NotFoundException, self.map.get, instance.key) def test_does_not_insert_extra_column(self): instance = self.instance( 'TestColumnFamilyMap.test_does_not_insert_extra_column') instance.othercol = 'Test' self.map.insert(instance) get_instance = self.map.get(instance.key) assert get_instance.strcol == instance.strcol assert get_instance.intcol == instance.intcol assert get_instance.floatcol == instance.floatcol assert get_instance.datetimecol == instance.datetimecol assert_raises(AttributeError, getattr, get_instance, 'othercol') def test_has_defaults(self): key = 'TestColumnFamilyMap.test_has_defaults' self.cf.insert(key, {'strcol': '1'}) instance = self.map.get(key) assert instance.intcol == TestUTF8.intcol.default assert instance.floatcol == TestUTF8.floatcol.default assert instance.datetimecol == TestUTF8.datetimecol.default assert instance.intstrcol == TestUTF8.intstrcol.default assert instance.floatstrcol == TestUTF8.floatstrcol.default assert instance.datetimestrcol == TestUTF8.datetimestrcol.default
class TestColumnFamilyMap: def setUp(self): credentials = {'username': '******', 'password': '******'} self.pool = ConnectionPool(keyspace='Keyspace1', credentials=credentials) self.cf = ColumnFamily(self.pool, 'Standard2', autopack_names=False, autopack_values=False) self.indexed_cf = ColumnFamily(self.pool, 'Indexed1', autopack_names=False, autopack_values=False) self.map = ColumnFamilyMap(TestUTF8, self.cf) self.indexed_map = ColumnFamilyMap(TestIndex, self.indexed_cf) self.empty_map = ColumnFamilyMap(TestEmpty, self.cf, raw_columns=True) def tearDown(self): for key, columns in self.cf.get_range(): self.cf.remove(key) for key, columns in self.indexed_cf.get_range(): self.cf.remove(key) def instance(self, key): instance = TestUTF8() instance.key = key instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) instance.intstrcol = 8 instance.floatstrcol = 4.6 instance.datetimestrcol = datetime.now().replace(microsecond=0) return instance def test_empty(self): key = 'TestColumnFamilyMap.test_empty' assert_raises(NotFoundException, self.map.get, key) assert_equal(len(self.map.multiget([key])), 0) def test_insert_get(self): instance = self.instance('TestColumnFamilyMap.test_insert_get') assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) assert_equal(self.map.get(instance.key), instance) assert_equal(self.empty_map.get(instance.key).raw_columns['intstrcol'], str(instance.intstrcol)) def test_insert_get_indexed_slices(self): instance = TestIndex() instance.key = 'key' instance.birthdate = 1L self.indexed_map.insert(instance) instance.key = 'key2' self.indexed_map.insert(instance) instance.key = 'key3' self.indexed_map.insert(instance) expr = index.create_index_expression(column_name='birthdate', value=1L) clause = index.create_index_clause([expr]) result = self.indexed_map.get_indexed_slices(instance, index_clause=clause) assert_equal(len(result), 3) assert_equal(result.get('key3'), instance) def test_insert_multiget(self): instance1 = self.instance('TestColumnFamilyMap.test_insert_multiget1') instance2 = self.instance('TestColumnFamilyMap.test_insert_multiget2') missing_key = 'TestColumnFamilyMap.test_insert_multiget3' self.map.insert(instance1) self.map.insert(instance2) rows = self.map.multiget([instance1.key, instance2.key, missing_key]) assert_equal(len(rows), 2) assert_equal(rows[instance1.key], instance1) assert_equal(rows[instance2.key], instance2) assert_true(missing_key not in rows) assert_equal(self.empty_map.multiget([instance1.key])[instance1.key].raw_columns['intstrcol'], str(instance1.intstrcol)) def test_insert_get_count(self): instance = self.instance('TestColumnFamilyMap.test_insert_get_count') self.map.insert(instance) assert_equal(self.map.get_count(instance.key), 7) def test_insert_get_range(self): instances = [] for i in xrange(5): instance = self.instance('TestColumnFamilyMap.test_insert_get_range%s' % i) instances.append(instance) for instance in instances: self.map.insert(instance) rows = list(self.map.get_range(start=instances[0].key, finish=instances[-1].key)) assert_equal(len(rows), len(instances)) assert_equal(rows, instances) assert_equal(list(self.empty_map.get_range(start=instances[0].key, finish=instances[0].key))[0].raw_columns['intstrcol'], str(instances[0].intstrcol)) def test_remove(self): instance = self.instance('TestColumnFamilyMap.test_remove') self.map.insert(instance) self.map.remove(instance) assert_raises(NotFoundException, self.map.get, instance.key) def test_does_not_insert_extra_column(self): instance = self.instance('TestColumnFamilyMap.test_does_not_insert_extra_column') instance.othercol = 'Test' self.map.insert(instance) get_instance = self.map.get(instance.key) assert_equal(get_instance.strcol, instance.strcol) assert_equal(get_instance.intcol, instance.intcol) assert_equal(get_instance.floatcol, instance.floatcol) assert_equal(get_instance.datetimecol, instance.datetimecol) assert_raises(AttributeError, getattr, get_instance, 'othercol') def test_has_defaults(self): key = 'TestColumnFamilyMap.test_has_defaults' self.cf.insert(key, {'strcol': '1'}) instance = self.map.get(key) assert_equal(instance.intcol, TestUTF8.intcol.default) assert_equal(instance.floatcol, TestUTF8.floatcol.default) assert_equal(instance.datetimecol, TestUTF8.datetimecol.default) assert_equal(instance.intstrcol, TestUTF8.intstrcol.default) assert_equal(instance.floatstrcol, TestUTF8.floatstrcol.default) assert_equal(instance.datetimestrcol, TestUTF8.datetimestrcol.default)
class TestColumnFamilyMap: def setUp(self): self.client = connect() self.client.login('Keyspace1', {'username': '******', 'password': '******'}) self.cf = ColumnFamily(self.client, 'Keyspace1', 'Standard2', write_consistency_level=ConsistencyLevel.ONE, timestamp=self.timestamp) self.map = ColumnFamilyMap(TestUTF8, self.cf) self.empty_map = ColumnFamilyMap(TestEmpty, self.cf, raw_columns=True) try: self.timestamp_n = int(self.cf.get('meta')['timestamp']) except NotFoundException: self.timestamp_n = 0 self.clear() def tearDown(self): self.cf.insert('meta', {'timestamp': str(self.timestamp_n)}) # Since the timestamp passed to Cassandra will be in the same second # with the default timestamp function, causing problems with removing # and inserting (Cassandra doesn't know which is later), we supply our own def timestamp(self): self.timestamp_n += 1 return self.timestamp_n def clear(self): for key, columns in self.cf.get_range(include_timestamp=True): for value, timestamp in columns.itervalues(): self.timestamp_n = max(self.timestamp_n, timestamp) self.cf.remove(key) def instance(self, key): instance = TestUTF8() instance.key = key instance.strcol = '1' instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) instance.intstrcol = 8 instance.floatstrcol = 4.6 instance.datetimestrcol = datetime.now().replace(microsecond=0) return instance def test_will_not_insert_none(self): for column in ('strcol', 'intcol', 'floatcol', 'datetimecol', 'intstrcol', 'floatstrcol', 'datetimestrcol'): instance = self.instance('TestColumnFamilyMap.test_will_not_insert_none') setattr(instance, column, None) assert_raises(TypeError, self.map.insert, instance) def test_empty(self): key = 'TestColumnFamilyMap.test_empty' assert_raises(NotFoundException, self.map.get, key) assert len(self.map.multiget([key])) == 0 def test_insert_get(self): instance = self.instance('TestColumnFamilyMap.test_insert_get') assert_raises(NotFoundException, self.map.get, instance.key) self.map.insert(instance) assert self.map.get(instance.key) == instance assert self.empty_map.get(instance.key).raw_columns['intstrcol'] == str(instance.intstrcol) def test_insert_multiget(self): instance1 = self.instance('TestColumnFamilyMap.test_insert_multiget1') instance2 = self.instance('TestColumnFamilyMap.test_insert_multiget2') missing_key = 'TestColumnFamilyMap.test_insert_multiget3' self.map.insert(instance1) self.map.insert(instance2) rows = self.map.multiget([instance1.key, instance2.key, missing_key]) assert len(rows) == 2 assert rows[instance1.key] == instance1 assert rows[instance2.key] == instance2 assert missing_key not in rows assert self.empty_map.multiget([instance1.key])[instance1.key].raw_columns['intstrcol'] == str(instance1.intstrcol) def test_insert_get_count(self): instance = self.instance('TestColumnFamilyMap.test_insert_get_count') self.map.insert(instance) assert self.map.get_count(instance.key) == 7 def test_insert_get_range(self): instances = [] for i in xrange(5): instance = self.instance('TestColumnFamilyMap.test_insert_get_range%s' % i) instances.append(instance) for instance in instances: self.map.insert(instance) rows = list(self.map.get_range(start=instances[0].key, finish=instances[-1].key)) assert len(rows) == len(instances) assert rows == instances assert list(self.empty_map.get_range(start=instances[0].key, finish=instances[0].key))[0].raw_columns['intstrcol'] == str(instances[0].intstrcol) def test_remove(self): instance = self.instance('TestColumnFamilyMap.test_remove') self.map.insert(instance) self.map.remove(instance) assert_raises(NotFoundException, self.map.get, instance.key) def test_does_not_insert_extra_column(self): instance = self.instance('TestColumnFamilyMap.test_does_not_insert_extra_column') instance.othercol = 'Test' self.map.insert(instance) get_instance = self.map.get(instance.key) assert get_instance.strcol == instance.strcol assert get_instance.intcol == instance.intcol assert get_instance.floatcol == instance.floatcol assert get_instance.datetimecol == instance.datetimecol assert_raises(AttributeError, getattr, get_instance, 'othercol') def test_has_defaults(self): key = 'TestColumnFamilyMap.test_has_defaults' self.cf.insert(key, {'strcol': '1'}) instance = self.map.get(key) assert instance.intcol == TestUTF8.intcol.default assert instance.floatcol == TestUTF8.floatcol.default assert instance.datetimecol == TestUTF8.datetimecol.default assert instance.intstrcol == TestUTF8.intstrcol.default assert instance.floatstrcol == TestUTF8.floatstrcol.default assert instance.datetimestrcol == TestUTF8.datetimestrcol.default
class TestColumnFamilyMap(unittest.TestCase): def setUp(self): self.map = ColumnFamilyMap(TestUTF8, pool, CF) self.indexed_map = ColumnFamilyMap(TestIndex, pool, INDEXED_CF) self.empty_map = ColumnFamilyMap(TestEmpty, pool, CF, raw_columns=True) def tearDown(self): for instance in self.map.get_range(): self.map.remove(instance) for instance in self.indexed_map.get_range(): self.indexed_map.remove(instance) def instance(self, key): instance = TestUTF8() instance.key = key instance.strcol = "1" instance.intcol = 2 instance.floatcol = 3.5 instance.datetimecol = datetime.now().replace(microsecond=0) return instance def test_empty(self): key = "TestColumnFamilyMap.test_empty" assert_raises(NotFoundException, self.map.get, key) assert_equal(len(self.map.multiget([key])), 0) def test_insert_get(self): instance = self.instance("TestColumnFamilyMap.test_insert_get") assert_raises(NotFoundException, self.map.get, instance.key) ts = self.map.insert(instance) assert_true(isinstance(ts, int)) assert_equal(self.map.get(instance.key), instance) def test_insert_get_omitting_columns(self): r""" When omitting columns, pycassa should not try to insert the CassandraType instance on a ColumnFamilyMap object """ instance2 = TestUTF8() instance2.key = "TestColumnFamilyMap.test_insert_get_2" instance2.strcol = "lol" instance2.intcol = 2 assert_raises(NotFoundException, self.map.get, instance2.key) self.map.insert(instance2) ret_inst = self.map.get(instance2.key) assert_equal(ret_inst.key, instance2.key) assert_equal(ret_inst.strcol, instance2.strcol) assert_equal(ret_inst.intcol, instance2.intcol) ## these lines are commented out because, though they should work, wont ## because CassandraTypes are not descriptors when used on a ColumnFamilyMap ## instance, they are merely class attributes that are overwritten at runtime # assert_equal(ret_inst.floatcol, instance2.floatcol) # assert_equal(ret_inst.datetimecol, instance2.datetimecol) # assert_equal(self.map.get(instance2.key), instance2) def test_insert_get_indexed_slices(self): instance1 = TestIndex() instance1.key = "key1" instance1.birthdate = 1L self.indexed_map.insert(instance1) instance2 = TestIndex() instance2.key = "key2" instance2.birthdate = 1L self.indexed_map.insert(instance2) instance3 = TestIndex() instance3.key = "key3" instance3.birthdate = 2L self.indexed_map.insert(instance3) expr = index.create_index_expression(column_name="birthdate", value=2L) clause = index.create_index_clause([expr]) result = self.indexed_map.get_indexed_slices(index_clause=clause) count = 0 for instance in result: assert_equal(instance, instance3) count += 1 assert_equal(count, 1) def test_insert_multiget(self): instance1 = self.instance("TestColumnFamilyMap.test_insert_multiget1") instance2 = self.instance("TestColumnFamilyMap.test_insert_multiget2") missing_key = "TestColumnFamilyMap.test_insert_multiget3" self.map.insert(instance1) self.map.insert(instance2) rows = self.map.multiget([instance1.key, instance2.key, missing_key]) assert_equal(len(rows), 2) assert_equal(rows[instance1.key], instance1) assert_equal(rows[instance2.key], instance2) assert_true(missing_key not in rows) def test_insert_get_range(self): if sys_man.describe_partitioner() == "RandomPartitioner": raise SkipTest("Cannot use RandomPartitioner for this test") instances = [] for i in xrange(5): instance = self.instance("TestColumnFamilyMap.test_insert_get_range%s" % i) instances.append(instance) for instance in instances: self.map.insert(instance) rows = list(self.map.get_range(start=instances[0].key, finish=instances[-1].key)) assert_equal(len(rows), len(instances)) assert_equal(rows, instances) def test_remove(self): instance = self.instance("TestColumnFamilyMap.test_remove") self.map.insert(instance) self.map.remove(instance) assert_raises(NotFoundException, self.map.get, instance.key) def test_does_not_insert_extra_column(self): instance = self.instance("TestColumnFamilyMap.test_does_not_insert_extra_column") instance.othercol = "Test" self.map.insert(instance) get_instance = self.map.get(instance.key) assert_equal(get_instance.strcol, instance.strcol) assert_equal(get_instance.intcol, instance.intcol) assert_equal(get_instance.floatcol, instance.floatcol) assert_equal(get_instance.datetimecol, instance.datetimecol) assert_raises(AttributeError, getattr, get_instance, "othercol") def test_has_defaults(self): key = "TestColumnFamilyMap.test_has_defaults" ColumnFamily.insert(self.map, key, {"strcol": "1"}) instance = self.map.get(key) assert_equal(instance.intcol, TestUTF8.intcol.default) assert_equal(instance.floatcol, TestUTF8.floatcol.default) assert_equal(instance.datetimecol, TestUTF8.datetimecol.default)