[TEST] minor adjustments to boost assertions for queries that don't support it

This commit is contained in:
javanna 2015-09-08 19:40:31 +02:00 committed by Luca Cavanna
parent 18bec264f9
commit 50c43c9328
7 changed files with 28 additions and 28 deletions

View File

@ -47,6 +47,11 @@ public class MatchNoneQueryBuilder extends AbstractQueryBuilder<MatchNoneQueryBu
return Queries.newMatchNoDocsQuery();
}
@Override
protected void setFinalBoost(Query query) {
//no-op this query doesn't support boost
}
@Override
public QueryValidationException validate() {
// nothing to validate

View File

@ -74,6 +74,11 @@ public class QueryFilterBuilder extends AbstractQueryBuilder<QueryFilterBuilder>
return new ConstantScoreQuery(innerQuery);
}
@Override
protected void setFinalBoost(Query query) {
//no-op this query doesn't support boost
}
@Override
public QueryValidationException validate() {
return validateInnerQuery(queryBuilder, null);

View File

@ -106,10 +106,7 @@ public class WrapperQueryBuilder extends AbstractQueryBuilder<WrapperQueryBuilde
@Override
protected void setFinalBoost(Query query) {
if (boost != DEFAULT_BOOST) {
//if both the wrapped query and the wrapper hold a boost, the main one coming from the wrapper takes precedence
query.setBoost(boost);
}
//no-op this query doesn't support boost
}
@Override

View File

@ -216,11 +216,14 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
protected final QB createTestQueryBuilder() {
QB query = doCreateTestQueryBuilder();
if (randomBoolean()) {
query.boost(2.0f / randomIntBetween(1, 20));
}
if (randomBoolean()) {
query.queryName(randomAsciiOfLengthBetween(1, 10));
//we should not set boost and query name for queries that don't parse it
if (supportsBoostAndQueryName()) {
if (randomBoolean()) {
query.boost(2.0f / randomIntBetween(1, 20));
}
if (randomBoolean()) {
query.queryName(randomAsciiOfLengthBetween(1, 10));
}
}
return query;
}
@ -237,11 +240,6 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
@Test
public void testFromXContent() throws IOException {
QB testQuery = createTestQueryBuilder();
//we should not set boost and query name for queries that don't parse it, so we simply reset them to their default values
if (supportsBoostAndQueryNameParsing() == false) {
testQuery.boost(AbstractQueryBuilder.DEFAULT_BOOST);
testQuery.queryName(null);
}
assertParsedQuery(testQuery.toString(), testQuery);
for (Map.Entry<String, QB> alternateVersion : getAlternateVersions().entrySet()) {
assertParsedQuery(alternateVersion.getKey(), alternateVersion.getValue());
@ -296,7 +294,7 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
assertThat("two equivalent query builders lead to different lucene queries", secondLuceneQuery, equalTo(firstLuceneQuery));
//if the initial lucene query is null, changing its boost won't have any effect, we shouldn't test that
if (firstLuceneQuery != null) {
if (firstLuceneQuery != null && supportsBoostAndQueryName()) {
secondQuery.boost(firstQuery.boost() + 1f + randomFloat());
Query thirdLuceneQuery = secondQuery.toQuery(context);
assertThat("modifying the boost doesn't affect the corresponding lucene query", firstLuceneQuery, not(equalTo(thirdLuceneQuery)));
@ -308,7 +306,7 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
* This method allows to disable boost and queryName related tests for those queries. Those queries are easy to identify: their parsers
* don't parse `boost` and `_name` as they don't apply to the specific query: filter query, wrapper query and match_none
*/
protected boolean supportsBoostAndQueryNameParsing() {
protected boolean supportsBoostAndQueryName() {
return true;
}
@ -322,12 +320,15 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
Query namedQuery = context.copyNamedQueries().get(queryBuilder.queryName());
assertThat(namedQuery, equalTo(query));
}
if (query != null) {
if (query != null && supportsBoostAndQueryName()) {
assertBoost(queryBuilder, query);
}
doAssertLuceneQuery(queryBuilder, query, context);
}
/**
* Allows to override boost assertions for queries that don't have the default behaviour
*/
protected void assertBoost(QB queryBuilder, Query query) throws IOException {
assertThat(query.getBoost(), equalTo(queryBuilder.boost()));
}

View File

@ -30,7 +30,7 @@ import static org.hamcrest.CoreMatchers.instanceOf;
public class MatchNoneQueryBuilderTests extends BaseQueryTestCase {
@Override
protected boolean supportsBoostAndQueryNameParsing() {
protected boolean supportsBoostAndQueryName() {
return false;
}

View File

@ -49,7 +49,7 @@ public class QueryFilterBuilderTests extends BaseQueryTestCase<QueryFilterBuilde
}
@Override
protected boolean supportsBoostAndQueryNameParsing() {
protected boolean supportsBoostAndQueryName() {
return false;
}

View File

@ -32,7 +32,7 @@ import static org.hamcrest.Matchers.is;
public class WrapperQueryBuilderTests extends BaseQueryTestCase<WrapperQueryBuilder> {
@Override
protected boolean supportsBoostAndQueryNameParsing() {
protected boolean supportsBoostAndQueryName() {
return false;
}
@ -58,18 +58,10 @@ public class WrapperQueryBuilderTests extends BaseQueryTestCase<WrapperQueryBuil
contextCopy.reset(qSourceParser);
QueryBuilder<?> innerQuery = contextCopy.parseContext().parseInnerQueryBuilder();
Query expected = innerQuery.toQuery(context);
if (expected != null && queryBuilder.boost() != AbstractQueryBuilder.DEFAULT_BOOST) {
expected.setBoost(queryBuilder.boost());
}
assertThat(query, equalTo(expected));
}
}
@Override
protected void assertBoost(WrapperQueryBuilder queryBuilder, Query query) throws IOException {
//nothing to do here, boost check is already included in equality check done as part of doAssertLuceneQuery above
}
@Test
public void testValidate() {
WrapperQueryBuilder wrapperQueryBuilder = new WrapperQueryBuilder((byte[]) null);