Skip to content

Commit 79f79c7

Browse files
committed
generalize tests
1 parent 213487f commit 79f79c7

File tree

3 files changed

+69
-32
lines changed

3 files changed

+69
-32
lines changed

server/src/test/java/org/elasticsearch/search/aggregations/bucket/GeoHashGridTests.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,46 @@
2525

2626
public class GeoHashGridTests extends BaseAggregationTestCase<GeoGridAggregationBuilder> {
2727

28+
/**
29+
* Pick a random hash type
30+
*/
31+
public static GeoHashType randomType() {
32+
return randomFrom(GeoHashType.values());
33+
}
34+
35+
/**
36+
* Pick a random precision for the given hash type.
37+
*/
38+
public static int randomPrecision(final GeoHashType type) {
39+
int precision;
40+
switch (type) {
41+
case GEOHASH:
42+
precision = randomIntBetween(1, 12);
43+
break;
44+
default:
45+
throw new IllegalArgumentException("GeoHashType." + type.name() + " was not added to the test");
46+
}
47+
return precision;
48+
}
49+
50+
public static int maxPrecision(GeoHashType type) {
51+
switch (type) {
52+
case GEOHASH:
53+
return 12;
54+
default:
55+
throw new IllegalArgumentException("GeoHashType." + type.name() + " was not added to the test");
56+
}
57+
}
58+
2859
@Override
2960
protected GeoGridAggregationBuilder createTestAggregatorBuilder() {
3061
String name = randomAlphaOfLengthBetween(3, 20);
3162
GeoGridAggregationBuilder factory = new GeoGridAggregationBuilder(name);
3263
if (randomBoolean()) {
33-
factory.type(randomFrom(GeoHashType.values()).toString());
64+
factory.type(randomType().toString());
3465
}
3566
if (randomBoolean()) {
36-
int precision;
37-
switch (factory.type()) {
38-
case GEOHASH:
39-
precision = randomIntBetween(1, 12);
40-
break;
41-
default:
42-
throw new IllegalArgumentException(
43-
"GeoHashType." + factory.type().name() + " was not added to the test");
44-
}
45-
factory.precision(precision);
46-
} else {
47-
// precision gets initialized during the parse stage, so we have to force it
48-
// to the default value for the chosen hashing type.
49-
factory.precision(factory.type().getHandler().getDefaultPrecision());
67+
factory.precision(randomPrecision(factory.type()));
5068
}
5169
if (randomBoolean()) {
5270
factory.size(randomIntBetween(1, Integer.MAX_VALUE));

server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridAggregatorTests.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.elasticsearch.index.mapper.MappedFieldType;
3333
import org.elasticsearch.search.aggregations.Aggregator;
3434
import org.elasticsearch.search.aggregations.AggregatorTestCase;
35+
import org.elasticsearch.search.aggregations.bucket.GeoHashGridTests;
3536

3637
import java.io.IOException;
3738
import java.util.ArrayList;
@@ -48,24 +49,22 @@ public class GeoHashGridAggregatorTests extends AggregatorTestCase {
4849
private static final String FIELD_NAME = "location";
4950

5051
public void testNoDocs() throws IOException {
51-
testCase(new MatchAllDocsQuery(), FIELD_NAME, GeoHashType.GEOHASH, 1, iw -> {
52+
final GeoHashType type = GeoHashGridTests.randomType();
53+
testCase(new MatchAllDocsQuery(), FIELD_NAME, type, GeoHashGridTests.randomPrecision(type), iw -> {
5254
// Intentionally not writing any docs
53-
}, geoHashGrid -> {
54-
assertEquals(0, geoHashGrid.getBuckets().size());
55-
});
55+
}, geoHashGrid -> assertEquals(0, geoHashGrid.getBuckets().size()));
5656
}
5757

5858
public void testFieldMissing() throws IOException {
59-
testCase(new MatchAllDocsQuery(), "wrong_field", GeoHashType.GEOHASH, 1, iw -> {
59+
final GeoHashType type = GeoHashGridTests.randomType();
60+
testCase(new MatchAllDocsQuery(), "wrong_field", type, GeoHashGridTests.randomPrecision(type), iw -> {
6061
iw.addDocument(Collections.singleton(new LatLonDocValuesField(FIELD_NAME, 10D, 10D)));
61-
}, geoHashGrid -> {
62-
assertEquals(0, geoHashGrid.getBuckets().size());
63-
});
62+
}, geoHashGrid -> assertEquals(0, geoHashGrid.getBuckets().size()));
6463
}
6564

6665
public void testHashcodeWithSeveralDocs() throws IOException {
67-
int precision = randomIntBetween(1, 12);
68-
testWithSeveralDocs(GeoHashType.GEOHASH, precision);
66+
final GeoHashType type = GeoHashGridTests.randomType();
67+
testWithSeveralDocs(type, GeoHashGridTests.randomPrecision(type));
6968
}
7069

7170
private void testWithSeveralDocs(GeoHashType type, int precision)

server/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.common.xcontent.XContentParseException;
2424
import org.elasticsearch.common.xcontent.XContentParser;
2525
import org.elasticsearch.common.xcontent.json.JsonXContent;
26+
import org.elasticsearch.search.aggregations.bucket.GeoHashGridTests;
2627
import org.elasticsearch.test.ESTestCase;
2728

2829
import static org.hamcrest.Matchers.containsString;
@@ -32,19 +33,23 @@
3233

3334
public class GeoHashGridParserTests extends ESTestCase {
3435
public void testParseValidFromInts() throws Exception {
35-
int precision = randomIntBetween(1, 12);
36+
GeoHashType type = GeoHashGridTests.randomType();
37+
int precision = GeoHashGridTests.randomPrecision(type);
3638
XContentParser stParser = createParser(JsonXContent.jsonXContent,
37-
"{\"field\":\"my_loc\", \"precision\":" + precision + ", \"size\": 500, \"shard_size\": 550}");
39+
"{\"field\":\"my_loc\", \"type\":\"" + type + "\", \"precision\":" + precision +
40+
", \"size\": 500, \"shard_size\": 550}");
3841
XContentParser.Token token = stParser.nextToken();
3942
assertSame(XContentParser.Token.START_OBJECT, token);
4043
// can create a factory
4144
assertNotNull(GeoGridAggregationBuilder.parse("geohash_grid", stParser));
4245
}
4346

4447
public void testParseValidFromStrings() throws Exception {
45-
int precision = randomIntBetween(1, 12);
48+
GeoHashType type = GeoHashGridTests.randomType();
49+
int precision = GeoHashGridTests.randomPrecision(type);
4650
XContentParser stParser = createParser(JsonXContent.jsonXContent,
47-
"{\"field\":\"my_loc\", \"precision\":\"" + precision + "\", \"size\": \"500\", \"shard_size\": \"550\"}");
51+
"{\"field\":\"my_loc\", \"type\":\"" + type + "\", \"precision\":\"" + precision +
52+
"\", \"size\": \"500\", \"shard_size\": \"550\"}");
4853
XContentParser.Token token = stParser.nextToken();
4954
assertSame(XContentParser.Token.START_OBJECT, token);
5055
// can create a factory
@@ -94,7 +99,9 @@ public void testParseDistanceUnitPrecisionTooSmall() throws Exception {
9499
}
95100

96101
public void testParseErrorOnBooleanPrecision() throws Exception {
97-
XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":false}");
102+
GeoHashType type = GeoHashGridTests.randomType();
103+
XContentParser stParser = createParser(JsonXContent.jsonXContent,
104+
"{\"field\":\"my_loc\", \"type\":\"" + type + "\", \"precision\":false}");
98105
XContentParser.Token token = stParser.nextToken();
99106
assertSame(XContentParser.Token.START_OBJECT, token);
100107
XContentParseException e = expectThrows(XContentParseException.class,
@@ -104,15 +111,28 @@ public void testParseErrorOnBooleanPrecision() throws Exception {
104111
}
105112

106113
public void testParseErrorOnPrecisionOutOfRange() throws Exception {
107-
XContentParser stParser = createParser(JsonXContent.jsonXContent, "{\"field\":\"my_loc\", \"precision\":\"13\"}");
114+
final GeoHashType type = GeoHashGridTests.randomType();
115+
final int precision = GeoHashGridTests.maxPrecision(type) + 1;
116+
XContentParser stParser = createParser(JsonXContent.jsonXContent,
117+
"{\"field\":\"my_loc\", \"type\":\"" + type + "\", \"precision\":\""+ precision +"\"}");
108118
XContentParser.Token token = stParser.nextToken();
109119
assertSame(XContentParser.Token.START_OBJECT, token);
110120
try {
111121
GeoGridAggregationBuilder.parse("geohash_grid", stParser);
112122
fail();
113123
} catch (XContentParseException ex) {
114124
assertThat(ex.getCause(), instanceOf(IllegalArgumentException.class));
115-
assertEquals("Invalid geohash aggregation precision of 13. Must be between 1 and 12.", ex.getCause().getMessage());
125+
if (type == GeoHashType.GEOHASH) {
126+
String expectedMsg;
127+
switch (type) {
128+
case GEOHASH:
129+
expectedMsg = "Invalid geohash aggregation precision of 13. Must be between 1 and 12.";
130+
break;
131+
default:
132+
throw new IllegalArgumentException("GeoHashType." + type.name() + " was not added to the test");
133+
}
134+
assertEquals(expectedMsg, ex.getCause().getMessage());
135+
}
116136
}
117137
}
118138
}

0 commit comments

Comments
 (0)