def test_parse_result(self): agg = query.TagsAggregation() elasticsearch_result = { 'buckets': [ { 'key': 'tag-4', 'doc_count': 42 }, { 'key': 'tag-2', 'doc_count': 28 }, ] } assert agg.parse_result(elasticsearch_result) == [ { 'tag': 'tag-4', 'count': 42 }, { 'tag': 'tag-2', 'count': 28 }, ]
def test_it_returns_annotation_counts_by_tag(self, Annotation, search): for i in range(2): Annotation(tags=["tag_a"]) Annotation(tags=["tag_b"]) search.append_aggregation(query.TagsAggregation()) result = search.run(webob.multidict.MultiDict({})) tag_results = result.aggregations["tags"] count_for_tag_a = next(r for r in tag_results if r["tag"] == "tag_a")["count"] count_for_tag_b = next(r for r in tag_results if r["tag"] == "tag_b")["count"] assert len(tag_results) == 2 assert count_for_tag_a == 2 assert count_for_tag_b == 1
def test_it_limits_number_of_annotation_counts_by_tag_returned(self, Annotation, search): bucket_limit = 2 Annotation(tags=["tag_a"]) for i in range(3): Annotation(tags=["tag_b"]) for i in range(2): Annotation(tags=["tag_c"]) search.append_aggregation(query.TagsAggregation(bucket_limit)) result = search.run({}) tag_results = result.aggregations["tags"] count_for_tag_b = next(r for r in tag_results if r["tag"] == "tag_b")["count"] count_for_tag_c = next(r for r in tag_results if r["tag"] == "tag_c")["count"] assert len(tag_results) == bucket_limit assert count_for_tag_b == 3 assert count_for_tag_c == 2
def test_it_allows_to_set_a_limit(self): agg = query.TagsAggregation(limit=14) assert agg({}) == { 'terms': {'field': 'tags_raw', 'size': 14} }
def test_elasticsearch_aggregation(self): agg = query.TagsAggregation() assert agg({}) == { 'terms': {'field': 'tags_raw', 'size': 10} }
def test_key_is_tags(self): assert query.TagsAggregation().key == 'tags'
def test_parse_result_with_empty(self): agg = query.TagsAggregation() assert agg.parse_result({}) == {}
def test_parse_result_with_none(self): agg = query.TagsAggregation() assert agg.parse_result(None) == {}
def search(self, search): search.append_aggregation(query.TagsAggregation()) return search