def test_anonymize_simple_udt(self): schema = get_schema() keyspace = schema.keyspaces[0] udt = keyspace.user_types["address"] anonymize.anonymize_udts([udt]) self.assertTrue(udt.name.startswith("udt")) self.assertPrefixes(udt.field_names, "fld") self.assertEqual(udt.field_types, ["text", "int"])
def test_anonymize_column(self): schema = get_schema() keyspace = schema.keyspaces[0] table = keyspace.tables["my_table_0"] column = table.columns["my_column_0"] anonymize.anonymize_column(column) self.assertTrue(column.name.startswith("col")) self.assertEqual(column.cql_type, "ascii")
def test_table_comment_is_removed(self): schema = get_schema() keyspace = schema.keyspaces[0] tables = list(keyspace.tables.values()) table = tables[0] self.assertTrue(table.options["comment"] == "Lorem ipsum") anonymize.anonymize_table(table) self.assertTrue(table.options["comment"] == "")
def test_anonymize_index(self): schema = get_schema() keyspace = schema.keyspaces[0] table = keyspace.tables["my_table_0"] index = table.indexes["regular_index_my_table_0"] anonymize.anonymize_index(index) # check the index name has been anonymized self.assertTrue(index.name.startswith("idx")) # check the index'es target column has been anonymized self.assertTrue(index.index_options["target"].startswith("col"))
def test_anonymize_nested_udt(self): schema = get_schema() keyspace = schema.keyspaces[0] address = keyspace.user_types["address"] user = keyspace.user_types["user"] anonymize.anonymize_udts([address, user]) self.assertTrue(user.name.startswith("udt")) self.assertPrefixes(user.field_names, "fld") self.assertEqual(user.field_types, ["tuple<text,text>", "frozen<udt_0>"])
def test_anonymize_keyspace(self): schema = get_schema() keyspace = schema.keyspaces[0] anonymize.anonymize_keyspace(keyspace) self.assertEqual(keyspace.name, "ks_0") self.assertTrue(keyspace.durable_writes) self.assertEqual( keyspace.replication_strategy.replication_factor_info.all_replicas, 1) tables = [table.name for table in keyspace.tables.values()] self.assertPrefixes(tables, "tbl")
def test_anonymize_complex_udt(self): schema = get_schema() keyspace = schema.keyspaces[0] address = keyspace.user_types["address"] user = keyspace.user_types["user"] collections = keyspace.user_types["collections"] anonymize.anonymize_udts([address, user, collections]) self.assertTrue(collections.name.startswith("udt")) self.assertPrefixes(collections.field_names, "fld") self.assertEqual(collections.field_types, ["frozen<udt_1>",\ "frozen<map<frozen<udt_1>, frozen<udt_1>>>",\ "frozen<list<frozen<udt_0>>>",\ "frozen<tuple<frozen<udt_1>, frozen<udt_0>>>"])
def test_anonymize_table(self): schema = get_schema() keyspace = schema.keyspaces[0] table = keyspace.tables["my_table_0"] anonymize.anonymize_table(table) self.assertTrue(table.keyspace_name.startswith("ks")) self.assertTrue(table.name.startswith("tbl")) # check all columns at once columns = [column.name for column in table.columns.values()] self.assertPrefixes(columns, "col") # check standard columns match their pk and ck definitions self.assertEqual(columns[0], table.partition_key[0].name) self.assertEqual(columns[1], table.partition_key[1].name) self.assertEqual(columns[2], table.clustering_key[0].name) self.assertEqual(columns[3], table.clustering_key[1].name)
def test_custom_indexes_are_removed(self): schema = get_schema() keyspace = schema.keyspaces[0] tables = list(keyspace.tables.values()) table = tables[0] # ensure we have some custom indexes in the schema before anonymizing custom_indexes = [ index for index in table.indexes.values() if index.kind == "CUSTOM" ] self.assertTrue(len(custom_indexes) > 0) anonymize.anonymize_keyspace(keyspace) # custom indexes should have been removed custom_indexes = [ index for index in table.indexes.values() if index.kind == "CUSTOM" ] self.assertTrue(len(custom_indexes) == 0)