def test_search_raises_when_called_with_date_facet(self): queryset = DistinctCountsSearchQuerySet( index=CourseRunDocument._index._name) facet_field = 'start' agg_filter = ESDSLQ('match_all') agg = DateHistogramFacet(field=facet_field, interval='month') queryset.aggs.bucket('_filter_' + facet_field, 'filter', filter=agg_filter).bucket(facet_field, agg.get_aggregation()) backend = DistinctCountsElasticsearchQueryWrapper( queryset, 'aggregation_key') querystring = queryset.to_dict() with pytest.raises(RuntimeError) as err: backend.search(querystring) assert 'does not support date facets' in str(err.value)
def test_clone_with_different_class(self): """ Verify that clone does not copy aggregation_key and distinct_result_count when using different class.""" queryset = DistinctCountsSearchQuerySet() facet_field = 'pacing_type' agg_filter = ESDSLQ('match_all') agg = TermsFacet(field=facet_field) queryset.aggs.bucket('_filter_' + facet_field, 'filter', filter=agg_filter).bucket(facet_field, agg.get_aggregation()) queryset.aggregation_key = 'aggregation_key' queryset._distinct_result_count = 123 clone = queryset._clone(klass=Search) assert isinstance(clone, Search) assert queryset.to_dict() == clone.to_dict() assert not hasattr(clone, 'aggregation_key') assert not hasattr(clone, '_distinct_result_count')
def test_clone(self): """ Verify that clone copies all fields, including the aggregation_key and distinct_hit_count.""" queryset = DistinctCountsSearchQuerySet() facet_field = 'pacing_type' agg_filter = ESDSLQ('match_all') agg = TermsFacet(field=facet_field) queryset.aggs.bucket('_filter_' + facet_field, 'filter', filter=agg_filter).bucket(facet_field, agg.get_aggregation()) queryset.aggregation_key = 'aggregation_key' queryset._distinct_result_count = 123 clone = queryset._clone() assert queryset.to_dict() == clone.to_dict() assert queryset.aggregation_key == clone.aggregation_key assert queryset._distinct_result_count == clone._distinct_result_count
def test_search_raises_when_called_with_unsupported_field_facet_option( self): queryset = DistinctCountsSearchQuerySet( index=CourseRunDocument._index._name) facet_field = 'pacing_type' agg_filter = ESDSLQ('match_all') agg = TermsFacet(field=facet_field, order='term') queryset.aggs.bucket('_filter_' + facet_field, 'filter', filter=agg_filter).bucket(facet_field, agg.get_aggregation()) backend = DistinctCountsElasticsearchQueryWrapper( queryset, 'aggregation_key') querystring = queryset.to_dict() with pytest.raises(RuntimeError) as err: backend.search(querystring) assert 'only supports a limited set of field facet options' in str( err.value)
def test_build_search_kwargs_does_not_include_facet_clause(self): """ Verify that a facets clause is not included with search kwargs.""" queryset = DistinctCountsSearchQuerySet( index=CourseRunDocument._index._name) facet_field = 'pacing_type' agg_filter = ESDSLQ('match_all') agg = TermsFacet(field=facet_field) queryset.aggs.bucket('_filter_' + facet_field, 'filter', filter=agg_filter).bucket(facet_field, agg.get_aggregation()) queryset.aggs.bucket('_query_{0}'.format('hidden'), 'filter', filter=ESDSLQ('bool', filter=ESDSLQ('term', hidden=True))) querystring_params = queryset.to_dict() backend = DistinctCountsElasticsearchQueryWrapper( queryset, 'aggregation_key') search_kwargs = backend._build_search_kwargs(**querystring_params) assert 'facets' not in search_kwargs assert 'aggs' in search_kwargs