Skip to content

Commit

Permalink
Additional test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Jain <varunudr@amazon.com>
  • Loading branch information
vibrantvarun committed Sep 21, 2023
1 parent 3b6d79a commit e9c81e1
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.opensearch.test.AbstractQueryTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.nullValue;
Expand Down Expand Up @@ -133,4 +135,12 @@ public void testMustRewrite() throws IOException {
IllegalStateException e = expectThrows(IllegalStateException.class, () -> queryBuilder.toQuery(context));
assertEquals("Rewrite first", e.getMessage());
}

public void testVisit() {
ConstantScoreQueryBuilder queryBuilder = new ConstantScoreQueryBuilder(new TermQueryBuilder("unmapped_field", "foo"));
List<QueryBuilder> visitorQueries = new ArrayList<>();
queryBuilder.visit(createTestVisitor(visitorQueries));

assertEquals(2, visitorQueries.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package org.opensearch.index.query;

import org.apache.lucene.search.BooleanClause;
import org.opensearch.test.AbstractBuilderTestCase;

import java.util.ArrayList;
Expand All @@ -19,8 +20,11 @@ public void testNoOpsVisitor() {
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

List<QueryBuilder> visitedQueries = new ArrayList<>();
boolQueryBuilder.visit(createTestVisitor(visitedQueries));
QueryBuilderVisitor qbv = createTestVisitor(visitedQueries);
boolQueryBuilder.visit(qbv);
QueryBuilderVisitor subQbv = qbv.getChildVisitor(BooleanClause.Occur.MUST_NOT);
assertEquals(0, visitedQueries.size());
assertEquals(qbv, subQbv);
}

protected static QueryBuilderVisitor createTestVisitor(List<QueryBuilder> visitedQueries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.opensearch.test.AbstractQueryTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
Expand Down Expand Up @@ -192,4 +194,10 @@ public void testFromJsonWithNonDefaultBoostInLittleQuery() {
equalTo("span_containing [little] as a nested span clause can't have non-default boost value [2.0]")
);
}

public void testVisit() {
List<QueryBuilder> visitorQueries = new ArrayList<>();
doCreateTestQueryBuilder().visit(createTestVisitor(visitorQueries));
assertEquals(3, visitorQueries.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.opensearch.test.AbstractQueryTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.opensearch.index.query.QueryBuilders.spanTermQuery;
import static org.hamcrest.CoreMatchers.equalTo;
Expand Down Expand Up @@ -128,4 +130,10 @@ public void testFromJsonWithNonDefaultBoostInMatchQuery() {
Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json));
assertThat(exception.getMessage(), equalTo("span_first [match] as a nested span clause can't have non-default boost value [2.0]"));
}

public void testVisit() {
List<QueryBuilder> visitorQueries = new ArrayList<>();
doCreateTestQueryBuilder().visit(createTestVisitor(visitorQueries));
assertEquals(2, visitorQueries.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
import org.opensearch.test.AbstractQueryTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.util.Collections.singleton;
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
Expand Down Expand Up @@ -306,4 +308,12 @@ public void testTopNMultiTermsRewriteInsideSpan() throws Exception {
}

}

public void testVisit() {
MultiTermQueryBuilder multiTermQueryBuilder = new PrefixQueryBuilderTests().createTestQueryBuilder();
SpanMultiTermQueryBuilder spanMultiTermQueryBuilder = new SpanMultiTermQueryBuilder(multiTermQueryBuilder);
List<QueryBuilder> visitorQueries = new ArrayList<>();
spanMultiTermQueryBuilder.visit(createTestVisitor(visitorQueries));
assertEquals(2, visitorQueries.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
import org.opensearch.test.AbstractQueryTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.either;
Expand Down Expand Up @@ -231,4 +233,12 @@ public void testFromJsonWithNonDefaultBoostInInnerQuery() {
Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json));
assertThat(exception.getMessage(), equalTo("span_near [clauses] as a nested span clause can't have non-default boost value [2.0]"));
}

public void testVisit() {
SpanTermQueryBuilder[] spanTermQueries = new SpanTermQueryBuilderTests().createSpanTermQueryBuilders(1);
SpanNearQueryBuilder spanNearQueryBuilder = new SpanNearQueryBuilder(spanTermQueries[0], 1);
List<QueryBuilder> visitorQueries = new ArrayList<>();
spanNearQueryBuilder.visit(createTestVisitor(visitorQueries));
assertEquals(2, visitorQueries.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.opensearch.test.AbstractQueryTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.opensearch.index.query.QueryBuilders.spanNearQuery;
import static org.opensearch.index.query.QueryBuilders.spanTermQuery;
Expand Down Expand Up @@ -316,4 +318,10 @@ public void testFromJsonWithNonDefaultBoostInExcludeQuery() {
Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json));
assertThat(exception.getMessage(), equalTo("span_not [exclude] as a nested span clause can't have non-default boost value [2.0]"));
}

public void testVisit() {
List<QueryBuilder> visitedQueries = new ArrayList<>();
doCreateTestQueryBuilder().visit(createTestVisitor(visitedQueries));
assertEquals(3, visitedQueries.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
import org.opensearch.test.AbstractQueryTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
Expand Down Expand Up @@ -137,4 +139,12 @@ public void testFromJsonWithNonDefaultBoostInInnerQuery() {
Exception exception = expectThrows(ParsingException.class, () -> parseQuery(json));
assertThat(exception.getMessage(), equalTo("span_or [clauses] as a nested span clause can't have non-default boost value [2.0]"));
}

public void testVisit() {
SpanTermQueryBuilder spanTermQueryBuilder = new SpanTermQueryBuilder("demo", "demo");
SpanOrQueryBuilder spanOrQueryBuilder = new SpanOrQueryBuilder(spanTermQueryBuilder);
List<QueryBuilder> visitedQueries = new ArrayList<>();
spanOrQueryBuilder.visit(createTestVisitor(visitedQueries));
assertEquals(2, visitedQueries.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.opensearch.test.AbstractQueryTestCase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.instanceOf;
Expand Down Expand Up @@ -189,4 +191,10 @@ public void testFromJsonWithNonDefaultBoostInLittleQuery() {
equalTo("span_within [little] as a nested span clause can't have non-default boost value [2.0]")
);
}

public void testVisit() {
List<QueryBuilder> visitedQueries = new ArrayList<>();
doCreateTestQueryBuilder().visit(createTestVisitor(visitedQueries));
assertEquals(3, visitedQueries.size());
}
}

0 comments on commit e9c81e1

Please sign in to comment.