|
22 | 22 | import org.elasticsearch.xpack.esql.expression.function.FunctionName;
|
23 | 23 | import org.elasticsearch.xpack.esql.expression.function.MultiRowTestCaseSupplier;
|
24 | 24 | import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
|
| 25 | +import org.hamcrest.BaseMatcher; |
| 26 | +import org.hamcrest.Description; |
| 27 | +import org.hamcrest.Matcher; |
25 | 28 |
|
26 |
| -import java.nio.ByteOrder; |
27 | 29 | import java.util.List;
|
28 | 30 | import java.util.function.Supplier;
|
29 | 31 | import java.util.stream.Stream;
|
30 | 32 |
|
31 |
| -import static org.hamcrest.Matchers.equalTo; |
| 33 | +import static org.hamcrest.Matchers.closeTo; |
32 | 34 |
|
33 | 35 | @FunctionName("st_centroid_agg")
|
34 | 36 | public class SpatialCentroidTests extends AbstractAggregationTestCase {
|
@@ -74,16 +76,58 @@ private static TestCaseSupplier makeSupplier(TestCaseSupplier.TypedDataSupplier
|
74 | 76 | count++;
|
75 | 77 | }
|
76 | 78 |
|
77 |
| - var expected = new BytesRef( |
78 |
| - WellKnownBinary.toWKB(new Point(xSum.value() / count, ySum.value() / count), ByteOrder.LITTLE_ENDIAN) |
79 |
| - ); |
| 79 | + var expectedX = xSum.value() / count; |
| 80 | + var expectedY = ySum.value() / count; |
80 | 81 |
|
81 | 82 | return new TestCaseSupplier.TestCase(
|
82 | 83 | List.of(fieldTypedData),
|
83 | 84 | "SpatialCentroid[field=Attribute[channel=0]]",
|
84 | 85 | fieldTypedData.type(),
|
85 |
| - equalTo(expected) |
| 86 | + centroidMatches(expectedX, expectedY, 1e-14) |
86 | 87 | );
|
87 | 88 | });
|
88 | 89 | }
|
| 90 | + |
| 91 | + @SuppressWarnings("SameParameterValue") |
| 92 | + private static Matcher<BytesRef> centroidMatches(double x, double y, double error) { |
| 93 | + return new TestCentroidMatcher(x, y, error); |
| 94 | + } |
| 95 | + |
| 96 | + private static class TestCentroidMatcher extends BaseMatcher<BytesRef> { |
| 97 | + private final double x; |
| 98 | + private final double y; |
| 99 | + private final Matcher<Double> mx; |
| 100 | + private final Matcher<Double> my; |
| 101 | + |
| 102 | + private TestCentroidMatcher(double x, double y, double error) { |
| 103 | + this.x = x; |
| 104 | + this.y = y; |
| 105 | + this.mx = closeTo(x, error); |
| 106 | + this.my = closeTo(y, error); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean matches(Object item) { |
| 111 | + if (item instanceof BytesRef wkb) { |
| 112 | + var point = (Point) WellKnownBinary.fromWKB(GeometryValidator.NOOP, false, wkb.bytes, wkb.offset, wkb.length); |
| 113 | + return mx.matches(point.getX()) && my.matches(point.getY()); |
| 114 | + } |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public void describeMismatch(Object item, Description description) { |
| 120 | + if (item instanceof BytesRef wkb) { |
| 121 | + var point = (Point) WellKnownBinary.fromWKB(GeometryValidator.NOOP, false, wkb.bytes, wkb.offset, wkb.length); |
| 122 | + description.appendText("was ").appendValue(point); |
| 123 | + } else { |
| 124 | + description.appendText("was ").appendValue(item); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void describeTo(Description description) { |
| 130 | + description.appendValue(" POINT (" + x + " " + y + ")"); |
| 131 | + } |
| 132 | + } |
89 | 133 | }
|
0 commit comments