Skip to content

Commit 97fdf85

Browse files
committed
Refactor ScriptDocValuesDatesTests to make sure all cases are tested.
1 parent b20164c commit 97fdf85

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

server/src/test/java/org/elasticsearch/index/fielddata/ScriptDocValuesDatesTests.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.elasticsearch.index.fielddata;
2121

2222
import org.elasticsearch.index.fielddata.ScriptDocValues.Dates;
23+
import org.elasticsearch.script.ScriptModule;
2324
import org.elasticsearch.test.ESTestCase;
2425
import org.joda.time.DateTime;
2526
import org.joda.time.DateTimeZone;
@@ -32,6 +33,8 @@
3233
import java.security.Permissions;
3334
import java.security.PrivilegedAction;
3435
import java.security.ProtectionDomain;
36+
import java.util.Arrays;
37+
import java.util.Collections;
3538
import java.util.HashSet;
3639
import java.util.Set;
3740
import java.util.function.Consumer;
@@ -43,7 +46,28 @@ public void test() throws IOException {
4346
long[][] values = new long[between(3, 10)][];
4447
ReadableDateTime[][] expectedDates = new ReadableDateTime[values.length][];
4548
for (int d = 0; d < values.length; d++) {
46-
values[d] = new long[randomBoolean() ? randomBoolean() ? 0 : 1 : between(2, 100)];
49+
switch (d) {
50+
case 0:
51+
// empty
52+
values[d] = new long[0];
53+
break;
54+
case 1:
55+
// single value
56+
values[d] = new long[1];
57+
break;
58+
case 2:
59+
// multivalued
60+
values[d] = new long[between(2, 100)];
61+
break;
62+
default:
63+
// random
64+
values[d] = new long[between(0, 5)];
65+
break;
66+
}
67+
}
68+
Collections.shuffle(Arrays.asList(values), random());
69+
70+
for (int d = 0; d < values.length; d++) {
4771
expectedDates[d] = new ReadableDateTime[values[d].length];
4872
for (int i = 0; i < values[d].length; i++) {
4973
expectedDates[d][i] = new DateTime(randomNonNegativeLong(), DateTimeZone.UTC);
@@ -58,16 +82,17 @@ public void test() throws IOException {
5882
createTempDir();
5983
});
6084

61-
for (int round = 0; round < 10; round++) {
62-
int d = between(0, values.length - 1);
85+
for (int d = 0; d < values.length; d++) {
6386
dates.setNextDocId(d);
6487
if (expectedDates[d].length > 0) {
6588
assertEquals(expectedDates[d][0] , dates.getValue());
6689
assertEquals(expectedDates[d][0] , dates.getDate());
67-
} else {
90+
} else if (ScriptModule.EXCEPTION_FOR_MISSING_VALUE) {
6891
Exception e = expectThrows(IllegalStateException.class, () -> dates.getValue());
6992
assertEquals("A document doesn't have a value for a field! " +
7093
"Use doc[<field>].size()==0 to check if a document is missing a field!", e.getMessage());
94+
} else {
95+
assertEquals(0, dates.getValue().getMillis()); // Epoch
7196
}
7297
assertEquals(values[d].length, dates.size());
7398
for (int i = 0; i < values[d].length; i++) {

0 commit comments

Comments
 (0)