DATACASS-518 - Polishing.

Simplify unit tests by inlining tests into tests class and removing nested test enclosure classes.
This commit is contained in:
Mark Paluch
2018-01-18 11:17:47 +01:00
parent 3eedafb3e4
commit 4538b2aa56
4 changed files with 136 additions and 366 deletions

View File

@@ -64,11 +64,11 @@ public class CreateTableCqlGeneratorIntegrationTests extends AbstractKeyspaceCre
.clusteredKeyColumn("age", DataType.smallint()) //
.column("name", DataType.ascii());
session.execute(CreateTableCqlGenerator.toCql(table));
TableMetadata person = cluster.getMetadata().getKeyspace(getKeyspace()).getTable("person");
assertThat(person.getPartitionKey()).hasSize(2);
assertThat(person.getClusteringColumns()).hasSize(2);
session.execute(CreateTableCqlGenerator.toCql(table));
}
@Test // DATACASS-518

View File

@@ -17,21 +17,14 @@ package org.springframework.data.cassandra.core.cql.generator;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.cassandra.core.cql.CqlIdentifier.*;
import static org.springframework.data.cassandra.core.cql.generator.CreateTableCqlGenerator.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.cassandra.core.cql.CqlIdentifier;
import org.springframework.data.cassandra.core.cql.Ordering;
import org.springframework.data.cassandra.core.cql.ReservedKeyword;
import org.springframework.data.cassandra.core.cql.keyspace.CreateTableSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.Option;
import org.springframework.data.cassandra.core.cql.keyspace.TableOption;
@@ -51,7 +44,126 @@ import com.datastax.driver.core.DataType;
*/
public class CreateTableCqlGeneratorUnitTests {
private static final Logger log = LoggerFactory.getLogger(CreateTableCqlGeneratorUnitTests.class);
@Test
public void shouldGenerateCorrectCQL() {
CqlIdentifier name = of("mytable");
DataType partitionKeyType0 = DataType.text();
CqlIdentifier partitionKey0 = of("partitionKey0");
DataType columnType1 = DataType.text();
String column1 = "column1";
CreateTableSpecification table = CreateTableSpecification.createTable(name)
.partitionKeyColumn(partitionKey0, partitionKeyType0).column(column1, columnType1);
String cql = toCql(table);
assertPreamble(name, cql);
assertColumns(String.format("%s %s, %s %s", partitionKey0, partitionKeyType0, column1, columnType1), cql);
assertPrimaryKey(partitionKey0.toCql(), cql);
}
@Test
public void shouldGenerateCompositePrimaryKey() {
CqlIdentifier name = of("composite_partition_key_table");
DataType partKeyType0 = DataType.text();
CqlIdentifier partKey0 = of("partKey0");
DataType partKeyType1 = DataType.text();
CqlIdentifier partKey1 = of("partKey1");
CqlIdentifier column0 = of("column0");
DataType columnType0 = DataType.text();
CreateTableSpecification table = CreateTableSpecification.createTable(name)
.partitionKeyColumn(partKey0, partKeyType0).partitionKeyColumn(partKey1, partKeyType1)
.column(column0, columnType0);
String cql = toCql(table);
assertPreamble(name, cql);
assertColumns(
String.format("%s %s, %s %s, %s %s", partKey0, partKeyType0, partKey1, partKeyType1, column0, columnType0),
cql);
assertPrimaryKey(String.format("(%s, %s)", partKey0, partKey1), cql);
}
@Test
public void shouldGenerateTableOptions() {
CqlIdentifier name = of("mytable");
DataType partitionKeyType0 = DataType.text();
CqlIdentifier partitionKey0 = of("partitionKey0");
DataType partitionKeyType1 = DataType.timestamp();
CqlIdentifier partitionKey1 = of("create_timestamp");
DataType columnType1 = DataType.text();
CqlIdentifier column1 = of("column1");
Double readRepairChance = 0.5;
CreateTableSpecification table = CreateTableSpecification.createTable(name)
.partitionKeyColumn(partitionKey0, partitionKeyType0).partitionKeyColumn(partitionKey1, partitionKeyType1)
.column(column1, columnType1).with(TableOption.READ_REPAIR_CHANCE, readRepairChance);
String cql = toCql(table);
assertPreamble(name, cql);
assertColumns(String.format("%s %s, %s %s, %s %s", partitionKey0, partitionKeyType0, partitionKey1,
partitionKeyType1, column1, columnType1), cql);
assertPrimaryKey(String.format("(%s, %s)", partitionKey0, partitionKey1), cql);
assertDoubleOption(TableOption.READ_REPAIR_CHANCE.getName(), readRepairChance, cql);
}
@Test
public void shouldGenerateMultipleOptions() {
CqlIdentifier name = of("timeseries_table");
DataType partitionKeyType0 = DataType.timeuuid();
CqlIdentifier partitionKey0 = of("tid");
DataType partitionKeyType1 = DataType.timestamp();
CqlIdentifier partitionKey1 = of("create_timestamp");
DataType columnType1 = DataType.text();
CqlIdentifier column1 = of("data_point");
Double readRepairChance = 0.5;
Double dcLocalReadRepairChance = 0.7;
Double bloomFilterFpChance = 0.001;
Boolean replcateOnWrite = Boolean.FALSE;
Long gcGraceSeconds = 600l;
String comment = "This is My Table";
Map<Option, Object> compactionMap = new LinkedHashMap<>();
Map<Option, Object> compressionMap = new LinkedHashMap<>();
Map<Option, Object> cachingMap = new LinkedHashMap<>();
// Compaction
compactionMap.put(CompactionOption.CLASS, "SizeTieredCompactionStrategy");
compactionMap.put(CompactionOption.MIN_THRESHOLD, "4");
// Compression
compressionMap.put(CompressionOption.SSTABLE_COMPRESSION, "SnappyCompressor");
compressionMap.put(CompressionOption.CHUNK_LENGTH_KB, 128);
compressionMap.put(CompressionOption.CRC_CHECK_CHANCE, 0.75);
// Caching
cachingMap.put(CachingOption.KEYS, KeyCachingOption.ALL);
cachingMap.put(CachingOption.ROWS_PER_PARTITION, "NONE");
CreateTableSpecification table = CreateTableSpecification.createTable(name)
.partitionKeyColumn(partitionKey0, partitionKeyType0).partitionKeyColumn(partitionKey1, partitionKeyType1)
.column(column1, columnType1).with(TableOption.COMPACT_STORAGE)
.with(TableOption.READ_REPAIR_CHANCE, readRepairChance).with(TableOption.COMPACTION, compactionMap)
.with(TableOption.COMPRESSION, compressionMap).with(TableOption.BLOOM_FILTER_FP_CHANCE, bloomFilterFpChance)
.with(TableOption.CACHING, cachingMap).with(TableOption.COMMENT, comment)
.with(TableOption.DCLOCAL_READ_REPAIR_CHANCE, dcLocalReadRepairChance)
.with(TableOption.GC_GRACE_SECONDS, gcGraceSeconds);
String cql = toCql(table);
assertPreamble(name, cql);
assertColumns(String.format("%s %s, %s %s, %s %s", partitionKey0, partitionKeyType0, partitionKey1,
partitionKeyType1, column1, columnType1), cql);
assertPrimaryKey(String.format("(%s, %s)", partitionKey0, partitionKey1), cql);
assertNullOption(TableOption.COMPACT_STORAGE.getName(), cql);
assertDoubleOption(TableOption.READ_REPAIR_CHANCE.getName(), readRepairChance, cql);
assertDoubleOption(TableOption.DCLOCAL_READ_REPAIR_CHANCE.getName(), dcLocalReadRepairChance, cql);
assertDoubleOption(TableOption.BLOOM_FILTER_FP_CHANCE.getName(), bloomFilterFpChance, cql);
assertStringOption(TableOption.COMMENT.getName(), comment, cql);
assertLongOption(TableOption.GC_GRACE_SECONDS.getName(), gcGraceSeconds, cql);
}
@Test // DATACASS-518
public void createTableWithOrderedClustering() {
@@ -61,10 +173,9 @@ public class CreateTableCqlGeneratorUnitTests {
.clusteredKeyColumn("date_of_birth", DataType.date(), Ordering.ASCENDING) //
.column("name", DataType.ascii());
assertThat(CreateTableCqlGenerator.toCql(table))
.isEqualTo("CREATE TABLE person (id ascii, date_of_birth date, name ascii, " //
+ "PRIMARY KEY (id, date_of_birth)) " //
+ "WITH CLUSTERING ORDER BY (date_of_birth ASC);");
assertThat(toCql(table)).isEqualTo("CREATE TABLE person (id ascii, date_of_birth date, name ascii, " //
+ "PRIMARY KEY (id, date_of_birth)) " //
+ "WITH CLUSTERING ORDER BY (date_of_birth ASC);");
}
@Test // DATACASS-518
@@ -75,16 +186,15 @@ public class CreateTableCqlGeneratorUnitTests {
.clusteredKeyColumn("date_of_birth", DataType.date(), Ordering.ASCENDING) //
.column("name", DataType.ascii()).with(TableOption.COMPACT_STORAGE);
assertThat(CreateTableCqlGenerator.toCql(table))
.isEqualTo("CREATE TABLE person (id ascii, date_of_birth date, name ascii, " //
+ "PRIMARY KEY (id, date_of_birth)) " //
+ "WITH CLUSTERING ORDER BY (date_of_birth ASC) AND COMPACT STORAGE;");
assertThat(toCql(table)).isEqualTo("CREATE TABLE person (id ascii, date_of_birth date, name ascii, " //
+ "PRIMARY KEY (id, date_of_birth)) " //
+ "WITH CLUSTERING ORDER BY (date_of_birth ASC) AND COMPACT STORAGE;");
}
/**
* Asserts that the preamble is first & correctly formatted in the given CQL string.
*/
public static void assertPreamble(CqlIdentifier tableName, String cql) {
private static void assertPreamble(CqlIdentifier tableName, String cql) {
assertThat(cql.startsWith("CREATE TABLE " + tableName + " ")).isTrue();
}
@@ -93,7 +203,7 @@ public class CreateTableCqlGeneratorUnitTests {
*
* @param primaryKeyString IE, "foo", "foo, bar, baz", "(foo, bar), baz", etc
*/
public static void assertPrimaryKey(String primaryKeyString, String cql) {
private static void assertPrimaryKey(String primaryKeyString, String cql) {
assertThat(cql.contains(", PRIMARY KEY (" + primaryKeyString + "))")).isTrue();
}
@@ -102,250 +212,32 @@ public class CreateTableCqlGeneratorUnitTests {
*
* @param columnSpec IE, "foo text, bar blob"
*/
public static void assertColumns(String columnSpec, String cql) {
private static void assertColumns(String columnSpec, String cql) {
assertThat(cql.contains("(" + columnSpec + ",")).isTrue();
}
/**
* Asserts that the read repair change is set properly
*/
public static void assertStringOption(String name, String value, String cql) {
log.info(name + " -> " + value);
private static void assertStringOption(String name, String value, String cql) {
assertThat(cql.contains(name + " = '" + value + "'")).isTrue();
}
/**
* Asserts that the option is set
*/
public static void assertDoubleOption(String name, Double value, String cql) {
log.info(name + " -> " + value);
private static void assertDoubleOption(String name, Double value, String cql) {
assertThat(cql.contains(name + " = " + value)).isTrue();
}
public static void assertLongOption(String name, Long value, String cql) {
log.info(name + " -> " + value);
private static void assertLongOption(String name, Long value, String cql) {
assertThat(cql.contains(name + " = " + value)).isTrue();
}
/**
* Asserts that the read repair change is set properly
*/
public static void assertNullOption(String name, String cql) {
log.info(name);
private static void assertNullOption(String name, String cql) {
assertThat(cql.contains(" " + name + " ")).isTrue();
}
/**
* Convenient base class that other test classes can use so as not to repeat the generics declarations or
* {@link #generator()} method.
*/
public static abstract class CreateTableTest
extends AbstractTableOperationCqlGeneratorTest<CreateTableSpecification, CreateTableCqlGenerator> {
@Override
public CreateTableCqlGenerator generator() {
return new CreateTableCqlGenerator(specification);
}
}
public static class BasicTest extends CreateTableTest {
public CqlIdentifier name = of("mytable");
public DataType partitionKeyType0 = DataType.text();
public CqlIdentifier partitionKey0 = of("partitionKey0");
public DataType columnType1 = DataType.text();
public String column1 = "column1";
@Override
public CreateTableSpecification specification() {
return CreateTableSpecification.createTable(name).partitionKeyColumn(partitionKey0, partitionKeyType0)
.column(column1, columnType1);
}
@Test
public void test() {
prepare();
assertPreamble(name, cql);
assertColumns(String.format("%s %s, %s %s", partitionKey0, partitionKeyType0, column1, columnType1), cql);
assertPrimaryKey(partitionKey0.toCql(), cql);
}
}
public static class CompositePartitionKeyTest extends CreateTableTest {
public CqlIdentifier name = of("composite_partition_key_table");
public DataType partKeyType0 = DataType.text();
public CqlIdentifier partKey0 = of("partKey0");
public DataType partKeyType1 = DataType.text();
public CqlIdentifier partKey1 = of("partKey1");
public CqlIdentifier column0 = of("column0");
public DataType columnType0 = DataType.text();
@Override
public CreateTableSpecification specification() {
return CreateTableSpecification.createTable(name).partitionKeyColumn(partKey0, partKeyType0)
.partitionKeyColumn(partKey1, partKeyType1).column(column0, columnType0);
}
@Test
public void test() {
prepare();
assertPreamble(name, cql);
assertColumns(
String.format("%s %s, %s %s, %s %s", partKey0, partKeyType0, partKey1, partKeyType1, column0, columnType0),
cql);
assertPrimaryKey(String.format("(%s, %s)", partKey0, partKey1), cql);
}
}
/**
* Test just the Read Repair Chance
*
* @author David Webb
*/
public static class ReadRepairChanceTest extends CreateTableTest {
public CqlIdentifier name = of("mytable");
public DataType partitionKeyType0 = DataType.text();
public CqlIdentifier partitionKey0 = of("partitionKey0");
public DataType partitionKeyType1 = DataType.timestamp();
public CqlIdentifier partitionKey1 = of("create_timestamp");
public DataType columnType1 = DataType.text();
public CqlIdentifier column1 = of("column1");
public Double readRepairChance = 0.5;
@Override
public CreateTableSpecification specification() {
return CreateTableSpecification.createTable(name).partitionKeyColumn(partitionKey0, partitionKeyType0)
.partitionKeyColumn(partitionKey1, partitionKeyType1).column(column1, columnType1)
.with(TableOption.READ_REPAIR_CHANCE, readRepairChance);
}
@Test
public void test() {
prepare();
assertPreamble(name, cql);
assertColumns(String.format("%s %s, %s %s, %s %s", partitionKey0, partitionKeyType0, partitionKey1,
partitionKeyType1, column1, columnType1), cql);
assertPrimaryKey(String.format("(%s, %s)", partitionKey0, partitionKey1), cql);
assertDoubleOption(TableOption.READ_REPAIR_CHANCE.getName(), readRepairChance, cql);
}
}
/**
* Fully test all available create table options
*
* @author David Webb
*/
public static class MultipleOptionsTest extends CreateTableTest {
public CqlIdentifier name = of("timeseries_table");
public DataType partitionKeyType0 = DataType.timeuuid();
public CqlIdentifier partitionKey0 = of("tid");
public DataType partitionKeyType1 = DataType.timestamp();
public CqlIdentifier partitionKey1 = of("create_timestamp");
public DataType columnType1 = DataType.text();
public CqlIdentifier column1 = of("data_point");
public Double readRepairChance = 0.5;
public Double dcLocalReadRepairChance = 0.7;
public Double bloomFilterFpChance = 0.001;
public Boolean replcateOnWrite = Boolean.FALSE;
public Long gcGraceSeconds = 600l;
public String comment = "This is My Table";
public Map<Option, Object> compactionMap = new LinkedHashMap<>();
public Map<Option, Object> compressionMap = new LinkedHashMap<>();
public Map<Option, Object> cachingMap = new LinkedHashMap<>();
@Override
public CreateTableSpecification specification() {
// Compaction
compactionMap.put(CompactionOption.CLASS, "SizeTieredCompactionStrategy");
compactionMap.put(CompactionOption.MIN_THRESHOLD, "4");
// Compression
compressionMap.put(CompressionOption.SSTABLE_COMPRESSION, "SnappyCompressor");
compressionMap.put(CompressionOption.CHUNK_LENGTH_KB, 128);
compressionMap.put(CompressionOption.CRC_CHECK_CHANCE, 0.75);
// Caching
cachingMap.put(CachingOption.KEYS, KeyCachingOption.ALL);
cachingMap.put(CachingOption.ROWS_PER_PARTITION, "NONE");
return CreateTableSpecification.createTable(name).partitionKeyColumn(partitionKey0, partitionKeyType0)
.partitionKeyColumn(partitionKey1, partitionKeyType1).column(column1, columnType1)
.with(TableOption.COMPACT_STORAGE).with(TableOption.READ_REPAIR_CHANCE, readRepairChance)
.with(TableOption.COMPACTION, compactionMap).with(TableOption.COMPRESSION, compressionMap)
.with(TableOption.BLOOM_FILTER_FP_CHANCE, bloomFilterFpChance).with(TableOption.CACHING, cachingMap)
.with(TableOption.COMMENT, comment).with(TableOption.DCLOCAL_READ_REPAIR_CHANCE, dcLocalReadRepairChance)
.with(TableOption.GC_GRACE_SECONDS, gcGraceSeconds);
}
@Test
public void test() {
prepare();
log.info(cql);
assertPreamble(name, cql);
assertColumns(String.format("%s %s, %s %s, %s %s", partitionKey0, partitionKeyType0, partitionKey1,
partitionKeyType1, column1, columnType1), cql);
assertPrimaryKey(String.format("(%s, %s)", partitionKey0, partitionKey1), cql);
assertNullOption(TableOption.COMPACT_STORAGE.getName(), cql);
assertDoubleOption(TableOption.READ_REPAIR_CHANCE.getName(), readRepairChance, cql);
assertDoubleOption(TableOption.DCLOCAL_READ_REPAIR_CHANCE.getName(), dcLocalReadRepairChance, cql);
assertDoubleOption(TableOption.BLOOM_FILTER_FP_CHANCE.getName(), bloomFilterFpChance, cql);
assertStringOption(TableOption.COMMENT.getName(), comment, cql);
assertLongOption(TableOption.GC_GRACE_SECONDS.getName(), gcGraceSeconds, cql);
}
}
public static class FunkyTableNameTest {
public static final List<String> FUNKY_LEGAL_NAMES;
static {
List<String> funkies = new ArrayList<>(Arrays.asList(new String[] { /* TODO */ }));
// TODO: should these work? "a \"\" x", "a\"\"\"\"x", "a b"
FUNKY_LEGAL_NAMES = Collections.unmodifiableList(Arrays.stream(ReservedKeyword.values()) //
.map(Enum::name) //
.collect(Collectors.toList()));
}
@Test
public void test() {
for (String name : FUNKY_LEGAL_NAMES) {
new TableNameTest(name).test();
}
}
}
/**
* This class is supposed to be used by other test classes.
*/
public static class TableNameTest extends CreateTableTest {
public String tableName;
public TableNameTest(String tableName) {
this.tableName = tableName;
}
@Override
public CreateTableSpecification specification() {
return CreateTableSpecification.createTable(tableName).partitionKeyColumn(of("pk"), DataType.text());
}
/**
* There is no @Test annotation on this method on purpose! It's supposed to be called by another test class's @Test
* method so that you can loop, calling this test method as many times as are necessary.
*/
public void test() {
prepare();
assertPreamble(of(tableName), cql);
}
}
}

View File

@@ -1,76 +0,0 @@
/*
* Copyright 2017-2018 the original author or authors.
*
* Licensed 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.springframework.data.cassandra.core.cql.generator;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.cassandra.core.cql.keyspace.DropTableSpecification;
import org.springframework.data.cassandra.test.util.AbstractKeyspaceCreatingIntegrationTest;
/**
* Test CREATE TABLE / ALTER TABLE / DROP TABLE
*
* @author David Webb
* @author Oliver Gierke
* @author Mark Paluch
*/
public class TableLifecycleIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest {
private static final Logger log = LoggerFactory.getLogger(TableLifecycleIntegrationTests.class);
CreateTableCqlGeneratorUnitTests.MultipleOptionsTest createTableTest = new CreateTableCqlGeneratorUnitTests.MultipleOptionsTest();
@Before
public void setUp() throws Exception {
execute("cassandraOperationsTest-cql-dataload.cql", this.keyspace);
}
@Test
public void dropIsSuccessful() {
createTableTest.prepare();
log.info(createTableTest.cql);
session.execute(createTableTest.cql);
CqlTableSpecificationAssertions.assertTable(createTableTest.specification, keyspace, session);
DropTableTest dropTest = new DropTableTest();
dropTest.prepare();
log.info(dropTest.cql);
session.execute(dropTest.cql);
CqlTableSpecificationAssertions.assertNoTable(dropTest.specification, keyspace, session);
}
public class DropTableTest extends DropTableCqlGeneratorUnitTests.DropTableTest {
@Override
public DropTableSpecification specification() {
return DropTableSpecification.dropTable(createTableTest.specification.getName());
}
@Override
public DropTableCqlGenerator generator() {
return new DropTableCqlGenerator(specification);
}
}
}

View File

@@ -1,46 +0,0 @@
/*
* Copyright 2017-2018 the original author or authors.
*
* Licensed 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.springframework.data.cassandra.core.cql.generator;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.cassandra.test.util.AbstractKeyspaceCreatingIntegrationTest;
/**
* Test CREATE TABLE for all Options and assert against C* TableMetaData
*
* @author David Webb
* @author Oliver Gierke
*/
public class TableOptionsIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest {
private static final Logger log = LoggerFactory.getLogger(TableOptionsIntegrationTests.class);
@Test
public void test() {
CreateTableCqlGeneratorUnitTests.MultipleOptionsTest optionsTest = new CreateTableCqlGeneratorUnitTests.MultipleOptionsTest();
optionsTest.prepare();
log.info(optionsTest.cql);
session.execute(optionsTest.cql);
CqlTableSpecificationAssertions.assertTable(optionsTest.specification, keyspace, session);
}
}