From 50c43c9328b248cd5357ef90fee57ec9f31772dc Mon Sep 17 00:00:00 2001 From: javanna Date: Tue, 8 Sep 2015 19:40:31 +0200 Subject: [PATCH] [TEST] minor adjustments to boost assertions for queries that don't support it --- .../index/query/MatchNoneQueryBuilder.java | 5 ++++ .../index/query/QueryFilterBuilder.java | 5 ++++ .../index/query/WrapperQueryBuilder.java | 5 +--- .../index/query/BaseQueryTestCase.java | 27 ++++++++++--------- .../query/MatchNoneQueryBuilderTests.java | 2 +- .../index/query/QueryFilterBuilderTests.java | 2 +- .../index/query/WrapperQueryBuilderTests.java | 10 +------ 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/query/MatchNoneQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/MatchNoneQueryBuilder.java index 247f514fbab..e6d6a7d3f13 100644 --- a/core/src/main/java/org/elasticsearch/index/query/MatchNoneQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/MatchNoneQueryBuilder.java @@ -47,6 +47,11 @@ public class MatchNoneQueryBuilder extends AbstractQueryBuilder 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); diff --git a/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java index bd1cb32870b..f4a75c7245e 100644 --- a/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/WrapperQueryBuilder.java @@ -106,10 +106,7 @@ public class WrapperQueryBuilder extends AbstractQueryBuilder> 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> 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 alternateVersion : getAlternateVersions().entrySet()) { assertParsedQuery(alternateVersion.getKey(), alternateVersion.getValue()); @@ -296,7 +294,7 @@ public abstract class BaseQueryTestCase> 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> 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> 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())); } diff --git a/core/src/test/java/org/elasticsearch/index/query/MatchNoneQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/MatchNoneQueryBuilderTests.java index 5d9d0a72c89..1511243f109 100644 --- a/core/src/test/java/org/elasticsearch/index/query/MatchNoneQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/MatchNoneQueryBuilderTests.java @@ -30,7 +30,7 @@ import static org.hamcrest.CoreMatchers.instanceOf; public class MatchNoneQueryBuilderTests extends BaseQueryTestCase { @Override - protected boolean supportsBoostAndQueryNameParsing() { + protected boolean supportsBoostAndQueryName() { return false; } diff --git a/core/src/test/java/org/elasticsearch/index/query/QueryFilterBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/QueryFilterBuilderTests.java index ff9508306be..6530e65d310 100644 --- a/core/src/test/java/org/elasticsearch/index/query/QueryFilterBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/QueryFilterBuilderTests.java @@ -49,7 +49,7 @@ public class QueryFilterBuilderTests extends BaseQueryTestCase { @Override - protected boolean supportsBoostAndQueryNameParsing() { + protected boolean supportsBoostAndQueryName() { return false; } @@ -58,18 +58,10 @@ public class WrapperQueryBuilderTests extends BaseQueryTestCase 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);