<commit_before>from dtest import Tester

import os, sys, time, re
from uuid import UUID
from ccmlib.cluster import Cluster

def assert_columns(cli, names):
    assert not cli.has_errors(), cli.errors()
    output = cli.last_output()

    for name in names:
        assert re.search('name=%s' % name, output) is not None, 'Cannot find column %s in %s' % (name, output)

class TestSCCache(Tester):

    def sc_with_row_cache_test(self):
        """ Test for bug reported in #4190 """
        cluster = self.cluster

        cluster.populate(1).start()
        node1 = cluster.nodelist()[0]
        self.patient_cql_connection(node1)

        cli = node1.cli()
        cli.do("create keyspace ks")
        cli.do("use ks")
        cli.do("""
           create column family Users
           with column_type='Super' and key_validation_class='UTF8Type' and comparator='UTF8Type' and subcomparator='UTF8Type' and default_validation_class='UTF8Type'
           and caching='ROWS_ONLY';
        """)

        cli.do("set Users['mina']['attrs']['name'] = 'Mina'")
        cli.do("get Users['mina']")
        assert_columns(cli, ['name'])

        cli.do("set Users['mina']['attrs']['country'] = 'Canada'")
        cli.do("get Users['mina']")
        assert_columns(cli, ['name', 'country'])

        cli.do("set Users['mina']['attrs']['region'] = 'Quebec'")
        cli.do("get Users['mina']")
        assert_columns(cli, ['name', 'country', 'region'])<commit_msg>Port super column cache test from CLI to Thrift API<commit_after>from thrift_bindings.v30.ttypes import (KsDef, CfDef, Mutation, ColumnOrSuperColumn,
                                        Column, SuperColumn, SliceRange, SlicePredicate,
                                        ColumnParent)
from thrift_bindings.v30.ttypes import ConsistencyLevel as ThriftConsistencyLevel

from thrift_tests import get_thrift_client

from dtest import Tester


class TestSCCache(Tester):

    def sc_with_row_cache_test(self):
        """ Test for bug reported in #4190 """
        cluster = self.cluster

        cluster.populate(1).start()
        node1 = cluster.nodelist()[0]
        self.patient_cql_connection(node1)

        node = self.cluster.nodelist()[0]
        host, port = node.network_interfaces['thrift']
        client = get_thrift_client(host, port)
        client.transport.open()

        ksdef = KsDef()
        ksdef.name = 'ks'
        ksdef.strategy_class = 'SimpleStrategy'
        ksdef.strategy_options = {'replication_factor': '1'}
        ksdef.cf_defs = []

        client.system_add_keyspace(ksdef)
        client.set_keyspace('ks')

        # create a super column family with UTF8 for all types
        cfdef = CfDef()
        cfdef.keyspace = 'ks'
        cfdef.name = 'Users'
        cfdef.column_type = 'Super'
        cfdef.comparator_type = 'UTF8Type'
        cfdef.subcomparator_type = 'UTF8Type'
        cfdef.key_validation_class = 'UTF8Type'
        cfdef.default_validation_class = 'UTF8Type'
        cfdef.caching = 'rows_only'

        client.system_add_column_family(cfdef)

        column = Column(name='name', value='Mina', timestamp=100)
        client.batch_mutate(
            {'mina': {'Users': [Mutation(ColumnOrSuperColumn(super_column=SuperColumn('attrs', [column])))]}},
            ThriftConsistencyLevel.ONE)

        column_parent = ColumnParent(column_family='Users')
        predicate = SlicePredicate(slice_range=SliceRange("", "", False, 100))
        super_columns = client.get_slice('mina', column_parent, predicate, ThriftConsistencyLevel.ONE)
        self.assertEqual(1, len(super_columns))
        super_column = super_columns[0].super_column
        self.assertEqual('attrs', super_column.name)
        self.assertEqual(1, len(super_column.columns))
        self.assertEqual('name', super_column.columns[0].name)
        self.assertEqual('Mina', super_column.columns[0].value)

        # add a 'country' subcolumn
        column = Column(name='country', value='Canada', timestamp=100)
        client.batch_mutate(
            {'mina': {'Users': [Mutation(ColumnOrSuperColumn(super_column=SuperColumn('attrs', [column])))]}},
            ThriftConsistencyLevel.ONE)

        super_columns = client.get_slice('mina', column_parent, predicate, ThriftConsistencyLevel.ONE)
        self.assertEqual(1, len(super_columns))
        super_column = super_columns[0].super_column
        self.assertEqual('attrs', super_column.name)
        self.assertEqual(2, len(super_column.columns))

        self.assertEqual('country', super_column.columns[0].name)
        self.assertEqual('Canada', super_column.columns[0].value)

        self.assertEqual('name', super_column.columns[1].name)
        self.assertEqual('Mina', super_column.columns[1].value)

        # add a 'region' subcolumn
        column = Column(name='region', value='Quebec', timestamp=100)
        client.batch_mutate(
            {'mina': {'Users': [Mutation(ColumnOrSuperColumn(super_column=SuperColumn('attrs', [column])))]}},
            ThriftConsistencyLevel.ONE)

        super_columns = client.get_slice('mina', column_parent, predicate, ThriftConsistencyLevel.ONE)
        self.assertEqual(1, len(super_columns))
        super_column = super_columns[0].super_column
        self.assertEqual('attrs', super_column.name)
        self.assertEqual(3, len(super_column.columns))

        self.assertEqual('country', super_column.columns[0].name)
        self.assertEqual('Canada', super_column.columns[0].value)

        self.assertEqual('name', super_column.columns[1].name)
        self.assertEqual('Mina', super_column.columns[1].value)

        self.assertEqual('region', super_column.columns[2].name)
        self.assertEqual('Quebec', super_column.columns[2].value)
{'fraction_non_alphanumeric': 0.12889740463333915, 'fraction_numerical': 0.01132206932590141, 'mean_word_length': 3.1339092872570196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7028458', 'n_tokens_mistral': 1748, 'n_tokens_neox': 1682, 'n_words': 317}