Get rid of duplicate random sort order generation

This commit is contained in:
Isabel Drost-Fromm 2016-04-28 15:14:15 +02:00
parent 8dc0610eaa
commit 0c7e58de93
5 changed files with 7 additions and 21 deletions

View File

@ -40,7 +40,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase<FieldSortBuilder
String fieldName = rarely() ? FieldSortBuilder.DOC_FIELD_NAME : randomAsciiOfLengthBetween(1, 10); String fieldName = rarely() ? FieldSortBuilder.DOC_FIELD_NAME : randomAsciiOfLengthBetween(1, 10);
FieldSortBuilder builder = new FieldSortBuilder(fieldName); FieldSortBuilder builder = new FieldSortBuilder(fieldName);
if (randomBoolean()) { if (randomBoolean()) {
builder.order(RandomSortDataGenerator.order(null)); builder.order(randomFrom(SortOrder.values()));
} }
if (randomBoolean()) { if (randomBoolean()) {
@ -95,7 +95,7 @@ public class FieldSortBuilderTests extends AbstractSortTestCase<FieldSortBuilder
mutated.missing(RandomSortDataGenerator.missing(mutated.missing())); mutated.missing(RandomSortDataGenerator.missing(mutated.missing()));
break; break;
case 5: case 5:
mutated.order(RandomSortDataGenerator.order(mutated.order())); mutated.order(ESTestCase.randomValueOtherThan(mutated.order(), () -> randomFrom(SortOrder.values())));
break; break;
default: default:
throw new IllegalStateException("Unsupported mutation."); throw new IllegalStateException("Unsupported mutation.");

View File

@ -83,7 +83,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
result.unit(unit(result.unit())); result.unit(unit(result.unit()));
} }
if (randomBoolean()) { if (randomBoolean()) {
result.order(RandomSortDataGenerator.order(null)); result.order(randomFrom(SortOrder.values()));
} }
if (randomBoolean()) { if (randomBoolean()) {
result.sortMode(mode(result.sortMode())); result.sortMode(mode(result.sortMode()));
@ -171,7 +171,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
result.unit(unit(original.unit())); result.unit(unit(original.unit()));
break; break;
case 4: case 4:
result.order(RandomSortDataGenerator.order(original.order())); result.order(ESTestCase.randomValueOtherThan(original.order(), () -> randomFrom(SortOrder.values())));
break; break;
case 5: case 5:
result.sortMode(mode(original.sortMode())); result.sortMode(mode(original.sortMode()));

View File

@ -84,19 +84,4 @@ public class RandomSortDataGenerator {
} }
return missing; return missing;
} }
/**
* return a random {@link SortOrder} settings, except the one provided by parameter if set
*/
public static SortOrder order(@Nullable SortOrder original) {
if (original == null) {
return ESTestCase.randomBoolean() ? SortOrder.ASC : SortOrder.DESC;
}
if (original.equals(SortOrder.ASC)) {
return SortOrder.DESC;
} else {
return SortOrder.ASC;
}
}
} }

View File

@ -26,6 +26,7 @@ import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.test.ESTestCase;
import org.junit.Rule; import org.junit.Rule;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
@ -45,7 +46,7 @@ public class ScoreSortBuilderTests extends AbstractSortTestCase<ScoreSortBuilder
@Override @Override
protected ScoreSortBuilder mutate(ScoreSortBuilder original) throws IOException { protected ScoreSortBuilder mutate(ScoreSortBuilder original) throws IOException {
ScoreSortBuilder result = new ScoreSortBuilder(); ScoreSortBuilder result = new ScoreSortBuilder();
result.order(RandomSortDataGenerator.order(original.order())); result.order(ESTestCase.randomValueOtherThan(original.order(), () -> randomFrom(SortOrder.values())));
return result; return result;
} }

View File

@ -49,7 +49,7 @@ public class ScriptSortBuilderTests extends AbstractSortTestCase<ScriptSortBuild
ScriptSortBuilder builder = new ScriptSortBuilder(new Script(randomAsciiOfLengthBetween(5, 10)), ScriptSortBuilder builder = new ScriptSortBuilder(new Script(randomAsciiOfLengthBetween(5, 10)),
type); type);
if (randomBoolean()) { if (randomBoolean()) {
builder.order(RandomSortDataGenerator.order(null)); builder.order(randomFrom(SortOrder.values()));
} }
if (randomBoolean()) { if (randomBoolean()) {
if (type == ScriptSortType.NUMBER) { if (type == ScriptSortType.NUMBER) {