[TEST] parse queries through QueryParseContext in BaseQueryTestCase#parseQuery

This allows us to remove the string argument from the parseQuery method and the need for removing the first few tokens from the query, as the selected parser knows only about his portion of dsl.
This commit is contained in:
javanna 2015-08-31 09:55:37 +02:00 committed by Luca Cavanna
parent 4ed198fef0
commit 2958751a2d
12 changed files with 22 additions and 30 deletions

View File

@ -83,7 +83,7 @@ public class AndQueryBuilderTest extends BaseQueryTestCase<AndQueryBuilder> {
@Test(expected=QueryParsingException.class)
public void testMissingFiltersSection() throws IOException {
parseQuery("{ \"and\" : {}", AndQueryBuilder.NAME);
parseQuery("{ \"and\" : {}");
}
@Test
@ -119,6 +119,6 @@ public class AndQueryBuilderTest extends BaseQueryTestCase<AndQueryBuilder> {
@Test(expected=QueryParsingException.class)
public void testParsingExceptionNonFiltersElementArray() throws IOException {
String queryString = "{ \"and\" : { \"whatever_filters\" : [ { \"match_all\" : {} } ] } }";
parseQuery(queryString, AndQueryBuilder.NAME);
parseQuery(queryString);
}
}

View File

@ -252,18 +252,17 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
* Parses the query provided as string argument and compares it with the expected result provided as argument as a {@link QueryBuilder}
*/
protected void assertParsedQuery(String queryAsString, QueryBuilder<?> expectedQuery) throws IOException {
QueryBuilder<?> newQuery = parseQuery(queryAsString, expectedQuery.getName());
QueryBuilder<?> newQuery = parseQuery(queryAsString);
assertNotSame(newQuery, expectedQuery);
assertEquals(expectedQuery, newQuery);
assertEquals(expectedQuery.hashCode(), newQuery.hashCode());
}
protected QueryBuilder<?> parseQuery(String queryAsString, String queryId) throws IOException {
protected QueryBuilder<?> parseQuery(String queryAsString) throws IOException {
XContentParser parser = XContentFactory.xContent(queryAsString).createParser(queryAsString);
QueryParseContext context = createParseContext();
context.reset(parser);
assertQueryHeader(parser, queryId);
return context.queryParser(queryId).fromXContent(context);
return context.parseInnerQueryBuilder();
}
/**
@ -412,13 +411,6 @@ public abstract class BaseQueryTestCase<QB extends AbstractQueryBuilder<QB>> ext
return createShardContext().parseContext();
}
private static void assertQueryHeader(XContentParser parser, String expectedParserName) throws IOException {
assertThat(parser.nextToken(), is(XContentParser.Token.START_OBJECT));
assertThat(parser.nextToken(), is(XContentParser.Token.FIELD_NAME));
assertThat(parser.currentName(), is(expectedParserName));
assertThat(parser.nextToken(), either(is(XContentParser.Token.START_OBJECT)).or(is(XContentParser.Token.START_ARRAY)));
}
protected static void assertValidate(QueryBuilder queryBuilder, int totalExpectedErrors) {
QueryValidationException queryValidationException = queryBuilder.validate();
if (totalExpectedErrors > 0) {

View File

@ -55,7 +55,7 @@ public class ConstantScoreQueryBuilderTest extends BaseQueryTestCase<ConstantSco
@Test(expected=QueryParsingException.class)
public void testFilterElement() throws IOException {
String queryString = "{ \"" + ConstantScoreQueryBuilder.NAME + "\" : {}";
parseQuery(queryString, ConstantScoreQueryBuilder.NAME);
parseQuery(queryString);
}
@Test

View File

@ -100,8 +100,8 @@ public class DisMaxQueryBuilderTest extends BaseQueryTestCase<DisMaxQueryBuilder
*/
@Test
public void testInnerQueryReturnsNull() throws IOException {
String queryString = "{ \"" + ConstantScoreQueryBuilder.NAME + "\" : { \"filter\" : { } }";
QueryBuilder<?> innerQueryBuilder = parseQuery(queryString, ConstantScoreQueryBuilder.NAME);
String queryString = "{ \"" + ConstantScoreQueryBuilder.NAME + "\" : { \"filter\" : { } } }";
QueryBuilder<?> innerQueryBuilder = parseQuery(queryString);
DisMaxQueryBuilder disMaxBuilder = new DisMaxQueryBuilder().add(innerQueryBuilder);
assertNull(disMaxBuilder.toQuery(createShardContext()));
}

View File

@ -66,8 +66,8 @@ public class FQueryFilterBuilderTest extends BaseQueryTestCase<FQueryFilterBuild
@Test
public void testInnerQueryReturnsNull() throws IOException {
// create inner filter
String queryString = "{ \"constant_score\" : { \"filter\" : {} }";
QueryBuilder innerQuery = parseQuery(queryString, ConstantScoreQueryBuilder.NAME);
String queryString = "{ \"constant_score\" : { \"filter\" : {} } }";
QueryBuilder innerQuery = parseQuery(queryString);
// check that when wrapping this filter, toQuery() returns null
FQueryFilterBuilder queryFilterQuery = new FQueryFilterBuilder(innerQuery);
assertNull(queryFilterQuery.toQuery(createShardContext()));

View File

@ -42,7 +42,7 @@ public class IdsQueryBuilderTest extends BaseQueryTestCase<IdsQueryBuilder> {
@Test(expected=QueryParsingException.class)
public void testIdsNotProvided() throws IOException {
String noIdsFieldQuery = "{\"ids\" : { \"type\" : \"my_type\" }";
parseQuery(noIdsFieldQuery, IdsQueryBuilder.NAME);
parseQuery(noIdsFieldQuery);
}
@Override

View File

@ -63,7 +63,7 @@ public class NotQueryBuilderTest extends BaseQueryTestCase<NotQueryBuilder> {
@Test(expected=QueryParsingException.class)
public void testMissingFilterSection() throws IOException {
String queryString = "{ \"not\" : {}";
parseQuery(queryString, NotQueryBuilder.NAME);
parseQuery(queryString);
}
@Override

View File

@ -95,7 +95,7 @@ public class OrQueryBuilderTest extends BaseQueryTestCase<OrQueryBuilder> {
@Test(expected=QueryParsingException.class)
public void testMissingFiltersSection() throws IOException {
String queryString = "{ \"or\" : {}";
parseQuery(queryString, OrQueryBuilder.NAME);
parseQuery(queryString);
}
@Test

View File

@ -59,8 +59,8 @@ public class QueryFilterBuilderTest extends BaseQueryTestCase<QueryFilterBuilder
@Test
public void testInnerQueryReturnsNull() throws IOException {
// create inner filter
String queryString = "{ \"constant_score\" : { \"filter\" : {} }";
QueryBuilder<?> innerQuery = parseQuery(queryString, ConstantScoreQueryBuilder.NAME);
String queryString = "{ \"constant_score\" : { \"filter\" : {} } }";
QueryBuilder<?> innerQuery = parseQuery(queryString);
// check that when wrapping this filter, toQuery() returns null
QueryFilterBuilder queryFilterQuery = new QueryFilterBuilder(innerQuery);
assertNull(queryFilterQuery.toQuery(createShardContext()));

View File

@ -82,7 +82,7 @@ public class SpanFirstQueryBuilderTest extends BaseQueryTestCase<SpanFirstQueryB
builder.endObject();
try {
parseQuery(builder.string(), SpanFirstQueryBuilder.NAME);
parseQuery(builder.string());
fail("missing [end] parameter should raise exception");
} catch (QueryParsingException e) {
assertTrue(e.getMessage().contains("spanFirst must have [end] set"));
@ -98,7 +98,7 @@ public class SpanFirstQueryBuilderTest extends BaseQueryTestCase<SpanFirstQueryB
builder.endObject();
try {
parseQuery(builder.string(), SpanFirstQueryBuilder.NAME);
parseQuery(builder.string());
fail("missing [match] parameter should raise exception");
} catch (QueryParsingException e) {
assertTrue(e.getMessage().contains("spanFirst must have [match] span query clause"));

View File

@ -130,7 +130,7 @@ public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuild
builder.field("dist", 3);
builder.endObject();
builder.endObject();
SpanNotQueryBuilder query = (SpanNotQueryBuilder)parseQuery(builder.string(), SpanNotQueryBuilder.NAME);
SpanNotQueryBuilder query = (SpanNotQueryBuilder)parseQuery(builder.string());
assertThat(query.pre(), equalTo(3));
assertThat(query.post(), equalTo(3));
assertNotNull(query.includeQuery());
@ -154,7 +154,7 @@ public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuild
builder.endObject();
try {
parseQuery(builder.string(), SpanNotQueryBuilder.NAME);
parseQuery(builder.string());
fail("QueryParsingException should have been caught");
} catch (QueryParsingException e) {
assertThat("QueryParsingException should have been caught", e.getDetailedMessage(), containsString("spanNot must have [include]"));
@ -173,7 +173,7 @@ public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuild
builder.endObject();
try {
parseQuery(builder.string(), SpanNotQueryBuilder.NAME);
parseQuery(builder.string());
fail("QueryParsingException should have been caught");
} catch (QueryParsingException e) {
assertThat("QueryParsingException should have been caught", e.getDetailedMessage(), containsString("spanNot must have [exclude]"));
@ -195,7 +195,7 @@ public class SpanNotQueryBuilderTest extends BaseQueryTestCase<SpanNotQueryBuild
builder.endObject();
try {
parseQuery(builder.string(), SpanNotQueryBuilder.NAME);
parseQuery(builder.string());
fail("QueryParsingException should have been caught");
} catch (QueryParsingException e) {
assertThat("QueryParsingException should have been caught", e.getDetailedMessage(), containsString("spanNot can either use [dist] or [pre] & [post] (or none)"));

View File

@ -197,7 +197,7 @@ public class TermsQueryBuilderTest extends BaseQueryTestCase<TermsQueryBuilder>
" }\n" +
" }\n" +
"}";
QueryBuilder termsQueryBuilder = parseQuery(query, TermsQueryBuilder.NAME);
QueryBuilder termsQueryBuilder = parseQuery(query);
assertThat(termsQueryBuilder.validate().validationErrors().size(), is(1));
}