Switch to new random value supplier for test.

This commit is contained in:
Isabel Drost-Fromm 2016-04-28 14:27:22 +02:00
parent c1fa9cd18e
commit 3ab006e422
2 changed files with 20 additions and 30 deletions

View File

@ -36,6 +36,7 @@ import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper;
import org.elasticsearch.index.query.GeoValidationMethod; import org.elasticsearch.index.query.GeoValidationMethod;
import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.indices.query.IndicesQueriesRegistry; import org.elasticsearch.indices.query.IndicesQueriesRegistry;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.geo.RandomGeoGenerator; import org.elasticsearch.test.geo.RandomGeoGenerator;
import java.io.IOException; import java.io.IOException;
@ -80,13 +81,13 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
result.geoDistance(geoDistance(result.geoDistance())); result.geoDistance(geoDistance(result.geoDistance()));
} }
if (randomBoolean()) { if (randomBoolean()) {
result.unit(unit(result.unit())); result.unit(ESTestCase.randomValueOtherThan(result.unit(), () -> randomFrom(DistanceUnit.values())));
} }
if (randomBoolean()) { if (randomBoolean()) {
result.order(RandomSortDataGenerator.order(null)); result.order(RandomSortDataGenerator.order(null));
} }
if (randomBoolean()) { if (randomBoolean()) {
result.sortMode(mode(result.sortMode())); result.sortMode(ESTestCase.randomValueOtherThan(SortMode.SUM, () -> randomFrom(SortMode.values())));
} }
if (randomBoolean()) { if (randomBoolean()) {
result.setNestedFilter(RandomSortDataGenerator.nestedFilter(result.getNestedFilter())); result.setNestedFilter(RandomSortDataGenerator.nestedFilter(result.getNestedFilter()));
@ -95,7 +96,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
result.setNestedPath(RandomSortDataGenerator.randomAscii(result.getNestedPath())); result.setNestedPath(RandomSortDataGenerator.randomAscii(result.getNestedPath()));
} }
if (randomBoolean()) { if (randomBoolean()) {
result.validation(randomFrom(GeoValidationMethod.values())); result.validation(ESTestCase.randomValueOtherThan(result.validation(), () -> randomFrom(GeoValidationMethod.values())));
} }
return result; return result;
@ -108,30 +109,6 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
return clone; return clone;
} }
private static SortMode mode(SortMode original) {
SortMode result;
do {
result = randomFrom(SortMode.values());
} while (result == SortMode.SUM || result == original);
return result;
}
private static GeoValidationMethod validation(GeoValidationMethod original) {
GeoValidationMethod result;
do {
result = randomFrom(GeoValidationMethod.values());
} while (result == original);
return result;
}
private static DistanceUnit unit(DistanceUnit original) {
int id = -1;
while (id == -1 || (original != null && original.ordinal() == id)) {
id = randomIntBetween(0, DistanceUnit.values().length - 1);
}
return DistanceUnit.values()[id];
}
private static GeoPoint[] points(GeoPoint[] original) { private static GeoPoint[] points(GeoPoint[] original) {
GeoPoint[] result = null; GeoPoint[] result = null;
while (result == null || Arrays.deepEquals(original, result)) { while (result == null || Arrays.deepEquals(original, result)) {
@ -170,13 +147,15 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
result.geoDistance(geoDistance(original.geoDistance())); result.geoDistance(geoDistance(original.geoDistance()));
break; break;
case 3: case 3:
result.unit(unit(original.unit())); result.unit(ESTestCase.randomValueOtherThan(result.unit(), () -> randomFrom(DistanceUnit.values())));
break; break;
case 4: case 4:
result.order(RandomSortDataGenerator.order(original.order())); result.order(RandomSortDataGenerator.order(original.order()));
break; break;
case 5: case 5:
result.sortMode(mode(original.sortMode())); result.sortMode(ESTestCase.randomValueOtherThanMany(
Arrays.asList(SortMode.SUM, result.sortMode()),
() -> randomFrom(SortMode.values())));
break; break;
case 6: case 6:
result.setNestedFilter(RandomSortDataGenerator.nestedFilter(original.getNestedFilter())); result.setNestedFilter(RandomSortDataGenerator.nestedFilter(original.getNestedFilter()));
@ -185,7 +164,7 @@ public class GeoDistanceSortBuilderTests extends AbstractSortTestCase<GeoDistanc
result.setNestedPath(RandomSortDataGenerator.randomAscii(original.getNestedPath())); result.setNestedPath(RandomSortDataGenerator.randomAscii(original.getNestedPath()));
break; break;
case 8: case 8:
result.validation(validation(original.validation())); result.validation(ESTestCase.randomValueOtherThan(result.validation(), () -> randomFrom(GeoValidationMethod.values())));
break; break;
} }
return result; return result;

View File

@ -417,6 +417,17 @@ public abstract class ESTestCase extends LuceneTestCase {
return randomValue; 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. * Runs the code block for 10 seconds waiting for no assertion to trip.
*/ */