'test to ensure that materialized view metadata is properly constructed
test_create_view_metadata tests that materialized views metadata is properly constructed. It runs a simple
query to construct a materialized view, then proceeds to inspect the metadata associated with that MV.
Columns are inspected to insure that all are of the proper type, and in the proper type.
@since 3.0.0
@jira_ticket PYTHON-371
@expected_result Materialized view metadata should be constructed appropriately.
@test_category metadata'

def test_create_view_metadata(self):

    create_table = 'CREATE    TABLE    {0}.scores(\n                                                                                                user    TEXT,\n                                                                                                game    TEXT,\n                                                                                                year    INT,\n                                                                                                month    INT,\n                                                                                                day    INT,\n                                                                                                score    INT,\n                                                                                                PRIMARY    KEY    (user,    game,    year,    month,    day)\n                                                                                                )'.format(self.keyspace_name)
    self.session.execute(create_table)
    create_mv = 'CREATE    MATERIALIZED    VIEW    {0}.monthlyhigh    AS\n                                                                                                SELECT    game,    year,    month,    score,    user,    day    FROM    {0}.scores\n                                                                                                WHERE    game    IS    NOT    NULL    AND    year    IS    NOT    NULL    AND    month    IS    NOT    NULL    AND    score    IS    NOT    NULL    AND    user    IS    NOT    NULL    AND    day    IS    NOT    NULL\n                                                                                                PRIMARY    KEY    ((game,    year,    month),    score,    user,    day)\n                                                                                                WITH    CLUSTERING    ORDER    BY    (score    DESC,    user    ASC,    day    ASC)'.format(self.keyspace_name)
    self.session.execute(create_mv)
    score_table = self.cluster.metadata.keyspaces[self.keyspace_name].tables['scores']
    mv = self.cluster.metadata.keyspaces[self.keyspace_name].views['monthlyhigh']
    self.assertIsNotNone(score_table.views['monthlyhigh'])
    self.assertIsNotNone(len(score_table.views), 1)
    self.assertEqual(len(score_table.partition_key), 1)
    self.assertIsNotNone(score_table.columns['user'])
    self.assertTrue(score_table.columns['user'], score_table.partition_key[0])
    self.assertEqual(len(score_table.clustering_key), 4)
    self.assertIsNotNone(score_table.columns['game'])
    self.assertTrue(score_table.columns['game'], score_table.clustering_key[0])
    self.assertIsNotNone(score_table.columns['year'])
    self.assertTrue(score_table.columns['year'], score_table.clustering_key[1])
    self.assertIsNotNone(score_table.columns['month'])
    self.assertTrue(score_table.columns['month'], score_table.clustering_key[2])
    self.assertIsNotNone(score_table.columns['day'])
    self.assertTrue(score_table.columns['day'], score_table.clustering_key[3])
    self.assertIsNotNone(score_table.columns['score'])
    self.assertEqual(mv.keyspace_name, self.keyspace_name)
    self.assertEqual(mv.name, 'monthlyhigh')
    self.assertEqual(mv.base_table_name, 'scores')
    self.assertFalse(mv.include_all_columns)
    mv_columns = list(mv.columns.values())
    self.assertEqual(len(mv_columns), 6)
    game_column = mv_columns[0]
    self.assertIsNotNone(game_column)
    self.assertEqual(game_column.name, 'game')
    self.assertEqual(game_column, mv.partition_key[0])
    year_column = mv_columns[1]
    self.assertIsNotNone(year_column)
    self.assertEqual(year_column.name, 'year')
    self.assertEqual(year_column, mv.partition_key[1])
    month_column = mv_columns[2]
    self.assertIsNotNone(month_column)
    self.assertEqual(month_column.name, 'month')
    self.assertEqual(month_column, mv.partition_key[2])
    def compare_columns(a, b, name):
        self.assertEqual(a.name, name)
        self.assertEqual(a.name, b.name)
        self.assertEqual(a.table, b.table)
        self.assertEqual(a.cql_type, b.cql_type)
        self.assertEqual(a.is_static, b.is_static)
        self.assertEqual(a.is_reversed, b.is_reversed)
    score_column = mv_columns[3]
    compare_columns(score_column, mv.clustering_key[0], 'score')
    user_column = mv_columns[4]
    compare_columns(user_column, mv.clustering_key[1], 'user')
    day_column = mv_columns[5]
    compare_columns(day_column, mv.clustering_key[2], 'day')
