|
@@ -18,21 +18,43 @@ import static org.hamcrest.Matchers.is;
|
|
|
public class StringScriptFieldRangeQueryTests extends AbstractStringScriptFieldQueryTestCase<StringScriptFieldRangeQuery> {
|
|
|
@Override
|
|
|
protected StringScriptFieldRangeQuery createTestInstance() {
|
|
|
- String lower = randomAlphaOfLength(3);
|
|
|
- String upper = randomValueOtherThan(lower, () -> randomAlphaOfLength(3));
|
|
|
- if (lower.compareTo(upper) > 0) {
|
|
|
- String tmp = lower;
|
|
|
- lower = upper;
|
|
|
- upper = tmp;
|
|
|
- }
|
|
|
+ String lower = randomBoolean() ? null : randomAlphaOfLength(3);
|
|
|
+ String upper = randomBoolean() ? null : randomAlphaOfLength(3);
|
|
|
return new StringScriptFieldRangeQuery(
|
|
|
randomScript(),
|
|
|
leafFactory,
|
|
|
randomAlphaOfLength(5),
|
|
|
lower,
|
|
|
upper,
|
|
|
- randomBoolean(),
|
|
|
- randomBoolean()
|
|
|
+ lower == null || randomBoolean(),
|
|
|
+ upper == null || randomBoolean()
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testValidate() {
|
|
|
+ expectThrows(
|
|
|
+ IllegalArgumentException.class,
|
|
|
+ () -> new StringScriptFieldRangeQuery(
|
|
|
+ randomScript(),
|
|
|
+ leafFactory,
|
|
|
+ randomAlphaOfLength(5),
|
|
|
+ null,
|
|
|
+ randomAlphaOfLength(3),
|
|
|
+ false,
|
|
|
+ randomBoolean()
|
|
|
+ )
|
|
|
+ );
|
|
|
+ expectThrows(
|
|
|
+ IllegalArgumentException.class,
|
|
|
+ () -> new StringScriptFieldRangeQuery(
|
|
|
+ randomScript(),
|
|
|
+ leafFactory,
|
|
|
+ randomAlphaOfLength(5),
|
|
|
+ randomAlphaOfLength(3),
|
|
|
+ null,
|
|
|
+ randomBoolean(),
|
|
|
+ false
|
|
|
+ )
|
|
|
);
|
|
|
}
|
|
|
|
|
@@ -65,15 +87,27 @@ public class StringScriptFieldRangeQueryTests extends AbstractStringScriptFieldQ
|
|
|
fieldName += "modified";
|
|
|
break;
|
|
|
case 2:
|
|
|
- lower += "modified";
|
|
|
+ lower = mutate(lower);
|
|
|
+ if (lower == null) {
|
|
|
+ includeLower = true;
|
|
|
+ }
|
|
|
break;
|
|
|
case 3:
|
|
|
- upper += "modified";
|
|
|
+ upper = mutate(upper);
|
|
|
+ if (upper == null) {
|
|
|
+ includeUpper = true;
|
|
|
+ }
|
|
|
break;
|
|
|
case 4:
|
|
|
+ if (lower == null) {
|
|
|
+ lower = mutate(lower);
|
|
|
+ }
|
|
|
includeLower = !includeLower;
|
|
|
break;
|
|
|
case 5:
|
|
|
+ if (upper == null) {
|
|
|
+ upper = mutate(upper);
|
|
|
+ }
|
|
|
includeUpper = !includeUpper;
|
|
|
break;
|
|
|
default:
|
|
@@ -82,6 +116,16 @@ public class StringScriptFieldRangeQueryTests extends AbstractStringScriptFieldQ
|
|
|
return new StringScriptFieldRangeQuery(script, leafFactory, fieldName, lower, upper, includeLower, includeUpper);
|
|
|
}
|
|
|
|
|
|
+ private static String mutate(String term) {
|
|
|
+ if (term == null) {
|
|
|
+ return randomAlphaOfLength(3);
|
|
|
+ }
|
|
|
+ if (randomBoolean()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return term + "modified";
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void testMatches() {
|
|
|
StringScriptFieldRangeQuery query = new StringScriptFieldRangeQuery(randomScript(), leafFactory, "test", "a", "b", true, true);
|
|
@@ -95,26 +139,72 @@ public class StringScriptFieldRangeQueryTests extends AbstractStringScriptFieldQ
|
|
|
assertTrue(query.matches(List.of("ab")));
|
|
|
assertFalse(query.matches(List.of("b")));
|
|
|
assertFalse(query.matches(List.of("ba")));
|
|
|
+ query = new StringScriptFieldRangeQuery(randomScript(), leafFactory, "test", "b", "a", randomBoolean(), randomBoolean());
|
|
|
+ assertFalse(query.matches(List.of("a")));
|
|
|
+ assertFalse(query.matches(List.of("ab")));
|
|
|
+ assertFalse(query.matches(List.of("b")));
|
|
|
+ assertFalse(query.matches(List.of("ba")));
|
|
|
+ query = new StringScriptFieldRangeQuery(randomScript(), leafFactory, "test", null, "b", true, false);
|
|
|
+ assertTrue(query.matches(List.of("a")));
|
|
|
+ assertTrue(query.matches(List.of("ab")));
|
|
|
+ assertFalse(query.matches(List.of("b")));
|
|
|
+ assertFalse(query.matches(List.of("ba")));
|
|
|
+ query = new StringScriptFieldRangeQuery(randomScript(), leafFactory, "test", "a", null, false, true);
|
|
|
+ assertFalse(query.matches(List.of("a")));
|
|
|
+ assertTrue(query.matches(List.of("ab")));
|
|
|
+ assertTrue(query.matches(List.of("b")));
|
|
|
+ assertTrue(query.matches(List.of("ba")));
|
|
|
+ assertTrue(query.matches(List.of("z")));
|
|
|
+ query = new StringScriptFieldRangeQuery(randomScript(), leafFactory, "test", null, null, true, true);
|
|
|
+ assertTrue(query.matches(List.of("a")));
|
|
|
+ assertTrue(query.matches(List.of("ab")));
|
|
|
+ assertTrue(query.matches(List.of("b")));
|
|
|
+ assertTrue(query.matches(List.of("ba")));
|
|
|
+ assertTrue(query.matches(List.of("z")));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void assertToString(StringScriptFieldRangeQuery query) {
|
|
|
assertThat(query.toString(query.fieldName()), containsString(query.includeLower() ? "[" : "{"));
|
|
|
assertThat(query.toString(query.fieldName()), containsString(query.includeUpper() ? "]" : "}"));
|
|
|
- assertThat(query.toString(query.fieldName()), containsString(query.lowerValue() + " TO " + query.upperValue()));
|
|
|
+ String lowerValue = query.lowerValue() == null ? "*" : query.lowerValue();
|
|
|
+ String upperValue = query.upperValue() == null ? "*" : query.upperValue();
|
|
|
+ assertThat(query.toString(query.fieldName()), containsString(lowerValue + " TO " + upperValue));
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void testVisit() {
|
|
|
StringScriptFieldRangeQuery query = createTestInstance();
|
|
|
ByteRunAutomaton automaton = visitForSingleAutomata(query);
|
|
|
- BytesRef term = new BytesRef(query.lowerValue() + "a");
|
|
|
- assertThat(automaton.run(term.bytes, term.offset, term.length), is(true));
|
|
|
- term = new BytesRef(query.lowerValue());
|
|
|
- assertThat(automaton.run(term.bytes, term.offset, term.length), is(query.includeLower()));
|
|
|
- term = new BytesRef(query.upperValue() + "a");
|
|
|
- assertThat(automaton.run(term.bytes, term.offset, term.length), is(false));
|
|
|
- term = new BytesRef(query.upperValue());
|
|
|
- assertThat(automaton.run(term.bytes, term.offset, term.length), is(query.includeUpper()));
|
|
|
+ boolean validRange = true;
|
|
|
+ if (query.lowerValue() != null && query.upperValue() != null) {
|
|
|
+ validRange = query.lowerValue().compareTo(query.upperValue()) < 0;
|
|
|
+ }
|
|
|
+ String termString;
|
|
|
+ if (query.lowerValue() == null) {
|
|
|
+ if (query.upperValue() == null) {
|
|
|
+ termString = randomAlphaOfLength(1);
|
|
|
+ } else {
|
|
|
+ termString = randomValueOtherThanMany(value -> {
|
|
|
+ int cmp = value.compareTo(query.upperValue());
|
|
|
+ return query.includeUpper() ? cmp > 0 : cmp >= 0;
|
|
|
+ }, () -> randomAlphaOfLength(1));
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ termString = query.lowerValue() + "a";
|
|
|
+ }
|
|
|
+ BytesRef term = new BytesRef(termString);
|
|
|
+ assertThat(automaton.run(term.bytes, term.offset, term.length), is(validRange));
|
|
|
+
|
|
|
+ if (query.lowerValue() != null) {
|
|
|
+ term = new BytesRef(query.lowerValue());
|
|
|
+ assertThat(automaton.run(term.bytes, term.offset, term.length), is(query.includeLower() && validRange));
|
|
|
+ }
|
|
|
+ if (query.upperValue() != null) {
|
|
|
+ term = new BytesRef(query.upperValue() + "a");
|
|
|
+ assertThat(automaton.run(term.bytes, term.offset, term.length), is(false));
|
|
|
+ term = new BytesRef(query.upperValue());
|
|
|
+ assertThat(automaton.run(term.bytes, term.offset, term.length), is(query.includeUpper() && validRange));
|
|
|
+ }
|
|
|
}
|
|
|
}
|