From 314a3343f93a4af884c117113e23b70f347ae534 Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Mon, 10 Jun 2013 16:05:34 +0200 Subject: [PATCH] Add more verbose matchers / asserts to tests --- .../SimpleRecoveryLocalGatewayTests.java | 13 ++- .../terms/UnmappedFieldsTermsFacetsTests.java | 2 +- .../search/rescore/QueryRescorerTests.java | 81 +++++++------------ 3 files changed, 40 insertions(+), 56 deletions(-) diff --git a/src/test/java/org/elasticsearch/test/integration/gateway/local/SimpleRecoveryLocalGatewayTests.java b/src/test/java/org/elasticsearch/test/integration/gateway/local/SimpleRecoveryLocalGatewayTests.java index 1ca45ffa206..f997c6e601d 100644 --- a/src/test/java/org/elasticsearch/test/integration/gateway/local/SimpleRecoveryLocalGatewayTests.java +++ b/src/test/java/org/elasticsearch/test/integration/gateway/local/SimpleRecoveryLocalGatewayTests.java @@ -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 diff --git a/src/test/java/org/elasticsearch/test/integration/search/facet/terms/UnmappedFieldsTermsFacetsTests.java b/src/test/java/org/elasticsearch/test/integration/search/facet/terms/UnmappedFieldsTermsFacetsTests.java index 6197e348fd2..5631aa2f04d 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/facet/terms/UnmappedFieldsTermsFacetsTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/facet/terms/UnmappedFieldsTermsFacetsTests.java @@ -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)) diff --git a/src/test/java/org/elasticsearch/test/integration/search/rescore/QueryRescorerTests.java b/src/test/java/org/elasticsearch/test/integration/search/rescore/QueryRescorerTests.java index 6737dae9950..6243b79908f 100644 --- a/src/test/java/org/elasticsearch/test/integration/search/rescore/QueryRescorerTests.java +++ b/src/test/java/org/elasticsearch/test/integration/search/rescore/QueryRescorerTests.java @@ -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());