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:39 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_r331987549
 
 

 ##########
 File path: test/unit/org/apache/cassandra/cql3/statements/DescribeStatementTest.java
 ##########
 @@ -0,0 +1,853 @@
+/*
+ * 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.cql3.statements;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.regex.Pattern;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+import com.datastax.driver.core.SimpleStatement;
+import com.datastax.driver.core.exceptions.InvalidQueryException;
+import org.apache.cassandra.db.virtual.SystemViewsKeyspace;
+import org.apache.cassandra.db.virtual.VirtualKeyspaceRegistry;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.apache.cassandra.OrderedJUnit4ClassRunner;
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.db.marshal.IntegerType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.schema.KeyspaceMetadata;
+import org.apache.cassandra.schema.KeyspaceParams;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.schema.Tables;
+import org.apache.cassandra.transport.ProtocolVersion;
+import org.apache.cassandra.utils.ByteBufferUtil;
+
+import static java.lang.String.format;
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+@RunWith(OrderedJUnit4ClassRunner.class)
+public class DescribeStatementTest extends CQLTester
+{
+    @BeforeClass
+    public static void setup()
+    {
+        VirtualKeyspaceRegistry.instance.register(SystemViewsKeyspace.instance);
+    }
+
+    @Test
+    public void testDescribeKeyspacesMocked() throws Throwable
+    {
+        DescribeStatement.SchemaAccess oldSchema = DescribeStatement.schema;
+        try
+        {
+            DescribeStatement.schema = () -> new DescribeStatement.SchemaSnapshot()
+            {
+                @Override
+                public ByteBuffer schemaHash()
+                {
+                    return ByteBuffer.allocate(0);
+                }
+
+                @Override
+                public Stream<String> rawKeyspaceNames()
+                {
+                    return Stream.of("abc", "def", "CqlshTests_Cl2_EF");
+                }
+
+                @Override
+                public KeyspaceMetadata keyspaceMetadata(String ks)
+                {
+                    return null;
+                }
+            };
+
+            assertThat(executeDescribe("DESC KEYSPACES"),
+                       containsString("\"CqlshTests_Cl2_EF\"  abc  def\n"));
+        }
+        finally
+        {
+            DescribeStatement.schema = oldSchema;
+        }
+    }
+
+    @Test
+    public void testSchemaChangeDuringPaging()
+    {
+        DescribeStatement.SchemaAccess oldSchema = DescribeStatement.schema;
+        try
+        {
+            AtomicReference<ByteBuffer> schemaHash = new AtomicReference<>(ByteBuffer.allocate(0));
+
+            DescribeStatement.schema = () -> new DescribeStatement.SchemaSnapshot()
+            {
+                @Override
+                public ByteBuffer schemaHash()
+                {
+                    return schemaHash.get().duplicate();
+                }
+
+                @Override
+                public Stream<String> rawKeyspaceNames()
+                {
+                    return Stream.of("abc", "def", "def");
+                }
+
+                @Override
+                public KeyspaceMetadata keyspaceMetadata(String ks)
+                {
+                    return null;
+                }
+            };
+
+            SimpleStatement stmt = new SimpleStatement("DESCRIBE KEYSPACES");
+            stmt.setFetchSize(1);
+            ResultSet rs = executeNet(ProtocolVersion.CURRENT, stmt);
+            Iterator<Row> iter = rs.iterator();
+            assertTrue(iter.hasNext());
+            iter.next();
+
+            schemaHash.set(ByteBufferUtil.bytes(1));
+            try
+            {
+                iter.next();
+                fail("Expected InvalidQueryException");
+            }
+            catch (InvalidQueryException e)
+            {
+                assertEquals(DescribeStatement.SCHEMA_CHANGED_WHILE_PAGING_MESSAGE, e.getMessage());
+            }
+        }
+        finally
+        {
+            DescribeStatement.schema = oldSchema;
+        }
+    }
+
+    /**
+     * Much of this test method has been ported from {@code ported from cqlsh_tests.cqlsh_tests.TestCqlsh#test_describe*}
+     * methods.
+     */
+    @Test
+    public void testDescribe() throws Throwable
+    {
+        try
+        {
+            // from test_describe()
+            execute("CREATE KEYSPACE test WITH REPLICATION = {'class' : 'SimpleStrategy',
'replication_factor' : 1};");
+            execute("CREATE TABLE test.users ( userid text PRIMARY KEY, firstname text, lastname
text, age int);");
+            execute("CREATE INDEX myindex ON test.users (age);");
+            execute("CREATE INDEX \"QuotedNameIndex\" on test.users (firstName);");
+            execute("CREATE TABLE test.test (id int, col int, val text, PRIMARY KEY(id, col));");
+            execute("CREATE INDEX ON test.test (col);");
+            execute("CREATE INDEX ON test.test (val)");
+            // from test_describe_mv()
+            execute("CREATE TABLE test.users_mv (username varchar, password varchar, gender
varchar, session_token varchar, " +
+                    "state varchar, birth_year bigint, PRIMARY KEY (username));");
+            execute("CREATE MATERIALIZED VIEW test.users_by_state AS SELECT * FROM test.users_mv
" +
+                    "WHERE STATE IS NOT NULL AND username IS NOT NULL PRIMARY KEY (state,
username)");
+            execute(allTypesTable());
+
+            // actual tests follow
+
+            testDescribeCluster();
+
+            testDescribeFullSchema();
+
+            testDescribeKeyspaces();
+
+            testDescribeTables();
+
+            testDescribeTable();
+
+            assertThat(executeDescribe("DESCRIBE KEYSPACE test"),
+                       allOf(containsString(keyspaceOutput()),
+                             containsString("CREATE TABLE")));
+            assertThat(executeDescribe("DESCRIBE ONLY KEYSPACE test"),
+                       allOf(containsString(keyspaceOutput()),
+                             not(containsString("CREATE TABLE"))));
+
+            // tests as in cqlsh_tests.cqlsh_tests.TestCqlsh#test_describe
+
+            Iterable<Matcher<? super String>> keyspaceOutput =
+                    Arrays.asList(containsString(keyspaceOutput()),
+                                  containsString(testTableOutput(true, true)),
+                                  containsString(indexOutput("test_col_idx", "test", "col")),
+                                  containsString(indexOutput("test_val_idx", "test", "val")),
+                                  containsString(userTableOutput()),
+                                  containsString(indexOutput("\"QuotedNameIndex\"", "users",
"firstname")),
+                                  containsString(indexOutput("myindex", "users", "age")));
+            Iterable<Matcher<? super String>> testTableOutput =
+                    Arrays.asList(containsString(testTableOutput(true, true)),
+                                  containsString(indexOutput("test_col_idx", "test", "col")),
+                                  containsString(indexOutput("test_val_idx", "test", "val")));
+            Iterable<Matcher<? super String>> usersTableOutput =
+                    Arrays.asList(containsString(userTableOutput()),
+                                  containsString(indexOutput("\"QuotedNameIndex\"", "users",
"firstname")),
+                                  containsString(indexOutput("myindex", "users", "age")));
+
+            assertThat(executeDescribe("DESCRIBE test"), allOf(keyspaceOutput));
+            describeError("DESCRIBE test2", "Keyspace 'test2' not found");
+            assertThat(executeDescribe("test", "DESCRIBE KEYSPACE"), allOf(keyspaceOutput));
+
+            // Describe table
+            assertThat(executeDescribe("DESCRIBE TABLE test.test"), allOf(testTableOutput));
+            assertThat(executeDescribe("DESCRIBE TABLE test.users"), allOf(usersTableOutput));
+            assertThat(executeDescribe("DESCRIBE test.test"), allOf(testTableOutput));
+            assertThat(executeDescribe("DESCRIBE test.users"), allOf(usersTableOutput));
+            describeError("DESCRIBE test.users2", "'users2' not found in keyspace 'test'");
+            assertThat(executeDescribe("test", "DESCRIBE TABLE test"), allOf(testTableOutput));
+            assertThat(executeDescribe("test", "DESCRIBE COLUMNFAMILY users"), allOf(usersTableOutput));
+            assertThat(executeDescribe("test", "DESCRIBE test"), allOf(keyspaceOutput));
+            assertThat(executeDescribe("test", "DESCRIBE users"), allOf(usersTableOutput));
+            describeError("test", "DESCRIBE users2", "'users2' not found in keyspace 'test'");
+
+            // Describe index
+            assertThat(executeDescribe("DESCRIBE INDEX test.myindex"), containsString(indexOutput("myindex",
"users", "age")));
+            assertThat(executeDescribe("DESCRIBE INDEX test.test_col_idx"), containsString(indexOutput("test_col_idx",
"test", "col")));
+            assertThat(executeDescribe("DESCRIBE INDEX test.test_val_idx"), containsString(indexOutput("test_val_idx",
"test", "val")));
+            assertThat(executeDescribe("DESCRIBE test.myindex"), containsString(indexOutput("myindex",
"users", "age")));
+            assertThat(executeDescribe("DESCRIBE test.test_col_idx"), containsString(indexOutput("test_col_idx",
"test", "col")));
+            assertThat(executeDescribe("DESCRIBE test.test_val_idx"), containsString(indexOutput("test_val_idx",
"test", "val")));
+            describeError("DESCRIBE test.myindex2", "'myindex2' not found in keyspace 'test'");
+            assertThat(executeDescribe("test", "DESCRIBE INDEX myindex"), containsString(indexOutput("myindex",
"users", "age")));
+            assertThat(executeDescribe("test", "DESCRIBE INDEX test_col_idx"), containsString(indexOutput("test_col_idx",
"test", "col")));
+            assertThat(executeDescribe("test", "DESCRIBE INDEX test_val_idx"), containsString(indexOutput("test_val_idx",
"test", "val")));
+            assertThat(executeDescribe("test", "DESCRIBE myindex"), containsString(indexOutput("myindex",
"users", "age")));
+            assertThat(executeDescribe("test", "DESCRIBE test_col_idx"), containsString(indexOutput("test_col_idx",
"test", "col")));
+            assertThat(executeDescribe("test", "DESCRIBE test_val_idx"), containsString(indexOutput("test_val_idx",
"test", "val")));
+            describeError("test", "DESCRIBE myindex2", "'myindex2' not found in keyspace
'test'");
+
+            // Drop table and recreate
+            execute("DROP TABLE test.users");
+            describeError("DESCRIBE test.users", "'users' not found in keyspace 'test'");
+            describeError("DESCRIBE test.myindex", "'myindex' not found in keyspace 'test'");
+            execute("CREATE TABLE test.users ( userid text PRIMARY KEY, firstname text, lastname
text, age int)");
+            execute("CREATE INDEX myindex ON test.users (age);");
+            execute("CREATE INDEX \"QuotedNameIndex\" on test.users (firstname)");
+            assertThat(executeDescribe("DESCRIBE test.users"), allOf(usersTableOutput));
+            assertThat(executeDescribe("DESCRIBE test.myindex"), containsString(indexOutput("myindex",
"users", "age")));
+
+            // Drop index and recreate
+            execute("DROP INDEX test.myindex");
+            describeError("DESCRIBE test.myindex", "'myindex' not found in keyspace 'test'");
+            execute("CREATE INDEX myindex ON test.users (age)");
+            assertThat(executeDescribe("DESCRIBE INDEX test.myindex"), containsString(indexOutput("myindex",
"users", "age")));
+            execute("DROP INDEX test.\"QuotedNameIndex\"");
+            describeError("DESCRIBE test.\"QuotedNameIndex\"", "'QuotedNameIndex' not found
in keyspace 'test'");
+            execute("CREATE INDEX \"QuotedNameIndex\" ON test.users (firstname)");
+            assertThat(executeDescribe("DESCRIBE INDEX test.\"QuotedNameIndex\""), containsString(indexOutput("\"QuotedNameIndex\"",
"users", "firstname")));
+
+            // Alter table. Renaming indexed columns is not allowed, and since 3.0 neither
is dropping them
+            // Prior to 3.0 the index would have been automatically dropped, but now we need
to explicitly do that.
+            execute("DROP INDEX test.test_val_idx");
+            execute("ALTER TABLE test.test DROP val");
+            assertThat(executeDescribe("DESCRIBE test.test"), containsString(testTableOutput(false,
true)));
+            // A dropped column is mentioned 2 times:
+            // - in the CREATE TABLE to be able to issue a ...
+            // - proper ALTER TABLE DROP USING TIMESTAMP
+            assertThat(executeDescribe("DESCRIBE test.test WITH INTERNALS"),
+                       allOf(containsString(testTableOutput(true, false)),
+                             containsString("ALTER TABLE test.test DROP val "),
+                             not(containsString("ALTER TABLE test.test ADD val text;"))));
+            describeError("DESCRIBE test.test_val_idx", "'test_val_idx' not found in keyspace
'test'");
+            execute("ALTER TABLE test.test ADD val text");
+            assertThat(executeDescribe("DESCRIBE test.test"), containsString(testTableOutput(true,
true)));
+            // A re-created column is mentioned 3 times:
+            // - in the CREATE TABLE to be able to issue a ...
+            // - proper ALTER TABLE DROP USING TIMESTAMP and finally ...
+            // - an ALTER TABLE ADD
+            assertThat(executeDescribe("DESCRIBE test.test WITH INTERNALS"),
+                       allOf(containsString("ALTER TABLE test.test DROP val "),
+                             containsString("ALTER TABLE test.test ADD val text;")));
+            describeError("DESCRIBE test.test_val_idx", "'test_val_idx' not found in keyspace
'test'");
+
+            // port of test_describe_describes_non_default_compaction_parameters()
+
+            execute("CREATE TABLE test.tab (key int PRIMARY KEY ) " +
+                    "WITH compaction = {'class': 'SizeTieredCompactionStrategy'," +
+                    "'min_threshold': 10, 'max_threshold': 100 }");
+            assertThat(executeDescribe("DESCRIBE test.tab"),
+                       allOf(containsString("'min_threshold': '10'"),
+                             containsString("'max_threshold': '100'")));
+
+            // port of test_describe_on_non_reserved_keywords()
+
+            execute("CREATE TABLE test.map (key int PRIMARY KEY, val text)");
+            assertThat(executeDescribe("DESCRIBE test.map"),
+                       containsString("CREATE TABLE test.map ("));
+            assertThat(executeDescribe("test", "DESCRIBE map"),
+                       containsString("CREATE TABLE test.map ("));
+
+            // port of test_describe_mv()
+
+            assertThat(executeDescribe("DESCRIBE KEYSPACE test"), containsString("users_by_state"));
+            assertThat(executeDescribe("DESCRIBE MATERIALIZED VIEW test.users_by_state"),
containsString(usersByStateMvOutput()));
+            assertThat(executeDescribe("DESCRIBE test.users_by_state"), containsString(usersByStateMvOutput()));
+            assertThat(executeDescribe("test", "DESCRIBE MATERIALIZED VIEW test.users_by_state"),
containsString(usersByStateMvOutput()));
+            assertThat(executeDescribe("test", "DESCRIBE MATERIALIZED VIEW users_by_state"),
containsString(usersByStateMvOutput()));
+            assertThat(executeDescribe("test", "DESCRIBE users_by_state"), containsString(usersByStateMvOutput()));
+
+            // test quotes
+            assertThat(executeDescribe("test", "DESCRIBE MATERIALIZED VIEW \"users_by_state\""),
containsString(usersByStateMvOutput()));
+            assertThat(executeDescribe("test", "DESCRIBE \"users_by_state\""), containsString(usersByStateMvOutput()));
+        }
+        finally
+        {
+            execute("DROP KEYSPACE IF EXISTS test");
+        }
+    }
+
+    private void testDescribeTable() throws Throwable
+    {
+        for (String cmd : new String[]{"describe table test.has_all_types",
+                                       "describe columnfamily test.has_all_types",
+                                       "describe test.has_all_types"})
+            assertThat(executeDescribe(cmd),
+                       containsString(allTypesTable()));
+        for (String cmd : new String[]{"describe table has_all_types",
+                                       "describe columnfamily has_all_types",
+                                       "describe has_all_types"})
+            assertThat(executeDescribe("test", cmd),
+                       containsString(allTypesTable()));
+    }
+
+    private void testDescribeTables() throws Throwable
+    {
+        for (String cmd : new String[]{"describe TABLES",
+                                       "DESC tables"})
+            assertThat(executeDescribe(cmd),
+                       allOf(matchesRegex(Pattern.compile(".*" +
+                                                          "\n" +
+                                                          "Keyspace test\n" +
+                                                          "-------------\n" +
+                                                          ".*",
+                                                          Pattern.DOTALL)),
+                             matchesRegex(Pattern.compile(".*" +
+                                                          "\n" +
+                                                          "Keyspace system\n" +
+                                                          "---------------\n" +
+                                                          ".*",
+                                                          Pattern.DOTALL))));
+        assertThat(executeDescribe("test", "describe TABLES"),
+                   allOf(containsString("\nhas_all_types  test  users  users_mv\n"),
+                         not(containsString("Keyspace test\n")),
+                         not(containsString("Keyspace system\n"))));
+    }
+
+    private void testDescribeKeyspaces() throws Throwable
+    {
+        assertThat(executeDescribe("DESCRIBE KEYSPACES"),
+                   allOf(containsString("test"),
+                         containsString("system")));
+    }
+
+    private void testDescribeFullSchema() throws Throwable
+    {
+        assertThat(executeDescribe("DESCRIBE FULL SCHEMA"),
+                   allOf(containsString("\nCREATE KEYSPACE system\n" +
+                                        "    WITH replication = {'class': 'org.apache.cassandra.locator.LocalStrategy'}\n"
+
+                                        "     AND durable_writes = true;\n"),
+                         containsString("\n// VIRTUAL KEYSPACE system_views;\n")));
+    }
+
+    private void testDescribeCluster() throws Throwable
+    {
+        assertThat(executeDescribe("DESCRIBE CLUSTER"),
+                   allOf(matchesRegex(Pattern.compile(".*" +
+                                                      "Cluster: [a-zA-Z0-9 ]+\n" +
+                                                      "Partitioner: [a-zA-Z0-9.]+\n" +
+                                                      "Snitch: [a-zA-Z0-9.$]+\n" +
+                                                      ".*",
+                                                      Pattern.DOTALL)),
+                         not(containsString("Range ownership:"))));
+        assertThat(executeDescribe("test", "DESCRIBE CLUSTER"),
+                   matchesRegex(Pattern.compile(".*" +
+                                                "Cluster: [a-zA-Z0-9 ]+\n" +
+                                                "Partitioner: [a-zA-Z0-9.]+\n" +
+                                                "Snitch: [a-zA-Z0-9.$]+\n" +
+                                                "\n" +
+                                                "Range ownership:\n" +
+                                                "([ ]+[^ ]+[ ][ ]\\[(\\d+[.]){3}\\d+:\\d+]\n)+"
+
+                                                "\n.*",
+                                                Pattern.DOTALL)));
+    }
+
+    @Test
+    public void testDescribeMissingKeyspace() throws Throwable
 
 Review comment:
   ![Codacy](https://app.codacy.com/assets/images/favicon.png) Issue found: [JUnit tests should
include assert() or fail()](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