Skip to content

Commit 790f2fb

Browse files
committed
Fixing doctests result matchers
1 parent 47a3e5c commit 790f2fb

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/aggregate/IncreaseTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.Objects;
2626
import java.util.function.Supplier;
2727

28-
import static org.hamcrest.Matchers.closeTo;
2928
import static org.hamcrest.Matchers.equalTo;
3029
import static org.hamcrest.Matchers.hasSize;
3130

@@ -127,17 +126,17 @@ private static TestCaseSupplier makeSupplier(TestCaseSupplier.TypedDataSupplier
127126
matcher = Matchers.nullValue();
128127
} else {
129128
double increase = 0.0;
130-
for (int i = 1; i < nonNullDataRows.size(); i++) {
131-
double previous = ((Number) nonNullDataRows.get(i - 1)).doubleValue();
132-
double current = ((Number) nonNullDataRows.get(i)).doubleValue();
129+
for (int i = nonNullDataRows.size() - 1; i > 0; i--) {
130+
double previous = ((Number) nonNullDataRows.get(i)).doubleValue();
131+
double current = ((Number) nonNullDataRows.get(i - 1)).doubleValue();
133132
if (current >= previous) {
134133
increase += current - previous;
135134
} else {
136135
// Counter reset
137136
increase += current;
138137
}
139138
}
140-
matcher = closeTo(increase, 0.1 * increase);
139+
matcher = Matchers.allOf(Matchers.greaterThanOrEqualTo(increase * 0.9), Matchers.lessThanOrEqualTo(increase * 3));
141140
}
142141

143142
return new TestCaseSupplier.TestCase(

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/aggregate/IrateTests.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,22 @@ private static TestCaseSupplier makeSupplier(TestCaseSupplier.TypedDataSupplier
122122
);
123123

124124
List<Object> nonNullDataRows = dataRows.stream().filter(Objects::nonNull).toList();
125-
Object maxValue = null;
126-
Object minValue = null;
127-
128-
for (int i = dataRows.size() - 1; i >= 0; i--) {
129-
if (dataRows.get(i) != null) {
130-
if (maxValue == null) {
131-
maxValue = dataRows.get(i);
132-
} else if (((Comparable) dataRows.get(i)).compareTo(maxValue) > 0) {
133-
maxValue = dataRows.get(i);
134-
}
135-
if (minValue == null) {
136-
minValue = dataRows.get(i);
137-
} else if (((Comparable) dataRows.get(i)).compareTo(minValue) < 0) {
138-
minValue = dataRows.get(i);
139-
}
140-
}
141-
}
142-
143-
final Matcher<?> matcher;
125+
Matcher<?> matcher;
144126
if (nonNullDataRows.size() < 2) {
145127
matcher = Matchers.nullValue();
146128
} else {
147-
var maxDiff = ((Number) maxValue).doubleValue() - ((Number) minValue).doubleValue();
148-
matcher = Matchers.allOf(Matchers.greaterThanOrEqualTo(-maxDiff), Matchers.lessThanOrEqualTo(maxDiff));
129+
var lastValue = ((Number) nonNullDataRows.getFirst()).doubleValue();
130+
var secondLastValue = ((Number) nonNullDataRows.get(1)).doubleValue();
131+
var increase = lastValue >= secondLastValue ? lastValue - secondLastValue : lastValue;
132+
var largestTimestamp = timestamps.get(0);
133+
var secondLargestTimestamp = timestamps.get(1);
134+
var smallestTimestamp = timestamps.getLast();
135+
matcher = Matchers.allOf(
136+
Matchers.greaterThanOrEqualTo(increase / (largestTimestamp - smallestTimestamp) * 1000 * 0.9),
137+
Matchers.lessThanOrEqualTo(
138+
increase / (largestTimestamp - secondLargestTimestamp) * (largestTimestamp - smallestTimestamp) * 1000
139+
)
140+
);
149141
}
150142

151143
return new TestCaseSupplier.TestCase(

0 commit comments

Comments
 (0)