<commit_before>import time
import collections
import sys
import traceback

from functools import partial
# TODO add in requirements.txt
from enum import Enum  # Remove when switching to py3
from multiprocessing import Process, Queue

from cassandra import ConsistencyLevel
from cassandra.cluster import Cluster
from cassandra.query import SimpleStatement

from dtest import Tester, debug
from tools import since, new_node
from assertions import assert_all, assert_one, assert_invalid, assert_unavailable, assert_none


@since('3.0')
class TestMaterializedViews(Tester):
    """
    Test materialized views implementation.
    @jira_ticket CASSANDRA-6477
    @since 3.0
    """

    def prepare(self, user_table=False, rf=1, options={}):
        cluster = self.cluster
        cluster.populate([3, 0])
        if options:
            cluster.set_configuration_options(values=options)
        cluster.start()
        node1 = cluster.nodelist()[0]

        session = self.patient_cql_connection(node1)
        self.create_ks(session, 'ks', rf)

        if user_table:
            session.execute(
                ("CREATE TABLE users (username varchar, password varchar, gender varchar, "
                 "session_token varchar, state varchar, birth_year bigint, "
                 "PRIMARY KEY (username));")
            )

            # create a materialized view
            session.execute(("CREATE MATERIALIZED VIEW users_by_state AS "
                             "SELECT * FROM users WHERE STATE IS NOT NULL AND username IS NOT NULL "
                             "PRIMARY KEY (state, username)"))

        return session

    def _insert_data(self, session):
        # insert data
        insert_stmt = "INSERT INTO users (username, password, gender, state, birth_year) VALUES "
        session.execute(insert_stmt + "('user1', 'ch@ngem3a', 'f', 'TX', 1968);")
        session.execute(insert_stmt + "('user2', '<PASSWORD>', 'm', 'CA', 1971);")
        session.execute(insert_stmt + "('user3', 'ch<PASSWORD>', 'f', 'FL', 1978);")
        session.execute(insert_stmt + "('user4', '<PASSWORD>', 'm', 'TX', 1974);")

    def _replay_batchlogs(self):
        debug("Replaying batchlog on all nodes")
        for node in self.cluster.nodelist():
            if node.is_running():
                node.nodetool("replaybatchlog")

    def create_test(self):
        """Test the materialized view creation"""

        session = self.prepare(user_table=True)

        result = session.execute(("SELECT * FROM system_schema.materialized_views "
                                  "WHERE keyspace_name='ks' AND table_name='users'"))
        self.assertEqual(len(result), 1, "Expecting 1 materialized view, got" + str(result))

    def insert_test(self):
        """Test basic insertions"""

        session = self.prepare(user_table=True)<commit_msg>added validation tests for gc_create_seconds on CREATE_MV, ALTER_MV and ALTER_TABLE<commit_after>import time
import collections
import sys
import traceback

from functools import partial
# TODO add in requirements.txt
from enum import Enum  # Remove when switching to py3
from multiprocessing import Process, Queue

from cassandra import ConsistencyLevel
from cassandra.cluster import Cluster
from cassandra.query import SimpleStatement

from dtest import Tester, debug
from tools import since, new_node
from assertions import assert_all, assert_one, assert_invalid, assert_unavailable, assert_none


@since('3.0')
class TestMaterializedViews(Tester):
    """
    Test materialized views implementation.
    @jira_ticket CASSANDRA-6477
    @since 3.0
    """

    def prepare(self, user_table=False, rf=1, options={}):
        cluster = self.cluster
        cluster.populate([3, 0])
        if options:
            cluster.set_configuration_options(values=options)
        cluster.start()
        node1 = cluster.nodelist()[0]

        session = self.patient_cql_connection(node1)
        self.create_ks(session, 'ks', rf)

        if user_table:
            session.execute(
                ("CREATE TABLE users (username varchar, password varchar, gender varchar, "
                 "session_token varchar, state varchar, birth_year bigint, "
                 "PRIMARY KEY (username));")
            )

            # create a materialized view
            session.execute(("CREATE MATERIALIZED VIEW users_by_state AS "
                             "SELECT * FROM users WHERE STATE IS NOT NULL AND username IS NOT NULL "
                             "PRIMARY KEY (state, username)"))

        return session

    def _insert_data(self, session):
        # insert data
        insert_stmt = "INSERT INTO users (username, password, gender, state, birth_year) VALUES "
        session.execute(insert_stmt + "('user1', '<PASSWORD>', 'f', 'TX', 1968);")
        session.execute(insert_stmt + "('user2', '<PASSWORD>', 'm', 'CA', 1971);")
        session.execute(insert_stmt + "('user3', '<PASSWORD>', 'f', 'FL', 1978);")
        session.execute(insert_stmt + "('user4', '<PASSWORD>', 'm', 'TX', 1974);")

    def _replay_batchlogs(self):
        debug("Replaying batchlog on all nodes")
        for node in self.cluster.nodelist():
            if node.is_running():
                node.nodetool("replaybatchlog")

    def create_test(self):
        """Test the materialized view creation"""

        session = self.prepare(user_table=True)

        result = session.execute(("SELECT * FROM system_schema.materialized_views "
                                  "WHERE keyspace_name='ks' AND table_name='users'"))
        self.assertEqual(len(result), 1, "Expecting 1 materialized view, got" + str(result))

    def test_gcgs_validation(self):
        """Verify that it's not possible to create or set a too low gc_grace_seconds on MVs"""
        session = self.prepare(user_table=True)

        # First on base table
        assert_invalid(session,
                       "ALTER TABLE users WITH gc_grace_seconds = 0",
                       "Cannot alter gc_grace_seconds of the base table of a "
                       "materialized view to a value lower than 10800 seconds.")
        # Now on MV
        assert_invalid(session,
                       "ALTER MATERIALIZED VIEW users_by_state WITH gc_grace_seconds = 0",
                       "Cannot alter gc_grace_seconds of a materialized view to a value "
                       "lower than 10800 seconds.")

        # Now let's drop MV
        session.execute("DROP MATERIALIZED VIEW ks.users_by_state;")

        # Now we should be able to set the gc_grace_seconds of the base table to 0
        session.execute("ALTER TABLE users WITH gc_grace_seconds = 0")

        # Now we shouldn't be able to create a new MV on this table
        assert_invalid(session,
                       "CREATE MATERIALIZED VIEW users_by_state AS "
                       "SELECT * FROM users WHERE STATE IS NOT NULL AND username IS NOT NULL "
                       "PRIMARY KEY (state, username)",
                       "Base table 'users' must have gcGraceSeconds attribute of at least "
                       "10800 seconds")


    def insert_test(self):
        """Test basic insertions"""

        session = self.prepare(user_table=True)
{'fraction_non_alphanumeric': 0.09515235457063712, 'fraction_numerical': 0.01301939058171745, 'mean_word_length': 2.5327788649706457, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '893455', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1929, 'n_words': 610}