Skip to content

Commit

Permalink
Query Refactoring: Increase coverage of BaseQueryTestCase
Browse files Browse the repository at this point in the history
We are creating random queries for testing purposes in BaseQueryTestCase and discussed
using more repetitions of the basic test cases (e.g. serialization, fromXContent parsing)
for better coverage if the query has more options.

Closes #11021
  • Loading branch information
cbuescher committed May 7, 2015
1 parent e44c026 commit bcdc371
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/test/java/org/elasticsearch/index/query/BaseQueryTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
@Ignore
public abstract class BaseQueryTestCase<QB extends BaseQueryBuilder & Streamable> extends ElasticsearchTestCase {

private static int RANDOM_REPS = 20;

private static Injector injector;
private static IndexQueryParserService queryParserService;
private static Index index;
Expand Down Expand Up @@ -137,15 +139,18 @@ public static void after() throws Exception {
*/
@Test
public void testFromXContent() throws IOException {
QueryParseContext context = createContext();
String contentString = testQuery.toString();
XContentParser parser = XContentFactory.xContent(contentString).createParser(contentString);
context.reset(parser);
assertQueryHeader(parser, testQuery.parserName());

QueryBuilder newQuery = queryParserService.queryParser(testQuery.parserName()).fromXContent(context);
assertNotSame(newQuery, testQuery);
assertEquals(newQuery, testQuery);
for (int i = 0; i < RANDOM_REPS; i++) {
testQuery = createTestQueryBuilder();
QueryParseContext context = createContext();
String contentString = testQuery.toString();
XContentParser parser = XContentFactory.xContent(contentString).createParser(contentString);
context.reset(parser);
assertQueryHeader(parser, testQuery.parserName());

QueryBuilder newQuery = queryParserService.queryParser(testQuery.parserName()).fromXContent(context);
assertNotSame(newQuery, testQuery);
assertEquals(newQuery, testQuery);
}
}

/**
Expand All @@ -154,25 +159,31 @@ public void testFromXContent() throws IOException {
*/
@Test
public void testToQuery() throws IOException {
QueryParseContext context = createContext();
context.setMapUnmappedFieldAsString(true);
assertLuceneQuery(testQuery, testQuery.toQuery(context), context);
for (int i = 0; i < RANDOM_REPS; i++) {
testQuery = createTestQueryBuilder();
QueryParseContext context = createContext();
context.setMapUnmappedFieldAsString(true);
assertLuceneQuery(testQuery, testQuery.toQuery(context), context);
}
}

/**
* Test serialization and deserialization of the test query.
*/
@Test
public void testSerialization() throws IOException {
BytesStreamOutput output = new BytesStreamOutput();
testQuery.writeTo(output);

BytesStreamInput in = new BytesStreamInput(output.bytes());
QB deserializedQuery = createEmptyQueryBuilder();
deserializedQuery.readFrom(in);

assertEquals(deserializedQuery, testQuery);
assertNotSame(deserializedQuery, testQuery);
for (int i = 0; i < RANDOM_REPS; i++) {
testQuery = createTestQueryBuilder();
BytesStreamOutput output = new BytesStreamOutput();
testQuery.writeTo(output);

BytesStreamInput in = new BytesStreamInput(output.bytes());
QB deserializedQuery = createEmptyQueryBuilder();
deserializedQuery.readFrom(in);

assertEquals(deserializedQuery, testQuery);
assertNotSame(deserializedQuery, testQuery);
}
}

/**
Expand Down

0 comments on commit bcdc371

Please sign in to comment.