23
23
import org .elasticsearch .common .xcontent .XContentParseException ;
24
24
import org .elasticsearch .common .xcontent .XContentParser ;
25
25
import org .elasticsearch .common .xcontent .json .JsonXContent ;
26
+ import org .elasticsearch .search .aggregations .bucket .GeoHashGridTests ;
26
27
import org .elasticsearch .test .ESTestCase ;
27
28
28
29
import static org .hamcrest .Matchers .containsString ;
32
33
33
34
public class GeoHashGridParserTests extends ESTestCase {
34
35
public void testParseValidFromInts () throws Exception {
35
- int precision = randomIntBetween (1 , 12 );
36
+ GeoHashType type = GeoHashGridTests .randomType ();
37
+ int precision = GeoHashGridTests .randomPrecision (type );
36
38
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}" );
38
41
XContentParser .Token token = stParser .nextToken ();
39
42
assertSame (XContentParser .Token .START_OBJECT , token );
40
43
// can create a factory
41
44
assertNotNull (GeoGridAggregationBuilder .parse ("geohash_grid" , stParser ));
42
45
}
43
46
44
47
public void testParseValidFromStrings () throws Exception {
45
- int precision = randomIntBetween (1 , 12 );
48
+ GeoHashType type = GeoHashGridTests .randomType ();
49
+ int precision = GeoHashGridTests .randomPrecision (type );
46
50
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\" }" );
48
53
XContentParser .Token token = stParser .nextToken ();
49
54
assertSame (XContentParser .Token .START_OBJECT , token );
50
55
// can create a factory
@@ -94,7 +99,9 @@ public void testParseDistanceUnitPrecisionTooSmall() throws Exception {
94
99
}
95
100
96
101
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}" );
98
105
XContentParser .Token token = stParser .nextToken ();
99
106
assertSame (XContentParser .Token .START_OBJECT , token );
100
107
XContentParseException e = expectThrows (XContentParseException .class ,
@@ -104,15 +111,28 @@ public void testParseErrorOnBooleanPrecision() throws Exception {
104
111
}
105
112
106
113
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 +"\" }" );
108
118
XContentParser .Token token = stParser .nextToken ();
109
119
assertSame (XContentParser .Token .START_OBJECT , token );
110
120
try {
111
121
GeoGridAggregationBuilder .parse ("geohash_grid" , stParser );
112
122
fail ();
113
123
} catch (XContentParseException ex ) {
114
124
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
+ }
116
136
}
117
137
}
118
138
}
0 commit comments