Skip to content

Commit

Permalink
improve GeoShapeScriptFieldTypeTests
Browse files Browse the repository at this point in the history
  • Loading branch information
iverase committed Oct 15, 2023
1 parent b56c486 commit 161318b
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class GeoShapeScriptFieldTypeTests extends AbstractNonTextScriptFieldType
) {
@Override
public void execute() {
emitFromObject("POINT(0 0)");
emitFromObject("LINESTRING(0 0, 1 1)");
}
};

Expand Down Expand Up @@ -99,12 +99,12 @@ protected ScriptFactory dummyScript() {
@Override
public void testDocValues() throws IOException {
try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": {\"lat\": 45.0, \"lon\" : 45.0}}"))));
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": {\"lat\": 0.0, \"lon\" : 0.0}}"))));
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": \"LINESTRING(0.0 0.0, 1.0 1.0)\" }"))));
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": \"LINESTRING(45.0 45.0, 3.0 3.0)\" }"))));
List<Object> results = new ArrayList<>();
try (DirectoryReader reader = iw.getReader()) {
IndexSearcher searcher = newSearcher(reader);
GeoShapeScriptFieldType ft = build("fromLatLon", Map.of(), OnScriptError.FAIL);
GeoShapeScriptFieldType ft = build("fromWKT", Map.of(), OnScriptError.FAIL);
GeoShapeScriptFieldData ifd = ft.fielddataBuilder(mockFielddataContext()).build(null, null);
searcher.search(new MatchAllDocsQuery(), new Collector() {
@Override
Expand Down Expand Up @@ -142,28 +142,28 @@ public void testSort() throws IOException {
public void testFetch() throws IOException {
try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
iw.addDocument(List.of(new StoredField("_source", new BytesRef("""
{"foo": {"coordinates": [45.0, 45.0], "type" : "Point"}}"""))));
{"foo": {"coordinates": [[45.0, 45.0], [0.0, 0.0]], "type" : "LineString"}}"""))));
try (DirectoryReader reader = iw.getReader()) {
SearchExecutionContext searchContext = mockContext(true, simpleMappedFieldType());
Source source = searchContext.lookup().getSource(reader.leaves().get(0), 0);
ValueFetcher fetcher = simpleMappedFieldType().valueFetcher(searchContext, randomBoolean() ? null : "geojson");
fetcher.setNextReader(reader.leaves().get(0));
assertThat(
fetcher.fetchValues(source, 0, null),
equalTo(List.of(Map.of("type", "Point", "coordinates", List.of(45.0, 45.0))))
equalTo(List.of(Map.of("type", "LineString", "coordinates", List.of(List.of(45.0, 45.0), List.of(0.0, 0.0)))))
);
fetcher = simpleMappedFieldType().valueFetcher(searchContext, "wkt");
fetcher.setNextReader(reader.leaves().get(0));
assertThat(fetcher.fetchValues(source, 0, null), equalTo(List.of("POINT (45.0 45.0)")));
assertThat(fetcher.fetchValues(source, 0, null), equalTo(List.of("LINESTRING (45.0 45.0, 0.0 0.0)")));
}
}
}

@Override
public void testUsedInScript() throws IOException {
try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": {\"lat\": 45.0, \"lon\" : 45.0}}"))));
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": {\"lat\": 0.0, \"lon\" : 0.0}}"))));
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": \"LINESTRING(0.0 0.0, 1.0 1.0)\" }"))));
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": \"LINESTRING(45.0 45.0, 3.0 3.0)\" }"))));
try (DirectoryReader reader = iw.getReader()) {
IndexSearcher searcher = newSearcher(reader);
SearchExecutionContext searchContext = mockContext(true, simpleMappedFieldType());
Expand Down Expand Up @@ -196,8 +196,8 @@ public double execute(ExplanationHolder explanation) {
@Override
public void testExistsQuery() throws IOException {
try (Directory directory = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), directory)) {
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": {\"lat\": 45.0, \"lon\" : 45.0}}"))));
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": {\"lat\": 0.0, \"lon\" : 0.0}}"))));
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": \"LINESTRING(0.0 0.0, 1.0 1.0)\" }"))));
iw.addDocument(List.of(new StoredField("_source", new BytesRef("{\"foo\": \"LINESTRING(45.0 45.0, 3.0 3.0)\" }"))));
try (DirectoryReader reader = iw.getReader()) {
IndexSearcher searcher = newSearcher(reader);
assertThat(searcher.count(simpleMappedFieldType().existsQuery(mockContext())), equalTo(2));
Expand Down Expand Up @@ -254,7 +254,7 @@ protected Query randomTermsQuery(MappedFieldType ft, SearchExecutionContext ctx)

@Override
protected GeoShapeScriptFieldType simpleMappedFieldType() {
return build("fromLatLon", Map.of(), OnScriptError.FAIL);
return build("fromWKT", Map.of(), OnScriptError.FAIL);
}

@Override
Expand All @@ -277,7 +277,7 @@ protected GeoShapeScriptFieldType build(String code, Map<String, Object> params,

private static GeometryFieldScript.Factory factory(Script script) {
return switch (script.getIdOrCode()) {
case "fromLatLon" -> (fieldName, params, lookup, onScriptError) -> (ctx) -> new GeometryFieldScript(
case "fromWKT" -> (fieldName, params, lookup, onScriptError) -> (ctx) -> new GeometryFieldScript(
fieldName,
params,
lookup,
Expand Down

0 comments on commit 161318b

Please sign in to comment.