kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 7ac3dee0174725d14fb7c292a1fb020f62afd8b66fdc96b3e92e00586d47a2cd |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-07-16 14:36 +02:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-07-16 16:10 +02:00 |
| Parents | 7e5f7db00631 |
5 files changed
modified driver-core/src/test/java/com/datastax/driver/core/Assertions.java +9 −0
| @@ -44,4 +44,13 @@ | ||
| 44 | 44 | public static LocalDateAssert assertThat(LocalDate localDate) { |
| 45 | 45 | return new LocalDateAssert(localDate); |
| 46 | 46 | } |
| 47 | + | |
| 48 | + public static TableMetadataAssert assertThat(TableMetadata table) { | |
| 49 | + return new TableMetadataAssert(table); | |
| 50 | + } | |
| 51 | + | |
| 52 | + public static ColumnMetadataAssert assertThat(ColumnMetadata column) { | |
| 53 | + return new ColumnMetadataAssert(column); | |
| 54 | + } | |
| 55 | + | |
| 47 | 56 | } |
added driver-core/src/test/java/com/datastax/driver/core/ColumnMetadataAssert.java +73 −0
| @@ -0,0 +1,73 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import org.assertj.core.api.AbstractAssert; | |
| 19 | + | |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 21 | + | |
| 22 | +public class ColumnMetadataAssert extends AbstractAssert<ColumnMetadataAssert, ColumnMetadata> { | |
| 23 | + | |
| 24 | + protected ColumnMetadataAssert(ColumnMetadata actual) { | |
| 25 | + super(actual, ColumnMetadataAssert.class); | |
| 26 | + } | |
| 27 | + | |
| 28 | + public ColumnMetadataAssert hasType(DataType dataType) { | |
| 29 | + assertThat(actual.getType()).isEqualTo(dataType); | |
| 30 | + return this; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public ColumnMetadataAssert hasName(String name) { | |
| 34 | + assertThat(actual.getName()).isEqualTo(name); | |
| 35 | + return this; | |
| 36 | + } | |
| 37 | + | |
| 38 | + public ColumnMetadataAssert isPrimaryKey() { | |
| 39 | + assertThat(actual.getTable().getPrimaryKey().contains(actual)); | |
| 40 | + return this; | |
| 41 | + } | |
| 42 | + | |
| 43 | + public ColumnMetadataAssert isPartitionKey() { | |
| 44 | + assertThat(actual.getTable().getPartitionKey().contains(actual)); | |
| 45 | + return this; | |
| 46 | + } | |
| 47 | + | |
| 48 | + public ColumnMetadataAssert isClusteringColumn() { | |
| 49 | + assertThat(actual.getTable().getClusteringColumns().contains(actual)); | |
| 50 | + return this; | |
| 51 | + } | |
| 52 | + | |
| 53 | + public ColumnMetadataAssert isRegularColumn() { | |
| 54 | + assertThat(!actual.getTable().getPrimaryKey().contains(actual)); | |
| 55 | + return this; | |
| 56 | + } | |
| 57 | + | |
| 58 | + public ColumnMetadataAssert hasClusteringOrder(TableMetadata.Order order) { | |
| 59 | + assertThat(actual.getTable().getClusteringOrder().get(actual.getTable().getClusteringColumns().indexOf(actual))).isEqualTo(order); | |
| 60 | + return this; | |
| 61 | + } | |
| 62 | + | |
| 63 | + public ColumnMetadataAssert isStatic() { | |
| 64 | + assertThat(actual.isStatic()).isTrue(); | |
| 65 | + return this; | |
| 66 | + } | |
| 67 | + | |
| 68 | + public ColumnMetadataAssert isNotStatic() { | |
| 69 | + assertThat(actual.isStatic()).isFalse(); | |
| 70 | + return this; | |
| 71 | + } | |
| 72 | + | |
| 73 | +} |
deleted driver-core/src/test/java/com/datastax/driver/core/SchemaTest.java +0 −176
| @@ -1,176 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | - * | |
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | - * you may not use this file except in compliance with the License. | |
| 6 | - * You may obtain a copy of the License at | |
| 7 | - * | |
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | - * | |
| 10 | - * Unless required by applicable law or agreed to in writing, software | |
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | - * See the License for the specific language governing permissions and | |
| 14 | - * limitations under the License. | |
| 15 | - */ | |
| 16 | -package com.datastax.driver.core; | |
| 17 | - | |
| 18 | -import java.net.InetSocketAddress; | |
| 19 | -import java.util.*; | |
| 20 | - | |
| 21 | -import org.testng.annotations.Test; | |
| 22 | -import static org.testng.Assert.assertEquals; | |
| 23 | - | |
| 24 | -/** | |
| 25 | - * Test we correctly process and print schema. | |
| 26 | - */ | |
| 27 | -public class SchemaTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 28 | - | |
| 29 | - private static final Map<String, String> cql3 = new HashMap<String, String>(); | |
| 30 | - private static final Map<String, String> compact = new HashMap<String, String>(); | |
| 31 | - | |
| 32 | - private static String withOptions; | |
| 33 | - | |
| 34 | - @Override | |
| 35 | - protected Collection<String> getTableDefinitions() { | |
| 36 | - | |
| 37 | - String sparse = String.format("CREATE TABLE %s.sparse (\n" | |
| 38 | - + " k text,\n" | |
| 39 | - + " c1 int,\n" | |
| 40 | - + " c2 float,\n" | |
| 41 | - + " l list<text>,\n" | |
| 42 | - + " v int,\n" | |
| 43 | - + " PRIMARY KEY (k, c1, c2)\n" | |
| 44 | - + ");", keyspace); | |
| 45 | - | |
| 46 | - String st = String.format("CREATE TABLE %s.static (\n" | |
| 47 | - + " k text,\n" | |
| 48 | - + " i int,\n" | |
| 49 | - + " m map<text, timeuuid>,\n" | |
| 50 | - + " v int,\n" | |
| 51 | - + " PRIMARY KEY (k)\n" | |
| 52 | - + ");", keyspace); | |
| 53 | - | |
| 54 | - String counters = String.format("CREATE TABLE %s.counters (\n" | |
| 55 | - + " k text,\n" | |
| 56 | - + " c counter,\n" | |
| 57 | - + " PRIMARY KEY (k)\n" | |
| 58 | - + ");", keyspace); | |
| 59 | - | |
| 60 | - String compactStatic = String.format("CREATE TABLE %s.compact_static (\n" | |
| 61 | - + " k text,\n" | |
| 62 | - + " i int,\n" | |
| 63 | - + " t timeuuid,\n" | |
| 64 | - + " v int,\n" | |
| 65 | - + " PRIMARY KEY (k)\n" | |
| 66 | - + ") WITH COMPACT STORAGE;", keyspace); | |
| 67 | - | |
| 68 | - String compactDynamic = String.format("CREATE TABLE %s.compact_dynamic (\n" | |
| 69 | - + " k text,\n" | |
| 70 | - + " c int,\n" | |
| 71 | - + " v timeuuid,\n" | |
| 72 | - + " PRIMARY KEY (k, c)\n" | |
| 73 | - + ") WITH COMPACT STORAGE;", keyspace); | |
| 74 | - | |
| 75 | - String compactComposite = String.format("CREATE TABLE %s.compact_composite (\n" | |
| 76 | - + " k text,\n" | |
| 77 | - + " c1 int,\n" | |
| 78 | - + " c2 float,\n" | |
| 79 | - + " c3 double,\n" | |
| 80 | - + " v timeuuid,\n" | |
| 81 | - + " PRIMARY KEY (k, c1, c2, c3)\n" | |
| 82 | - + ") WITH COMPACT STORAGE;", keyspace); | |
| 83 | - | |
| 84 | - cql3.put("sparse", sparse); | |
| 85 | - cql3.put("static", st); | |
| 86 | - cql3.put("counters", counters); | |
| 87 | - compact.put("compact_static", compactStatic); | |
| 88 | - compact.put("compact_dynamic", compactDynamic); | |
| 89 | - compact.put("compact_composite", compactComposite); | |
| 90 | - | |
| 91 | - withOptions = String.format("CREATE TABLE %s.with_options (\n" | |
| 92 | - + " k text,\n" | |
| 93 | - + " v1 int,\n" | |
| 94 | - + " v2 int,\n" | |
| 95 | - + " i int,\n" | |
| 96 | - + " PRIMARY KEY (k, v1, v2)\n" | |
| 97 | - + ") WITH CLUSTERING ORDER BY (v1 DESC, v2 ASC)\n" | |
| 98 | - + " AND read_repair_chance = 0.5\n" | |
| 99 | - + " AND dclocal_read_repair_chance = 0.6\n" | |
| 100 | - + " AND replicate_on_write = true\n" | |
| 101 | - + " AND gc_grace_seconds = 42\n" | |
| 102 | - + " AND bloom_filter_fp_chance = 0.01\n" | |
| 103 | - + " AND caching = 'ALL'\n" | |
| 104 | - + " AND comment = 'My awesome table'\n" | |
| 105 | - + " AND compaction = { 'class' : 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy', 'sstable_size_in_mb' : 15 }\n" | |
| 106 | - + " AND compression = { 'sstable_compression' : 'org.apache.cassandra.io.compress.SnappyCompressor', 'chunk_length_kb' : 128 };", keyspace); | |
| 107 | - | |
| 108 | - List<String> allDefs = new ArrayList<String>(); | |
| 109 | - allDefs.addAll(cql3.values()); | |
| 110 | - allDefs.addAll(compact.values()); | |
| 111 | - allDefs.add(withOptions); | |
| 112 | - return allDefs; | |
| 113 | - } | |
| 114 | - | |
| 115 | - private static String stripOptions(String def, boolean keepFirst) { | |
| 116 | - if (keepFirst) | |
| 117 | - return def.split("\n AND ")[0] + ';'; | |
| 118 | - else | |
| 119 | - return def.split(" WITH ")[0] + ';'; | |
| 120 | - } | |
| 121 | - | |
| 122 | - // Note: this test is a bit fragile in the sense that it rely on the exact | |
| 123 | - // string formatting of exportAsString, but it's a very simple/convenient | |
| 124 | - // way to check we correctly handle schemas so it's probably not so bad. | |
| 125 | - // In particular, exportAsString *does not* guarantee that you'll get | |
| 126 | - // exactly the same string than the one used to create the table. | |
| 127 | - @Test(groups = "short") | |
| 128 | - public void schemaExportTest() { | |
| 129 | - | |
| 130 | - KeyspaceMetadata metadata = cluster.getMetadata().getKeyspace(keyspace); | |
| 131 | - | |
| 132 | - for (Map.Entry<String, String> tableEntry : cql3.entrySet()) { | |
| 133 | - String table = tableEntry.getKey(); | |
| 134 | - String def = tableEntry.getValue(); | |
| 135 | - assertEquals(stripOptions(metadata.getTable(table).exportAsString(), false), def); | |
| 136 | - } | |
| 137 | - | |
| 138 | - for (Map.Entry<String, String> tableEntry : compact.entrySet()) { | |
| 139 | - String table = tableEntry.getKey(); | |
| 140 | - String def = tableEntry.getValue(); | |
| 141 | - assertEquals(stripOptions(metadata.getTable(table).exportAsString(), true), def); | |
| 142 | - } | |
| 143 | - } | |
| 144 | - | |
| 145 | - // Same remark as the preceding test | |
| 146 | - @Test(groups = "short") | |
| 147 | - public void schemaExportOptionsTest() { | |
| 148 | - TableMetadata metadata = cluster.getMetadata().getKeyspace(keyspace).getTable("with_options"); | |
| 149 | - | |
| 150 | - String withOpts = withOptions; | |
| 151 | - VersionNumber version = TestUtils.findHost(cluster, 1).getCassandraVersion(); | |
| 152 | - | |
| 153 | - if (version.getMajor() == 2) { | |
| 154 | - // Strip the last ';' | |
| 155 | - withOpts = withOpts.substring(0, withOpts.length() - 1) + '\n'; | |
| 156 | - | |
| 157 | - // With C* 2.x we'll have a few additional options | |
| 158 | - withOpts += " AND default_time_to_live = 0\n" | |
| 159 | - + " AND speculative_retry = '99.0PERCENTILE'\n"; | |
| 160 | - | |
| 161 | - if (version.getMinor() == 0) { | |
| 162 | - // With 2.0 we'll have one more options | |
| 163 | - withOpts += " AND index_interval = 128;"; | |
| 164 | - } else { | |
| 165 | - // With 2.1 we have different options, the caching option changes and replicate_on_write disappears | |
| 166 | - withOpts += " AND min_index_interval = 128\n" | |
| 167 | - + " AND max_index_interval = 2048;"; | |
| 168 | - | |
| 169 | - withOpts = withOpts.replace("caching = 'ALL'", | |
| 170 | - "caching = { 'keys' : 'ALL', 'rows_per_partition' : 'ALL' }") | |
| 171 | - .replace(" AND replicate_on_write = true\n", ""); | |
| 172 | - } | |
| 173 | - } | |
| 174 | - assertEquals(metadata.exportAsString(), withOpts); | |
| 175 | - } | |
| 176 | -} |
added driver-core/src/test/java/com/datastax/driver/core/TableMetadataAssert.java +48 −0
| @@ -0,0 +1,48 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import org.assertj.core.api.AbstractAssert; | |
| 19 | + | |
| 20 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 21 | + | |
| 22 | +public class TableMetadataAssert extends AbstractAssert<TableMetadataAssert, TableMetadata> { | |
| 23 | + | |
| 24 | + protected TableMetadataAssert(TableMetadata actual) { | |
| 25 | + super(actual, TableMetadataAssert.class); | |
| 26 | + } | |
| 27 | + | |
| 28 | + public TableMetadataAssert hasName(String name) { | |
| 29 | + assertThat(actual.getName()).isEqualTo(name); | |
| 30 | + return this; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public TableMetadataAssert isCompactStorage() { | |
| 34 | + assertThat(actual.getOptions().isCompactStorage()).isTrue(); | |
| 35 | + return this; | |
| 36 | + } | |
| 37 | + | |
| 38 | + public TableMetadataAssert isNotCompactStorage() { | |
| 39 | + assertThat(actual.getOptions().isCompactStorage()).isFalse(); | |
| 40 | + return this; | |
| 41 | + } | |
| 42 | + | |
| 43 | + public TableMetadataAssert hasNumberOfColumns(int expected) { | |
| 44 | + assertThat(actual.getColumns().size()).isEqualTo(expected); | |
| 45 | + return this; | |
| 46 | + } | |
| 47 | + | |
| 48 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/TableMetadataTest.java +337 −11
| @@ -16,25 +16,351 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import java.util.Collection; |
| 19 | +import java.util.Collections; | |
| 19 | 20 | |
| 20 | -import com.google.common.collect.Lists; | |
| 21 | 21 | import org.testng.annotations.Test; |
| 22 | 22 | |
| 23 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 23 | +import static org.assertj.core.api.Assertions.entry; | |
| 24 | + | |
| 25 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 26 | +import static com.datastax.driver.core.DataType.*; | |
| 27 | +import static com.datastax.driver.core.TableMetadata.Order.ASC; | |
| 28 | +import static com.datastax.driver.core.TableMetadata.Order.DESC; | |
| 24 | 29 | |
| 25 | 30 | public class TableMetadataTest extends CCMBridge.PerClassSingleNodeCluster { |
| 26 | - @Override | |
| 27 | - protected Collection<String> getTableDefinitions() { | |
| 28 | - return Lists.newArrayList( | |
| 29 | - "CREATE TABLE single_quote (\n" | |
| 30 | - + " c1 int PRIMARY KEY\n" | |
| 31 | - + ") WITH comment = 'comment with single quote '' should work'" | |
| 32 | - ); | |
| 31 | + | |
| 32 | + @Test(groups = "short") | |
| 33 | + public void should_parse_table_without_clustering_columns() { | |
| 34 | + // given | |
| 35 | + String cql = String.format("CREATE TABLE %s.static (\n" | |
| 36 | + + " k text,\n" | |
| 37 | + + " i int,\n" | |
| 38 | + + " m map<text, timeuuid>,\n" | |
| 39 | + + " v int,\n" | |
| 40 | + + " PRIMARY KEY (k)\n" | |
| 41 | + + ");", keyspace); | |
| 42 | + // when | |
| 43 | + session.execute(cql); | |
| 44 | + TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("static"); | |
| 45 | + // then | |
| 46 | + assertThat(table).isNotNull().hasName("static").hasNumberOfColumns(4).isNotCompactStorage(); | |
| 47 | + assertThat(table.getColumns().get(0)).isNotNull().hasName("k").isPartitionKey().hasType(text()); | |
| 48 | + assertThat(table.getColumns().get(1)).isNotNull().hasName("i").isRegularColumn().hasType(cint()); | |
| 49 | + assertThat(table.getColumns().get(2)).isNotNull().hasName("m").isRegularColumn().hasType(map(text(), timeuuid())); | |
| 50 | + assertThat(table.getColumns().get(3)).isNotNull().hasName("v").isRegularColumn().hasType(cint()); | |
| 51 | + } | |
| 52 | + | |
| 53 | + @Test(groups = "short") | |
| 54 | + public void should_parse_table_with_clustering_columns() { | |
| 55 | + // given | |
| 56 | + String cql = String.format("CREATE TABLE %s.sparse (\n" | |
| 57 | + + " k text,\n" | |
| 58 | + + " c1 int,\n" | |
| 59 | + + " c2 float,\n" | |
| 60 | + + " l list<text>,\n" | |
| 61 | + + " v int,\n" | |
| 62 | + + " PRIMARY KEY (k, c1, c2)\n" | |
| 63 | + + ");", keyspace); | |
| 64 | + // when | |
| 65 | + session.execute(cql); | |
| 66 | + TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("sparse"); | |
| 67 | + // then | |
| 68 | + assertThat(table).isNotNull().hasName("sparse").hasNumberOfColumns(5).isNotCompactStorage(); | |
| 69 | + assertThat(table.getColumns().get(0)).isNotNull().hasName("k").isPartitionKey().hasType(text()); | |
| 70 | + assertThat(table.getColumns().get(1)).isNotNull().hasName("c1").isClusteringColumn().hasClusteringOrder(ASC).hasType(cint()); | |
| 71 | + assertThat(table.getColumns().get(2)).isNotNull().hasName("c2").isClusteringColumn().hasClusteringOrder(ASC).hasType(cfloat()); | |
| 72 | + assertThat(table.getColumns().get(3)).isNotNull().hasName("l").isRegularColumn().hasType(list(text())); | |
| 73 | + assertThat(table.getColumns().get(4)).isNotNull().hasName("v").isRegularColumn().hasType(cint()); | |
| 74 | + } | |
| 75 | + | |
| 76 | + @Test(groups = "short") | |
| 77 | + public void should_parse_counter_table() { | |
| 78 | + // given | |
| 79 | + String cql = String.format("CREATE TABLE %s.counters (\n" | |
| 80 | + + " k text,\n" | |
| 81 | + + " c counter,\n" | |
| 82 | + + " PRIMARY KEY (k)\n" | |
| 83 | + + ");", keyspace); | |
| 84 | + // when | |
| 85 | + session.execute(cql); | |
| 86 | + TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("counters"); | |
| 87 | + // then | |
| 88 | + assertThat(table).isNotNull().hasName("counters").hasNumberOfColumns(2).isNotCompactStorage(); | |
| 89 | + assertThat(table.getColumns().get(0)).isNotNull().hasName("k").isPartitionKey().hasType(text()); | |
| 90 | + assertThat(table.getColumns().get(1)).isNotNull().hasName("c").isRegularColumn().hasType(counter()); | |
| 91 | + } | |
| 92 | + | |
| 93 | + @Test(groups = "short") | |
| 94 | + public void should_parse_compact_static_table() { | |
| 95 | + // given | |
| 96 | + String cql = String.format("CREATE TABLE %s.compact_static (\n" | |
| 97 | + + " k text,\n" | |
| 98 | + + " i int,\n" | |
| 99 | + + " t timeuuid,\n" | |
| 100 | + + " v int,\n" | |
| 101 | + + " PRIMARY KEY (k)\n" | |
| 102 | + + ") WITH COMPACT STORAGE;", keyspace); | |
| 103 | + // when | |
| 104 | + session.execute(cql); | |
| 105 | + TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("compact_static"); | |
| 106 | + // then | |
| 107 | + assertThat(table).isNotNull().hasName("compact_static").hasNumberOfColumns(4).isCompactStorage(); | |
| 108 | + assertThat(table.getColumns().get(0)).isNotNull().hasName("k").isPartitionKey().hasType(text()); | |
| 109 | + assertThat(table.getColumns().get(1)).isNotNull().hasName("i").isRegularColumn().hasType(cint()); | |
| 110 | + assertThat(table.getColumns().get(2)).isNotNull().hasName("t").isRegularColumn().hasType(timeuuid()); | |
| 111 | + assertThat(table.getColumns().get(3)).isNotNull().hasName("v").isRegularColumn().hasType(cint()); | |
| 112 | + } | |
| 113 | + | |
| 114 | + @Test(groups = "short") | |
| 115 | + public void should_parse_compact_dynamic_table() { | |
| 116 | + // given | |
| 117 | + String cql = String.format("CREATE TABLE %s.compact_dynamic (\n" | |
| 118 | + + " k text,\n" | |
| 119 | + + " c int,\n" | |
| 120 | + + " v timeuuid,\n" | |
| 121 | + + " PRIMARY KEY (k, c)\n" | |
| 122 | + + ") WITH COMPACT STORAGE;", keyspace); | |
| 123 | + // when | |
| 124 | + session.execute(cql); | |
| 125 | + TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("compact_dynamic"); | |
| 126 | + // then | |
| 127 | + assertThat(table).isNotNull().hasName("compact_dynamic").hasNumberOfColumns(3).isCompactStorage(); | |
| 128 | + assertThat(table.getColumns().get(0)).isNotNull().hasName("k").isPartitionKey().hasType(text()); | |
| 129 | + assertThat(table.getColumns().get(1)).isNotNull().hasName("c").isClusteringColumn().hasClusteringOrder(ASC).hasType(cint()); | |
| 130 | + assertThat(table.getColumns().get(2)).isNotNull().hasName("v").isRegularColumn().hasType(timeuuid()); | |
| 131 | + } | |
| 132 | + | |
| 133 | + @Test(groups = "short") | |
| 134 | + public void should_parse_compact_table_with_multiple_clustering_columns() { | |
| 135 | + // given | |
| 136 | + String cql = String.format("CREATE TABLE %s.compact_composite (\n" | |
| 137 | + + " k text,\n" | |
| 138 | + + " c1 int,\n" | |
| 139 | + + " c2 float,\n" | |
| 140 | + + " c3 double,\n" | |
| 141 | + + " v timeuuid,\n" | |
| 142 | + + " PRIMARY KEY (k, c1, c2, c3)\n" | |
| 143 | + + ") WITH COMPACT STORAGE;", keyspace); | |
| 144 | + // when | |
| 145 | + session.execute(cql); | |
| 146 | + TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("compact_composite"); | |
| 147 | + // then | |
| 148 | + assertThat(table).isNotNull().hasName("compact_composite").hasNumberOfColumns(5).isCompactStorage(); | |
| 149 | + assertThat(table.getColumns().get(0)).isNotNull().hasName("k").isPartitionKey().hasType(text()); | |
| 150 | + assertThat(table.getColumns().get(1)).isNotNull().hasName("c1").isClusteringColumn().hasClusteringOrder(ASC).hasType(cint()); | |
| 151 | + assertThat(table.getColumns().get(2)).isNotNull().hasName("c2").isClusteringColumn().hasClusteringOrder(ASC).hasType(cfloat()); | |
| 152 | + assertThat(table.getColumns().get(3)).isNotNull().hasName("c3").isClusteringColumn().hasClusteringOrder(ASC).hasType(cdouble()); | |
| 153 | + assertThat(table.getColumns().get(4)).isNotNull().hasName("v").isRegularColumn().hasType(timeuuid()); | |
| 154 | + } | |
| 155 | + | |
| 156 | + @Test(groups = "short") | |
| 157 | + public void should_parse_table_options() { | |
| 158 | + // given | |
| 159 | + String cql = String.format("CREATE TABLE %s.with_options (\n" | |
| 160 | + + " k text,\n" | |
| 161 | + + " c1 int,\n" | |
| 162 | + + " c2 int,\n" | |
| 163 | + + " i int,\n" | |
| 164 | + + " PRIMARY KEY (k, c1, c2)\n" | |
| 165 | + + ") WITH CLUSTERING ORDER BY (c1 DESC, c2 ASC)\n" | |
| 166 | + + " AND read_repair_chance = 0.5\n" | |
| 167 | + + " AND dclocal_read_repair_chance = 0.6\n" | |
| 168 | + + " AND replicate_on_write = true\n" | |
| 169 | + + " AND gc_grace_seconds = 42\n" | |
| 170 | + + " AND bloom_filter_fp_chance = 0.01\n" | |
| 171 | + + " AND caching = 'ALL'\n" | |
| 172 | + + " AND comment = 'My awesome table'\n" | |
| 173 | + + " AND compaction = { 'class' : 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy', 'sstable_size_in_mb' : 15 }\n" | |
| 174 | + + " AND compression = { 'sstable_compression' : 'org.apache.cassandra.io.compress.SnappyCompressor', 'chunk_length_kb' : 128 };", | |
| 175 | + keyspace); | |
| 176 | + // when | |
| 177 | + session.execute(cql); | |
| 178 | + TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("with_options"); | |
| 179 | + // then | |
| 180 | + assertThat(table).isNotNull().hasName("with_options").hasNumberOfColumns(4).isNotCompactStorage(); | |
| 181 | + assertThat(table.getColumns().get(0)).isNotNull().hasName("k").isPartitionKey().hasType(text()); | |
| 182 | + assertThat(table.getColumns().get(1)).isNotNull().hasName("c1").isClusteringColumn().hasClusteringOrder(DESC).hasType(cint()); | |
| 183 | + assertThat(table.getColumns().get(2)).isNotNull().hasName("c2").isClusteringColumn().hasClusteringOrder(ASC).hasType(cint()); | |
| 184 | + assertThat(table.getColumns().get(3)).isNotNull().hasName("i").isRegularColumn().hasType(cint()); | |
| 185 | + assertThat(table); | |
| 186 | + VersionNumber version = TestUtils.findHost(cluster, 1).getCassandraVersion(); | |
| 187 | + // Cassandra 3.0 + | |
| 188 | + if (version.getMajor() > 2) { | |
| 189 | + | |
| 190 | + assertThat(table.getOptions().getReadRepairChance()).isEqualTo(0.5); | |
| 191 | + assertThat(table.getOptions().getLocalReadRepairChance()).isEqualTo(0.6); | |
| 192 | + assertThat(table.getOptions().getGcGraceInSeconds()).isEqualTo(42); | |
| 193 | + assertThat(table.getOptions().getBloomFilterFalsePositiveChance()).isEqualTo(0.01); | |
| 194 | + assertThat(table.getOptions().getComment()).isEqualTo("My awesome table"); | |
| 195 | + assertThat(table.getOptions().getCaching()).contains(entry("keys", "ALL" )); | |
| 196 | + assertThat(table.getOptions().getCaching()).contains(entry("rows_per_partition", "ALL")); | |
| 197 | + assertThat(table.getOptions().getCompaction()).contains(entry("class", "org.apache.cassandra.db.compaction.LeveledCompactionStrategy")); | |
| 198 | + assertThat(table.getOptions().getCompaction()).contains(entry("sstable_size_in_mb", "15")); | |
| 199 | + assertThat(table.getOptions().getCompression()).contains(entry("class", "org.apache.cassandra.io.compress.SnappyCompressor")); // sstable_compression becomes class | |
| 200 | + assertThat(table.getOptions().getCompression()).contains(entry("chunk_length_in_kb", "128")); // note the "in" prefix | |
| 201 | + assertThat(table.getOptions().getDefaultTimeToLive()).isEqualTo(0); | |
| 202 | + assertThat(table.getOptions().getSpeculativeRetry()).isEqualTo("99.0PERCENTILE"); | |
| 203 | + assertThat(table.getOptions().getIndexInterval()).isNull(); | |
| 204 | + assertThat(table.getOptions().getMinIndexInterval()).isEqualTo(128); | |
| 205 | + assertThat(table.getOptions().getMaxIndexInterval()).isEqualTo(2048); | |
| 206 | + assertThat(table.getOptions().getReplicateOnWrite()).isTrue(); // default | |
| 207 | + assertThat(table.asCQLQuery()) | |
| 208 | + .contains("read_repair_chance = 0.5") | |
| 209 | + .contains("dclocal_read_repair_chance = 0.6") | |
| 210 | + .contains("gc_grace_seconds = 42") | |
| 211 | + .contains("bloom_filter_fp_chance = 0.01") | |
| 212 | + .contains("comment = 'My awesome table'") | |
| 213 | + .contains("'keys' : 'ALL'") | |
| 214 | + .contains("'rows_per_partition' : 'ALL'") | |
| 215 | + .contains("'class' : 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy'") | |
| 216 | + .contains("'sstable_size_in_mb' : 15") | |
| 217 | + .contains("'class' : 'org.apache.cassandra.io.compress.SnappyCompressor'") // sstable_compression becomes class | |
| 218 | + .contains("'chunk_length_in_kb' : 128") // note the "in" prefix | |
| 219 | + .contains("default_time_to_live = 0") | |
| 220 | + .contains("speculative_retry = '99.0PERCENTILE'") | |
| 221 | + .contains("min_index_interval = 128") | |
| 222 | + .contains("max_index_interval = 2048") | |
| 223 | + .doesNotContain(" index_interval") | |
| 224 | + .doesNotContain("replicate_on_write"); | |
| 225 | + | |
| 226 | + // Cassandra 2.1 and 2.2 | |
| 227 | + } else if (version.getMajor() == 2 && version.getMinor() > 0) { | |
| 228 | + | |
| 229 | + // With 2.1 we have different options, the caching option changes and replicate_on_write disappears | |
| 230 | + assertThat(table.getOptions().getReadRepairChance()).isEqualTo(0.5); | |
| 231 | + assertThat(table.getOptions().getLocalReadRepairChance()).isEqualTo(0.6); | |
| 232 | + assertThat(table.getOptions().getGcGraceInSeconds()).isEqualTo(42); | |
| 233 | + assertThat(table.getOptions().getBloomFilterFalsePositiveChance()).isEqualTo(0.01); | |
| 234 | + assertThat(table.getOptions().getComment()).isEqualTo("My awesome table"); | |
| 235 | + assertThat(table.getOptions().getCaching()).contains(entry("keys", "ALL" )); | |
| 236 | + assertThat(table.getOptions().getCaching()).contains(entry("rows_per_partition", "ALL")); | |
| 237 | + assertThat(table.getOptions().getCompaction()).contains(entry("class", "org.apache.cassandra.db.compaction.LeveledCompactionStrategy")); | |
| 238 | + assertThat(table.getOptions().getCompaction()).contains(entry("sstable_size_in_mb", "15")); | |
| 239 | + assertThat(table.getOptions().getCompression()).contains(entry("sstable_compression", "org.apache.cassandra.io.compress.SnappyCompressor")); | |
| 240 | + assertThat(table.getOptions().getCompression()).contains(entry("chunk_length_kb", "128")); | |
| 241 | + assertThat(table.getOptions().getDefaultTimeToLive()).isEqualTo(0); | |
| 242 | + assertThat(table.getOptions().getSpeculativeRetry()).isEqualTo("99.0PERCENTILE"); | |
| 243 | + assertThat(table.getOptions().getIndexInterval()).isNull(); | |
| 244 | + assertThat(table.getOptions().getMinIndexInterval()).isEqualTo(128); | |
| 245 | + assertThat(table.getOptions().getMaxIndexInterval()).isEqualTo(2048); | |
| 246 | + assertThat(table.getOptions().getReplicateOnWrite()).isTrue(); // default | |
| 247 | + assertThat(table.asCQLQuery()) | |
| 248 | + .contains("read_repair_chance = 0.5") | |
| 249 | + .contains("dclocal_read_repair_chance = 0.6") | |
| 250 | + .contains("gc_grace_seconds = 42") | |
| 251 | + .contains("bloom_filter_fp_chance = 0.01") | |
| 252 | + .contains("comment = 'My awesome table'") | |
| 253 | + .contains("'keys' : 'ALL'") | |
| 254 | + .contains("'rows_per_partition' : 'ALL'") | |
| 255 | + .contains("'class' : 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy'") | |
| 256 | + .contains("'sstable_size_in_mb' : 15") | |
| 257 | + .contains("'sstable_compression' : 'org.apache.cassandra.io.compress.SnappyCompressor'") | |
| 258 | + .contains("'chunk_length_kb' : 128") | |
| 259 | + .contains("default_time_to_live = 0") | |
| 260 | + .contains("speculative_retry = '99.0PERCENTILE'") | |
| 261 | + .contains("min_index_interval = 128") | |
| 262 | + .contains("max_index_interval = 2048") | |
| 263 | + .doesNotContain(" index_interval") | |
| 264 | + .doesNotContain("replicate_on_write"); | |
| 265 | + | |
| 266 | + // Cassandra 2.0 | |
| 267 | + } else if (version.getMajor() == 2 && version.getMinor() == 0) { | |
| 268 | + | |
| 269 | + assertThat(table.getOptions().getReadRepairChance()).isEqualTo(0.5); | |
| 270 | + assertThat(table.getOptions().getLocalReadRepairChance()).isEqualTo(0.6); | |
| 271 | + assertThat(table.getOptions().getGcGraceInSeconds()).isEqualTo(42); | |
| 272 | + assertThat(table.getOptions().getBloomFilterFalsePositiveChance()).isEqualTo(0.01); | |
| 273 | + assertThat(table.getOptions().getComment()).isEqualTo("My awesome table"); | |
| 274 | + assertThat(table.getOptions().getCaching()).contains(entry("keys", "ALL" )); | |
| 275 | + assertThat(table.getOptions().getCaching()).doesNotContain(entry("rows_per_partition", "ALL")); // 2.1 + | |
| 276 | + assertThat(table.getOptions().getCompaction()).contains(entry("class", "org.apache.cassandra.db.compaction.LeveledCompactionStrategy")); | |
| 277 | + assertThat(table.getOptions().getCompaction()).contains(entry("sstable_size_in_mb", "15")); | |
| 278 | + assertThat(table.getOptions().getCompression()).contains(entry("sstable_compression", "org.apache.cassandra.io.compress.SnappyCompressor")); | |
| 279 | + assertThat(table.getOptions().getCompression()).contains(entry("chunk_length_kb", "128")); | |
| 280 | + assertThat(table.getOptions().getDefaultTimeToLive()).isEqualTo(0); | |
| 281 | + assertThat(table.getOptions().getSpeculativeRetry()).isEqualTo("99.0PERCENTILE"); // default | |
| 282 | + assertThat(table.getOptions().getIndexInterval()).isEqualTo(128); | |
| 283 | + assertThat(table.getOptions().getMinIndexInterval()).isNull(); | |
| 284 | + assertThat(table.getOptions().getMaxIndexInterval()).isNull(); | |
| 285 | + assertThat(table.getOptions().getReplicateOnWrite()).isTrue(); // explicitly set | |
| 286 | + assertThat(table.asCQLQuery()) | |
| 287 | + .contains("read_repair_chance = 0.5") | |
| 288 | + .contains("dclocal_read_repair_chance = 0.6") | |
| 289 | + .contains("gc_grace_seconds = 42") | |
| 290 | + .contains("bloom_filter_fp_chance = 0.01") | |
| 291 | + .contains("comment = 'My awesome table'") | |
| 292 | + .contains("caching = 'ALL'") | |
| 293 | + .contains("'class' : 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy'") | |
| 294 | + .contains("'sstable_size_in_mb' : 15") | |
| 295 | + .contains("'sstable_compression' : 'org.apache.cassandra.io.compress.SnappyCompressor'") | |
| 296 | + .contains("'chunk_length_kb' : 128") | |
| 297 | + .contains("replicate_on_write = true") | |
| 298 | + .contains("index_interval = 128") | |
| 299 | + .contains("speculative_retry = '99.0PERCENTILE'") | |
| 300 | + .contains("default_time_to_live = 0") | |
| 301 | + .doesNotContain("min_index_interval") // 2.1 + | |
| 302 | + .doesNotContain("max_index_interval"); // 2.1 + | |
| 303 | + | |
| 304 | + // Cassandra 1.2 | |
| 305 | + } else { | |
| 306 | + | |
| 307 | + assertThat(table.getOptions().getReadRepairChance()).isEqualTo(0.5); | |
| 308 | + assertThat(table.getOptions().getLocalReadRepairChance()).isEqualTo(0.6); | |
| 309 | + assertThat(table.getOptions().getGcGraceInSeconds()).isEqualTo(42); | |
| 310 | + assertThat(table.getOptions().getBloomFilterFalsePositiveChance()).isEqualTo(0.01); | |
| 311 | + assertThat(table.getOptions().getComment()).isEqualTo("My awesome table"); | |
| 312 | + assertThat(table.getOptions().getCaching()).contains(entry("keys", "ALL" )); | |
| 313 | + assertThat(table.getOptions().getCaching()).doesNotContain(entry("rows_per_partition", "ALL")); // 2.1 + | |
| 314 | + assertThat(table.getOptions().getCompaction()).contains(entry("class", "org.apache.cassandra.db.compaction.LeveledCompactionStrategy")); | |
| 315 | + assertThat(table.getOptions().getCompaction()).contains(entry("sstable_size_in_mb", "15")); | |
| 316 | + assertThat(table.getOptions().getCompression()).contains(entry("sstable_compression", "org.apache.cassandra.io.compress.SnappyCompressor")); | |
| 317 | + assertThat(table.getOptions().getCompression()).contains(entry("chunk_length_kb", "128")); | |
| 318 | + assertThat(table.getOptions().getDefaultTimeToLive()).isEqualTo(0); // default | |
| 319 | + assertThat(table.getOptions().getSpeculativeRetry()).isEqualTo("NONE"); // default | |
| 320 | + assertThat(table.getOptions().getIndexInterval()).isNull(); | |
| 321 | + assertThat(table.getOptions().getMinIndexInterval()).isNull(); | |
| 322 | + assertThat(table.getOptions().getMaxIndexInterval()).isNull(); | |
| 323 | + assertThat(table.getOptions().getReplicateOnWrite()).isTrue(); // explicitly set | |
| 324 | + assertThat(table.asCQLQuery()) | |
| 325 | + .contains("read_repair_chance = 0.5") | |
| 326 | + .contains("dclocal_read_repair_chance = 0.6") | |
| 327 | + .contains("gc_grace_seconds = 42") | |
| 328 | + .contains("bloom_filter_fp_chance = 0.01") | |
| 329 | + .contains("comment = 'My awesome table'") | |
| 330 | + .contains("caching = 'ALL'") | |
| 331 | + .contains("'class' : 'org.apache.cassandra.db.compaction.LeveledCompactionStrategy'") | |
| 332 | + .contains("'sstable_size_in_mb' : 15") | |
| 333 | + .contains("'sstable_compression' : 'org.apache.cassandra.io.compress.SnappyCompressor'") | |
| 334 | + .contains("'chunk_length_kb' : 128") | |
| 335 | + .contains("replicate_on_write = true") | |
| 336 | + .doesNotContain("index_interval") // 2.0 | |
| 337 | + .doesNotContain("min_index_interval") // 2.1 + | |
| 338 | + .doesNotContain("max_index_interval") // 2.1 + | |
| 339 | + .doesNotContain("speculative_retry") // 2.0 + | |
| 340 | + .doesNotContain("default_time_to_live"); // 2.0 + | |
| 341 | + | |
| 342 | + } | |
| 343 | + | |
| 33 | 344 | } |
| 34 | 345 | |
| 35 | 346 | @Test(groups = "short") |
| 36 | 347 | public void should_escape_single_quote_table_comment() { |
| 348 | + // given | |
| 349 | + String cql = String.format("CREATE TABLE %s.single_quote (\n" | |
| 350 | + + " c1 int PRIMARY KEY\n" | |
| 351 | + + ") WITH comment = 'comment with single quote '' should work'", | |
| 352 | + keyspace); | |
| 353 | + // when | |
| 354 | + session.execute(cql); | |
| 37 | 355 | TableMetadata table = cluster.getMetadata().getKeyspace(keyspace).getTable("single_quote"); |
| 38 | - assertThat(table.asCQLQuery()).contains("comment with single quote '' should work"); | |
| 356 | + // then | |
| 357 | + assertThat(table.getOptions().getComment()).isEqualTo("comment with single quote ' should work"); | |
| 358 | + assertThat(table.asCQLQuery()).contains("comment = 'comment with single quote '' should work'"); | |
| 39 | 359 | } |
| 40 | -} | |
| No newline at the end of the file | ||
| 360 | + | |
| 361 | + @Override | |
| 362 | + protected Collection<String> getTableDefinitions() { | |
| 363 | + return Collections.emptyList(); | |
| 364 | +} | |
| 365 | + | |
| 366 | +} |