Add more verbose matchers / asserts to tests

This commit is contained in:
Simon Willnauer 2013-06-10 16:05:34 +02:00
parent f64f7c0c08
commit 314a3343f9
3 changed files with 40 additions and 56 deletions

View File

@ -19,6 +19,10 @@
package org.elasticsearch.test.integration.gateway.local;
import org.elasticsearch.test.hamcrest.ElasticsearchAssertions;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.action.admin.indices.status.IndexShardStatus;
@ -44,6 +48,7 @@ import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilde
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
@ -105,7 +110,9 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
node1.client().admin().indices().prepareRefresh().execute().actionGet();
assertThat(node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet().getCount(), equalTo(2l));
CountResponse count = node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet();
assertNoFailures(count);
assertThat(count.getCount(), equalTo(2l));
closeNode("node1");
node1 = startNode("node1", settingsBuilder().put("gateway.type", "local").build());
@ -117,7 +124,9 @@ public class SimpleRecoveryLocalGatewayTests extends AbstractNodesTests {
assertThat(clusterHealth.getStatus(), equalTo(ClusterHealthStatus.YELLOW));
node1.client().admin().indices().prepareRefresh().execute().actionGet();
assertThat(node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet().getCount(), equalTo(2l));
count = node1.client().prepareCount().setQuery(termQuery("appAccountIds", 179)).execute().actionGet();
assertNoFailures(count);
assertThat(count.getCount(), equalTo(2l));
}
@Test

View File

@ -202,7 +202,7 @@ public class UnmappedFieldsTermsFacetsTests extends AbstractSharedClusterTest {
client().admin().indices().prepareFlush().setRefresh(true).execute().actionGet();
SearchResponse searchResponse = client().prepareSearch()
SearchResponse searchResponse = client().prepareSearch("mapped_idx", "unmapped_idx")
.setQuery(matchAllQuery())
.addFacet(termsFacet("mapped").field("mapped").size(10))
.addFacet(termsFacet("partially_mapped_str").field("partially_mapped_str").size(10))

View File

@ -24,6 +24,9 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.elasticsearch.test.hamcrest.ElasticsearchMatchers.SearchHitHasIdMatcher;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*;
import org.apache.lucene.util.English;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
@ -46,12 +49,6 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
@Test
public void testRescorePhrase() throws Exception {
try {
client().admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
// ignore
}
client().admin()
.indices()
.prepareCreate("test")
@ -67,8 +64,7 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
client().prepareIndex("test", "type1", "3")
.setSource("field1", "quick huge brown", "field2", "the quick lazy huge brown fox jumps over the tree").execute()
.actionGet();
client().admin().indices().prepareRefresh("test").execute().actionGet();
refresh();
SearchResponse searchResponse = client().prepareSearch()
.setQuery(QueryBuilders.matchQuery("field1", "the quick brown").operator(MatchQueryBuilder.Operator.OR))
.setRescorer(RescoreBuilder.queryRescorer(QueryBuilders.matchPhraseQuery("field1", "quick brown").slop(2).boost(4.0f)))
@ -84,30 +80,24 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
.setRescorer(RescoreBuilder.queryRescorer(QueryBuilders.matchPhraseQuery("field1", "the quick brown").slop(3)))
.setRescoreWindow(5).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(3l));
assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1"));
assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2"));
assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("3"));
assertHitCount(searchResponse, 3);
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("1"));
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("2"));
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
searchResponse = client().prepareSearch()
.setQuery(QueryBuilders.matchQuery("field1", "the quick brown").operator(MatchQueryBuilder.Operator.OR))
.setRescorer(RescoreBuilder.queryRescorer((QueryBuilders.matchPhraseQuery("field1", "the quick brown"))))
.setRescoreWindow(5).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(3l));
assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1"));
assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2"));
assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("3"));
assertHitCount(searchResponse, 3);
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("1"));
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("2"));
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
}
@Test
public void testMoreDocs() throws Exception {
try {
client().admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
// ignore
}
Builder builder = ImmutableSettings.builder();
builder.put("index.analysis.analyzer.synonym.tokenizer", "whitespace");
builder.putArray("index.analysis.analyzer.synonym.filter", "synonym", "lowercase");
@ -146,12 +136,11 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
RescoreBuilder.queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3))
.setQueryWeight(0.6f).setRescoreQueryWeight(2.0f)).setRescoreWindow(20).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(9l));
assertThat(searchResponse.getHits().hits().length, equalTo(5));
assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("2"));
assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("6"));
assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("3"));
assertHitCount(searchResponse, 9);
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("2"));
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("6"));
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
searchResponse = client()
.prepareSearch()
@ -163,11 +152,11 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
RescoreBuilder.queryRescorer(QueryBuilders.matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3))
.setQueryWeight(0.6f).setRescoreQueryWeight(2.0f)).setRescoreWindow(20).execute().actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(9l));
assertThat(searchResponse.getHits().hits().length, equalTo(5));
assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("2"));
assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("6"));
assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("3"));
assertHitCount(searchResponse, 9);
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("2"));
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("6"));
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
}
private static final void assertEquivalent(SearchResponse plain, SearchResponse rescored) {
@ -199,12 +188,6 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
@Test
public void testEquivalence() throws Exception {
try {
client().admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
// ignore
}
client().admin()
.indices()
.prepareCreate("test")
@ -277,40 +260,32 @@ public class QueryRescorerTests extends AbstractSharedClusterTest {
@Test
public void testExplain() throws Exception {
try {
client().admin().indices().prepareDelete("test").execute().actionGet();
} catch (Exception e) {
// ignore
}
client().admin()
.indices()
.prepareCreate("test")
prepareCreate("test")
.addMapping(
"type1",
jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1")
.field("analyzer", "whitespace").field("type", "string").endObject().endObject().endObject().endObject())
.setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 2)).execute().actionGet();
ensureGreen();
client().prepareIndex("test", "type1", "1").setSource("field1", "the quick brown fox").execute().actionGet();
client().prepareIndex("test", "type1", "2").setSource("field1", "the quick lazy huge brown fox jumps over the tree").execute()
.actionGet();
client().prepareIndex("test", "type1", "3")
.setSource("field1", "quick huge brown", "field2", "the quick lazy huge brown fox jumps over the tree").execute()
.actionGet();
client().admin().indices().prepareRefresh("test").execute().actionGet();
refresh();
SearchResponse searchResponse = client()
.prepareSearch()
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.matchQuery("field1", "the quick brown").operator(MatchQueryBuilder.Operator.OR))
.setRescorer(
RescoreBuilder.queryRescorer(QueryBuilders.matchPhraseQuery("field1", "the quick brown").slop(2).boost(4.0f))
.setQueryWeight(0.5f).setRescoreQueryWeight(0.4f)).setRescoreWindow(5).setExplain(true).execute()
.actionGet();
assertThat(searchResponse.getHits().totalHits(), equalTo(3l));
assertThat(searchResponse.getHits().getHits()[0].getId(), equalTo("1"));
assertThat(searchResponse.getHits().getHits()[1].getId(), equalTo("2"));
assertThat(searchResponse.getHits().getHits()[2].getId(), equalTo("3"));
assertHitCount(searchResponse, 3);
assertFirstHit(searchResponse, new SearchHitHasIdMatcher("1"));
assertSecondHit(searchResponse, new SearchHitHasIdMatcher("2"));
assertThirdHit(searchResponse, new SearchHitHasIdMatcher("3"));
for (int i = 0; i < 3; i++) {
assertThat(searchResponse.getHits().getAt(i).explanation(), notNullValue());