cassandra-pr mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [cassandra] codacy-bot commented on a change in pull request #361: Expose schema via new DESCRIBE CQL statement
Date Mon, 07 Oct 2019 12:23:37 GMT
codacy-bot commented on a change in pull request #361: Expose schema via new DESCRIBE CQL statement
URL: https://github.com/apache/cassandra/pull/361#discussion_r331987530
 
 

 ##########
 File path: test/unit/org/apache/cassandra/db/SchemaCQLHelperTest.java
 ##########
 @@ -0,0 +1,478 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.db;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.io.Files;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.cassandra.*;
+import org.apache.cassandra.cql3.*;
+import org.apache.cassandra.cql3.statements.schema.IndexTarget;
+import org.apache.cassandra.db.marshal.*;
+import org.apache.cassandra.exceptions.ConfigurationException;
+import org.apache.cassandra.index.sasi.SASIIndex;
+import org.apache.cassandra.schema.*;
+import org.apache.cassandra.service.reads.SpeculativeRetryPolicy;
+import org.apache.cassandra.utils.ByteBufferUtil;
+import org.apache.cassandra.utils.FBUtilities;
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+
+import java.io.FileReader;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+
+public class SchemaCQLHelperTest extends CQLTester
+{
+    @Before
+    public void defineSchema() throws ConfigurationException
+    {
+        SchemaLoader.prepareServer();
+    }
+
+    @Test
+    public void testUserTypesCQL()
+    {
+        String keyspace = "cql_test_keyspace_user_types";
+        String table = "test_table_user_types";
+
+        UserType typeA = new UserType(keyspace, ByteBufferUtil.bytes("a"),
+                                      Arrays.asList(FieldIdentifier.forUnquoted("a1"),
+                                                    FieldIdentifier.forUnquoted("a2"),
+                                                    FieldIdentifier.forUnquoted("a3")),
+                                      Arrays.asList(IntegerType.instance,
+                                                    IntegerType.instance,
+                                                    IntegerType.instance),
+                                      true);
+
+        UserType typeB = new UserType(keyspace, ByteBufferUtil.bytes("b"),
+                                      Arrays.asList(FieldIdentifier.forUnquoted("b1"),
+                                                    FieldIdentifier.forUnquoted("b2"),
+                                                    FieldIdentifier.forUnquoted("b3")),
+                                      Arrays.asList(typeA,
+                                                    typeA,
+                                                    typeA),
+                                      true);
+
+        UserType typeC = new UserType(keyspace, ByteBufferUtil.bytes("c"),
+                                      Arrays.asList(FieldIdentifier.forUnquoted("c1"),
+                                                    FieldIdentifier.forUnquoted("c2"),
+                                                    FieldIdentifier.forUnquoted("c3")),
+                                      Arrays.asList(typeB,
+                                                    typeB,
+                                                    typeB),
+                                      true);
+
+        TableMetadata cfm =
+        TableMetadata.builder(keyspace, table)
+                     .addPartitionKeyColumn("pk1", IntegerType.instance)
+                     .addClusteringColumn("ck1", IntegerType.instance)
+                     .addRegularColumn("reg1", typeC.freeze())
+                     .addRegularColumn("reg2", ListType.getInstance(IntegerType.instance,
false))
+                     .addRegularColumn("reg3", MapType.getInstance(AsciiType.instance, IntegerType.instance,
true))
+                     .build();
+
+        SchemaLoader.createKeyspace(keyspace, KeyspaceParams.simple(1), Tables.of(cfm), Types.of(typeA,
typeB, typeC));
+
+        ColumnFamilyStore cfs = Keyspace.open(keyspace).getColumnFamilyStore(table);
+
+        assertEquals(ImmutableList.of("CREATE TYPE cql_test_keyspace_user_types.a (\n" +
+                                      "    a1 varint,\n" +
+                                      "    a2 varint,\n" +
+                                      "    a3 varint\n" +
+                                      ");",
+                                      "CREATE TYPE cql_test_keyspace_user_types.b (\n" +
+                                      "    b1 a,\n" +
+                                      "    b2 a,\n" +
+                                      "    b3 a\n" +
+                                      ");",
+                                      "CREATE TYPE cql_test_keyspace_user_types.c (\n" +
+                                      "    c1 b,\n" +
+                                      "    c2 b,\n" +
+                                      "    c3 b\n" +
+                                      ");"),
+                     SchemaCQLHelper.getUserTypesAsCQL(cfs.metadata(), cfs.keyspace.getMetadata().types).collect(Collectors.toList()));
+    }
+
+    @Test
+    public void testDroppedColumnsCQL()
+    {
+        String keyspace = "cql_test_keyspace_dropped_columns";
+        String table = "test_table_dropped_columns";
+
+        TableMetadata.Builder builder =
+        TableMetadata.builder(keyspace, table)
+                     .addPartitionKeyColumn("pk1", IntegerType.instance)
+                     .addClusteringColumn("ck1", IntegerType.instance)
+                     .addStaticColumn("st1", IntegerType.instance)
+                     .addRegularColumn("reg1", IntegerType.instance)
+                     .addRegularColumn("reg2", IntegerType.instance)
+                     .addRegularColumn("reg3", IntegerType.instance);
+
+        ColumnMetadata st1 = builder.getColumn(ByteBufferUtil.bytes("st1"));
+        ColumnMetadata reg1 = builder.getColumn(ByteBufferUtil.bytes("reg1"));
+        ColumnMetadata reg2 = builder.getColumn(ByteBufferUtil.bytes("reg2"));
+        ColumnMetadata reg3 = builder.getColumn(ByteBufferUtil.bytes("reg3"));
+
+        builder.removeRegularOrStaticColumn(st1.name)
+               .removeRegularOrStaticColumn(reg1.name)
+               .removeRegularOrStaticColumn(reg2.name)
+               .removeRegularOrStaticColumn(reg3.name);
+
+        builder.recordColumnDrop(st1, 5000)
+               .recordColumnDrop(reg1, 10000)
+               .recordColumnDrop(reg2, 20000)
+               .recordColumnDrop(reg3, 30000);
+
+        SchemaLoader.createKeyspace(keyspace, KeyspaceParams.simple(1), builder);
+
+        ColumnFamilyStore cfs = Keyspace.open(keyspace).getColumnFamilyStore(table);
+
+        String expected = "CREATE TABLE IF NOT EXISTS cql_test_keyspace_dropped_columns.test_table_dropped_columns
(\n" +
+                          "    pk1 varint,\n" +
+                          "    ck1 varint,\n" +
+                          "    reg1 varint,\n" +
+                          "    reg3 varint,\n" +
+                          "    reg2 varint,\n" +
+                          "    st1 varint static,\n" +
+                          "    PRIMARY KEY (pk1, ck1)\n) WITH ID =";
+        String actual = SchemaCQLHelper.getTableMetadataAsCQL(cfs.metadata(), Types.none(),
true, true, true);
+
+        assertThat(actual, startsWith(expected));
+
+        assertThat(SchemaCQLHelper.getDroppedColumnsAsCQL(cfs.metadata()).collect(Collectors.joining()),
+                   allOf(containsString("ALTER TABLE cql_test_keyspace_dropped_columns.test_table_dropped_columns
DROP reg1 USING TIMESTAMP 10000;"),
+                         containsString("ALTER TABLE cql_test_keyspace_dropped_columns.test_table_dropped_columns
DROP reg3 USING TIMESTAMP 30000;"),
+                         containsString("ALTER TABLE cql_test_keyspace_dropped_columns.test_table_dropped_columns
DROP reg2 USING TIMESTAMP 20000;"),
+                         containsString("ALTER TABLE cql_test_keyspace_dropped_columns.test_table_dropped_columns
DROP st1 USING TIMESTAMP 5000;")));
+    }
+
+    /**
+     * Asserts that {@code actual} contains {@code expected} as a substring.
+     */
+    private void assertContains(String expected, String actual)
 
 Review comment:
   ![Codacy](https://app.codacy.com/assets/images/favicon.png) Issue found: [Avoid unused
private methods such as 'assertContains(String,String)'.](https://app.codacy.com/app/Wen1234/cassandra/pullRequest?prid=4274613)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org


Mime
View raw message