Switch from separate sort_mode to more general randomValueOtherThan

... for sort tests only ...
This commit is contained in:
Isabel Drost-Fromm 2016-04-28 14:45:56 +02:00
parent de43bda3f3
commit 47fefdd273
4 changed files with 18 additions and 19 deletions

View File

@ -25,6 +25,7 @@ import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
@ -51,7 +52,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase<FieldSortBuilder
}
if (randomBoolean()) {
builder.sortMode(RandomSortDataGenerator.mode(builder.sortMode()));
builder.sortMode(ESTestCase.randomValueOtherThan(builder.sortMode(), () -> randomFrom(SortMode.values())));
}
if (randomBoolean()) {
@ -77,7 +78,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase<FieldSortBuilder
mutated.setNestedFilter(RandomSortDataGenerator.nestedFilter(mutated.getNestedFilter()));
break;
case 2:
mutated.sortMode(RandomSortDataGenerator.mode(mutated.sortMode()));
mutated.sortMode(ESTestCase.randomValueOtherThan(mutated.sortMode(), () -> randomFrom(SortMode.values())));
break;
case 3:
mutated.unmappedType(RandomSortDataGenerator.randomAscii(mutated.unmappedType()));

View File

@ -63,20 +63,6 @@ public class RandomSortDataGenerator {
return nestedPath;
}
public static SortMode mode(SortMode original) {
Set<SortMode> set = new HashSet<>();
set.add(original);
return mode(set);
}
public static SortMode mode(Set<SortMode> except) {
SortMode mode = ESTestCase.randomFrom(SortMode.values());
while (except.contains(mode)) {
mode = ESTestCase.randomFrom(SortMode.values());
}
return mode;
}
public static Object missing(Object original) {
Object missing = null;
Object otherMissing = original;

View File

@ -29,6 +29,7 @@ import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptService.ScriptType;
import org.elasticsearch.search.sort.ScriptSortBuilder.ScriptSortType;
import org.elasticsearch.test.ESTestCase;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
@ -52,13 +53,13 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
}
if (randomBoolean()) {
if (type == ScriptSortType.NUMBER) {
builder.sortMode(RandomSortDataGenerator.mode(builder.sortMode()));
builder.sortMode(ESTestCase.randomValueOtherThan(builder.sortMode(), () -> randomFrom(SortMode.values())));
} else {
Set<SortMode> exceptThis = new HashSet<>();
exceptThis.add(SortMode.SUM);
exceptThis.add(SortMode.AVG);
exceptThis.add(SortMode.MEDIAN);
builder.sortMode(RandomSortDataGenerator.mode(exceptThis));
builder.sortMode(ESTestCase.randomValueOtherThanMany(exceptThis, () -> randomFrom(SortMode.values())));
}
}
if (randomBoolean()) {
@ -101,7 +102,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
break;
case 1:
if (original.type() == ScriptSortType.NUMBER) {
result.sortMode(RandomSortDataGenerator.mode(original.sortMode()));
result.sortMode(ESTestCase.randomValueOtherThan(result.sortMode(), () -> randomFrom(SortMode.values())));
} else {
// script sort type String only allows MIN and MAX, so we only switch
if (original.sortMode() == SortMode.MIN) {

View File

@ -417,6 +417,17 @@ public abstract class ESTestCase extends LuceneTestCase {
return randomValue;
}
/**
* helper to get a random value in a certain range that's different from the input
*/
public static <T> T randomValueOtherThanMany(Collection<T> input, Supplier<T> randomSupplier) {
T randomValue = null;
do {
randomValue = randomSupplier.get();
} while (input.contains(randomValue));
return randomValue;
}
/**
* Runs the code block for 10 seconds waiting for no assertion to trip.
*/