From 8cb4471cca1dee05e1e5bcd557295876b129e9ea Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Thu, 24 Jul 2014 11:13:25 +0200 Subject: [PATCH] [TESTS] Add more assertions to SimpleFacetsTests. --- .../search/facet/SimpleFacetsTests.java | 127 ++++++++---------- 1 file changed, 53 insertions(+), 74 deletions(-) diff --git a/src/test/java/org/elasticsearch/search/facet/SimpleFacetsTests.java b/src/test/java/org/elasticsearch/search/facet/SimpleFacetsTests.java index 2c3b6bde353..19e93636f3c 100644 --- a/src/test/java/org/elasticsearch/search/facet/SimpleFacetsTests.java +++ b/src/test/java/org/elasticsearch/search/facet/SimpleFacetsTests.java @@ -57,8 +57,7 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.index.query.FilterBuilders.termFilter; import static org.elasticsearch.index.query.QueryBuilders.*; import static org.elasticsearch.search.facet.FacetBuilders.*; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; -import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount; +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.*; import static org.hamcrest.Matchers.*; /** @@ -112,6 +111,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { "{\"facet1\":{\"terms\":{\"field\":\"tag\"},\"facet_filter\":{ }}}")) .get(); + assertSearchResponse(searchResponse); assertHitCount(searchResponse, 1l); assertThat(searchResponse.getHits().hits().length, equalTo(0)); TermsFacet facet = searchResponse.getFacets().facet("facet1"); @@ -150,6 +150,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .endObject().bytes()) .execute().actionGet(); + assertSearchResponse(searchResponse); assertThat(searchResponse.getHits().totalHits(), equalTo(2l)); assertThat(searchResponse.getHits().hits().length, equalTo(0)); TermsFacet facet = searchResponse.getFacets().facet("facet1"); @@ -212,6 +213,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .execute().actionGet(); + assertSearchResponse(searchResponse); assertThat(searchResponse.getHits().getTotalHits(), equalTo(110l)); TermsFacet facet = searchResponse.getFacets().facet("termFacet"); assertThat(facet.getName(), equalTo("termFacet")); @@ -390,8 +392,9 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("termFacet").executionHint("map").field("name").size(10)); } - SearchResponse actionGet = facetRequest.execute().actionGet(); - return actionGet.getFacets(); + SearchResponse searchResponse = facetRequest.execute().actionGet(); + assertSearchResponse(searchResponse); + return searchResponse.getFacets(); } }, 5000 ); @@ -511,7 +514,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .startObject("fielddata").field("format", "fst").field("loading", randomBoolean() ? "eager" : "lazy").startObject("filter") .startObject("regex").field("pattern", "\\d{1,2}").endObject().endObject() .endObject() - // only 1 or 2 digits + // only 1 or 2 digits .endObject() .startObject("filtered_mv") .field("type", "string") @@ -626,8 +629,9 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("termFacet").executionHint("map").field(field).size(10)); break; } - SearchResponse actionGet = facetRequest.execute().actionGet(); - return actionGet.getFacets(); + SearchResponse searchResponse = facetRequest.execute().actionGet(); + assertSearchResponse(searchResponse); + return searchResponse.getFacets(); } }, 2000 ); @@ -658,6 +662,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("tag").size(10)) .execute().actionGet(); + assertSearchResponse(searchResponse); assertThat(searchResponse.getHits().hits().length, equalTo(2)); TermsFacet facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); @@ -673,6 +678,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("tag").size(10)) .execute().actionGet(); + assertSearchResponse(searchResponse); assertThat(searchResponse.getHits().hits().length, equalTo(1)); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); @@ -713,6 +719,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("stag").size(10)) .execute().actionGet(); + assertSearchResponse(searchResponse); assertThat(searchResponse.getHits().hits().length, equalTo(0)); TermsFacet facet = searchResponse.getFacets().facet("facet1"); @@ -729,6 +736,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet2").field("tag").size(10)) .execute().actionGet(); + assertSearchResponse(searchResponse); assertThat(searchResponse.getHits().hits().length, equalTo(0)); facet = searchResponse.getFacets().facet("facet1"); @@ -766,7 +774,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("_index").size(10)) .execute().actionGet(); - + assertSearchResponse(searchResponse); TermsFacet facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(2)); @@ -775,13 +783,6 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { assertThat(facet.getEntries().get(1).getTerm().string(), equalTo("test2")); assertThat(facet.getEntries().get(1).getCount(), equalTo(1)); } - - try { - client().admin().indices().prepareDelete("test1").execute().actionGet(); - client().admin().indices().prepareDelete("test2").execute().actionGet(); - } catch (Exception e) { - // ignore - } } @Test @@ -811,6 +812,8 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(filterFacet("facet4").filter(termFilter("tag", "zzz"))) .execute().actionGet(); + assertSearchResponse(searchResponse); + FilterFacet facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getCount(), equalTo(2l)); @@ -862,6 +865,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("stag").size(10)) .execute().actionGet(); + assertSearchResponse(searchResponse); TermsFacet facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getMissingCount(), equalTo(1l)); } @@ -925,6 +929,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet2").field("tag").size(10).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); TermsFacet facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getTotalCount(), equalTo(2l)); @@ -948,6 +953,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet3").field("ltag").size(10).exclude(3000).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet, instanceOf(InternalLongTermsFacet.class)); assertThat(facet.getName(), equalTo("facet1")); @@ -981,6 +987,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet2").field("dtag").size(10).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet, instanceOf(InternalDoubleTermsFacet.class)); assertThat(facet.getName(), equalTo("facet1")); @@ -1015,6 +1022,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("istag").size(10).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(1)); @@ -1039,6 +1047,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("stag").size(10).facetFilter(termFilter("tag", "xxx")).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(1)); @@ -1051,6 +1060,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("stag").size(10).facetFilter(termFilter("tag", "xxx")).global(true).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(1)); @@ -1064,6 +1074,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("type1.stag").size(10).facetFilter(termFilter("tag", "xxx")).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(1)); @@ -1092,6 +1103,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("tag").size(2).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(2)); @@ -1107,6 +1119,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("tag").size(10).exclude("yyy").executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(2)); @@ -1122,6 +1135,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("tag").size(10).order(TermsFacet.ComparatorType.TERM).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(3)); @@ -1137,6 +1151,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("tag").size(10).order(TermsFacet.ComparatorType.REVERSE_TERM).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(3)); @@ -1169,6 +1184,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("tag").size(10).script("term == 'xxx' ? false : true").order(TermsFacet.ComparatorType.TERM).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(2)); @@ -1184,6 +1200,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").fields("stag", "tag").size(10).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(4)); @@ -1201,6 +1218,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("tag").size(10).allTerms(true).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(3)); @@ -1216,6 +1234,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").fields("tag", "stag").size(10).allTerms(true).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(4)); @@ -1233,6 +1252,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("ltag").size(10).allTerms(true).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(3)); @@ -1248,6 +1268,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("dtag").size(10).allTerms(true).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(3)); @@ -1266,6 +1287,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet2").scriptField("_source.tag").size(10).executionHint(executionHint)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getTotalCount(), equalTo(2l)); @@ -1314,6 +1336,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsFacet("facet1").field("text").size(10)) .execute().actionGet(); + assertSearchResponse(searchResponse); TermsFacet facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(3)); @@ -1356,13 +1379,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(statisticalScriptFacet("stats3").script("doc['num'].value * 2")) .execute().actionGet(); - if (searchResponse.getFailedShards() > 0) { - logger.warn("Failed shards:"); - for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) { - logger.warn("-> {}", shardSearchFailure); - } - } - assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertSearchResponse(searchResponse); StatisticalFacet facet = searchResponse.getFacets().facet("stats1"); assertThat(facet.getName(), equalTo(facet.getName())); @@ -1397,6 +1414,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("stats"); assertThat(facet.getName(), equalTo(facet.getName())); assertThat(facet.getCount(), equalTo(6l)); @@ -1414,6 +1432,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("stats"); assertThat(facet.getName(), equalTo(facet.getName())); assertThat(facet.getCount(), equalTo(6l)); @@ -1457,7 +1476,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { logger.warn("-> {}", shardSearchFailure); } } - assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertSearchResponse(searchResponse); HistogramFacet facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); @@ -1516,13 +1535,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(histogramScriptFacet("stats8").keyField("num").valueScript("doc.score").interval(100)) .execute().actionGet(); - if (searchResponse.getFailedShards() > 0) { - logger.warn("Failed shards:"); - for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) { - logger.warn("-> {}", shardSearchFailure); - } - } - assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertSearchResponse(searchResponse); HistogramFacet facet; @@ -1689,13 +1702,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(rangeFacet("range6").field("date").addUnboundedFrom("1970-01-01T00:00:26").addRange("1970-01-01T00:00:15", "1970-01-01T00:00:53").addUnboundedTo("1970-01-01T00:00:26")) .execute().actionGet(); - if (searchResponse.getFailedShards() > 0) { - logger.warn("Failed shards:"); - for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) { - logger.warn("-> {}", shardSearchFailure); - } - } - assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertSearchResponse(searchResponse); RangeFacet facet = searchResponse.getFacets().facet("range1"); assertThat(facet.getName(), equalTo("range1")); @@ -1856,13 +1863,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(dateHistogramFacet("stats8").field("date_in_seconds").interval("day").factor(1000f).mode(mode)) .execute().actionGet(); - if (searchResponse.getFailedShards() > 0) { - logger.warn("Failed shards:"); - for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) { - logger.warn("-> {}", shardSearchFailure); - } - } - assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertSearchResponse(searchResponse); DateHistogramFacet facet = searchResponse.getFacets().facet("stats1"); assertThat(facet.getName(), equalTo("stats1")); @@ -1926,7 +1927,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { assertThat(facet.getEntries().size(), equalTo(1)); assertThat(facet.getEntries().get(0).getTime(), equalTo(utcTimeInMillis("2009-01-01"))); - // check date_histogram on a long field containing date in seconds - we use a factor. + // check date_histogram on a long field containing date in seconds - we use a factor. facet = searchResponse.getFacets().facet("stats8"); assertThat(facet.getName(), equalTo("stats8")); assertThat(facet.getEntries().size(), equalTo(2)); @@ -1974,13 +1975,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(dateHistogramFacet("stats2").field("date").valueField("num").interval("day").preZone("+01:30")) .execute().actionGet(); - if (searchResponse.getFailedShards() > 0) { - logger.warn("Failed shards:"); - for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) { - logger.warn("-> {}", shardSearchFailure); - } - } - assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertSearchResponse(searchResponse); // time zone causes the dates to shift by 2:00 DateHistogramFacet facet = searchResponse.getFacets().facet("stats1"); @@ -2052,13 +2047,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsStatsFacet("stats13").keyField("field").valueScript("doc['num'].value * 2")) .execute().actionGet(); - if (searchResponse.getFailedShards() > 0) { - logger.warn("Failed shards:"); - for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) { - logger.warn("-> {}", shardSearchFailure); - } - } - assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertSearchResponse(searchResponse); TermsStatsFacet facet = searchResponse.getFacets().facet("stats1"); assertThat(facet.getEntries().size(), equalTo(2)); @@ -2228,13 +2217,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsStatsFacet("stats2").keyField("dField").valueField("num")) .execute().actionGet(); - if (searchResponse.getFailedShards() > 0) { - logger.warn("Failed shards:"); - for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) { - logger.warn("-> {}", shardSearchFailure); - } - } - assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertSearchResponse(searchResponse); TermsStatsFacet facet = searchResponse.getFacets().facet("stats1"); assertThat(facet.getEntries().size(), equalTo(2)); @@ -2285,13 +2268,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(termsStatsFacet("stats2").keyField("num").valueScript("doc.score").order(TermsStatsFacet.ComparatorType.TOTAL)) .execute().actionGet(); - if (searchResponse.getFailedShards() > 0) { - logger.warn("Failed shards:"); - for (ShardSearchFailure shardSearchFailure : searchResponse.getShardFailures()) { - logger.warn("-> {}", shardSearchFailure); - } - } - assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertSearchResponse(searchResponse); TermsStatsFacet facet = searchResponse.getFacets().facet("stats1"); assertThat(facet.getEntries().size(), equalTo(10)); @@ -2324,6 +2301,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(queryFacet("query").query(termQuery("num", 1)).global(true)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("query"); assertThat(facet.getCount(), equalTo(2l)); @@ -2332,6 +2310,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { .addFacet(queryFacet("query").query(termsQuery("num", new long[]{1, 2})).facetFilter(termFilter("num", 1)).global(true)) .execute().actionGet(); + assertSearchResponse(searchResponse); facet = searchResponse.getFacets().facet("query"); assertThat(facet.getCount(), equalTo(2l)); } @@ -2354,7 +2333,7 @@ public class SimpleFacetsTests extends ElasticsearchIntegrationTest { ) .execute().actionGet(); - assertThat(searchResponse.getFailedShards(), equalTo(0)); + assertSearchResponse(searchResponse); TermsFacet facet = searchResponse.getFacets().facet("facet1"); assertThat(facet.getName(), equalTo("facet1")); assertThat(facet.getEntries().size(), equalTo(0));