Add test for GeoHashUtils#bbox()

This commit is contained in:
Christoph Büscher 2016-06-24 17:26:39 +02:00 committed by Nicholas Knize
parent f77f79c24a
commit 0d428b6ba8
3 changed files with 28 additions and 14 deletions

View File

@ -18,6 +18,7 @@
*/
package org.elasticsearch.common.geo;
import org.apache.lucene.geo.Rectangle;
import org.elasticsearch.test.ESTestCase;
/**
@ -57,4 +58,16 @@ public class GeoHashTests extends ESTestCase {
}
}
}
public void testBboxFromHash() {
String hash = randomGeohash(1, 12);
int level = hash.length();
Rectangle bbox = GeoHashUtils.bbox(hash);
// check that the length is as expected
double expectedLonDiff = 360.0 / (Math.pow(8.0, (level + 1) / 2) * Math.pow(4.0, level / 2));
double expectedLatDiff = 180.0 / (Math.pow(4.0, (level + 1) / 2) * Math.pow(8.0, level / 2));
assertEquals(expectedLonDiff, bbox.maxLon - bbox.minLon, 0.00001);
assertEquals(expectedLatDiff, bbox.maxLat - bbox.minLat, 0.00001);
assertEquals(hash, GeoHashUtils.stringEncode(bbox.minLon, bbox.minLat, level));
}
}

View File

@ -19,7 +19,6 @@
package org.elasticsearch.test;
import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
@ -128,7 +127,6 @@ import static org.hamcrest.Matchers.not;
public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>> extends ESTestCase {
private static final GeohashGenerator geohashGenerator = new GeohashGenerator();
public static final String STRING_FIELD_NAME = "mapped_string";
protected static final String STRING_FIELD_NAME_2 = "mapped_string_2";
protected static final String INT_FIELD_NAME = "mapped_int";
@ -685,18 +683,6 @@ public abstract class AbstractQueryTestCase<QB extends AbstractQueryBuilder<QB>>
return (serviceHolder.currentTypes.length == 0) ? MetaData.ALL : randomFrom(serviceHolder.currentTypes);
}
public static String randomGeohash(int minPrecision, int maxPrecision) {
return geohashGenerator.ofStringLength(random(), minPrecision, maxPrecision);
}
public static class GeohashGenerator extends CodepointSetGenerator {
private static final char[] ASCII_SET = "0123456789bcdefghjkmnpqrstuvwxyz".toCharArray();
public GeohashGenerator() {
super(ASCII_SET);
}
}
protected static Fuzziness randomFuzziness(String fieldName) {
switch (fieldName) {
case INT_FIELD_NAME:

View File

@ -24,6 +24,7 @@ import com.carrotsearch.randomizedtesting.annotations.ThreadLeakLingering;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope.Scope;
import com.carrotsearch.randomizedtesting.annotations.TimeoutSuite;
import com.carrotsearch.randomizedtesting.generators.CodepointSetGenerator;
import com.carrotsearch.randomizedtesting.generators.RandomInts;
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import com.carrotsearch.randomizedtesting.generators.RandomStrings;
@ -663,6 +664,20 @@ public abstract class ESTestCase extends LuceneTestCase {
return things;
}
public static String randomGeohash(int minPrecision, int maxPrecision) {
return geohashGenerator.ofStringLength(random(), minPrecision, maxPrecision);
}
private static final GeohashGenerator geohashGenerator = new GeohashGenerator();
public static class GeohashGenerator extends CodepointSetGenerator {
private final static char[] ASCII_SET = "0123456789bcdefghjkmnpqrstuvwxyz".toCharArray();
public GeohashGenerator() {
super(ASCII_SET);
}
}
/**
* Randomly shuffles the fields inside objects in the {@link XContentBuilder} passed in.
* Recursively goes through inner objects and also shuffles them. Exceptions for this