def test_column_name_multiple_partition(self): tm = TableMetadata('ks', 'table') cm = ColumnMetadata(tm, self.name, u'int') tm.columns[cm.name] = cm tm.partition_key.append(cm) cm = ColumnMetadata(tm, self.name + 'x', u'int') tm.columns[cm.name] = cm tm.partition_key.append(cm) tm.export_as_string()
def test_protect_names(self): """ Test TableMetadata.protect_names output """ table_metadata = TableMetadata('ks_name', 'table_name') self.assertEqual(table_metadata.protect_names(['tests']), ['tests']) self.assertEqual( table_metadata.protect_names( ['tests', 'test\'s', 'tests ?!@#$%^&*()', '1']), ['tests', "\"test's\"", '"tests ?!@#$%^&*()"', '"1"'])
def test_protect_value(self): """ Test TableMetadata.protect_value output """ table_metadata = TableMetadata('ks_name', 'table_name') self.assertEqual(table_metadata.protect_value(True), "True") self.assertEqual(table_metadata.protect_value(False), "False") self.assertEqual(table_metadata.protect_value(3.14), '3.14') self.assertEqual(table_metadata.protect_value(3), '3') self.assertEqual(table_metadata.protect_value('test'), "'test'") self.assertEqual(table_metadata.protect_value('test\'s'), "'test''s'") self.assertEqual(table_metadata.protect_value(None), 'NULL')
def test_protect_names(self): """ Test TableMetadata.protect_names output """ table_metadata = TableMetadata('ks_name', 'table_name') self.assertEqual(table_metadata.protect_names(['tests']), ['tests']) self.assertEqual(table_metadata.protect_names( [ 'tests', 'test\'s', 'tests ?!@#$%^&*()', '1' ]), [ 'tests', "\"test's\"", '"tests ?!@#$%^&*()"', '"1"' ])
def test_protect_name(self): """ Test TableMetadata.protect_name output """ table_metadata = TableMetadata('ks_name', 'table_name') self.assertEqual(table_metadata.protect_name('tests'), 'tests') self.assertEqual(table_metadata.protect_name('test\'s'), '"test\'s"') self.assertEqual(table_metadata.protect_name('test\'s'), "\"test's\"") self.assertEqual(table_metadata.protect_name('tests ?!@#$%^&*()'), '"tests ?!@#$%^&*()"') self.assertEqual(table_metadata.protect_name('1'), '"1"') self.assertEqual(table_metadata.protect_name('1test'), '"1test"')
def test_is_valid_name(self): """ Test TableMetadata.is_valid_name output """ table_metadata = TableMetadata('ks_name', 'table_name') self.assertEqual(table_metadata.is_valid_name(None), False) self.assertEqual(table_metadata.is_valid_name('test'), True) self.assertEqual(table_metadata.is_valid_name('Test'), False) self.assertEqual(table_metadata.is_valid_name('t_____1'), True) self.assertEqual(table_metadata.is_valid_name('test1'), True) self.assertEqual(table_metadata.is_valid_name('1test1'), False) non_valid_keywords = cassandra.metadata._keywords - cassandra.metadata._unreserved_keywords for keyword in non_valid_keywords: self.assertEqual(table_metadata.is_valid_name(keyword), False)
def test_table_name(self): tm = TableMetadata(self.name, self.name) tm.export_as_string()
def get_table(keyspace, name): table = TableMetadata(keyspace.name, name) # build columns for each data type columns_dict = {} for i in range(len(data_types)): col = column(table, "my_column_%s" % i, data_types[i]) columns_dict[col.name] = col # create some udts address_udt = udt(keyspace, "address", ["street", "number"], ["text", "int"]) user_udt = udt(keyspace, "user", ["name", "address"], ["tuple<text,text>", "frozen<address>"]) collections_udt = udt(keyspace, "collections", ["field1", "field2", "field3", "field4"], ["frozen<user>",\ "frozen<map<frozen<user>, frozen<user>>>",\ "frozen<list<frozen<address>>>",\ "frozen<tuple<frozen<user>, frozen<address>>>"]) # add udts to keyspace keyspace.user_types = {address_udt.name: address_udt,\ user_udt.name: user_udt,\ collections_udt.name: collections_udt} # build column with udt types columns_dict["user"] = column(table, user_udt.name, "frozen<%s>" % user_udt.name) columns_dict["collections"] = column(table, collections_udt.name, "frozen<%s>" % collections_udt.name) # set some pk's and ck's columns = list(columns_dict.values()) partition_keys = [columns[0], columns[1]] clustering_keys = [columns[2], columns[3]] table.columns = columns_dict table.partition_key = partition_keys table.clustering_key = clustering_keys # indexes indexes = {} # add a regular index regular_index = index(keyspace, name, "regular_index_" + name, None, {"target": "my_column_0"}) indexes[regular_index.name] = regular_index # add a CUSTOM index (this must be removed by the anonymizer) custom_index = index( keyspace, name, "custom_index_" + name, "CUSTOM", { "target": "my_column_5", "class_name": "org.apache.cassandra.index.sasi.SASIIndex" }) indexes[custom_index.name] = custom_index table.indexes = indexes # table options compatible with C* 2.1+ table.options = { # set a comment, which must be removed by the anonymizer "comment": "Lorem ipsum", "compaction_strategy_class": "org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy", "max_compaction_threshold": "32", "min_compaction_threshold": "4", "compression_parameters": '{"sstable_compression": ""}' } return table