def test_add_field_facet_validates_options(self): """ Verify that add_field_facet validates the provided options.""" query = DistinctCountsSearchQuery() with pytest.raises(RuntimeError) as err: query.add_field_facet('pacing_type', order='term') assert 'only supports a limited set of field facet options' in str( err.value) query.add_field_facet('pacing_type', size=5) assert query.facets['pacing_type_exact']['size'] == 5
def test_clone(self): """ Verify that clone copies all fields, including the aggregation_key and distinct_hit_count.""" query = DistinctCountsSearchQuery() query.add_field_facet('pacing_type') query.aggregation_key = 'aggregation_key' query._distinct_hit_count = 123 clone = query._clone() assert query.facets == clone.facets assert query.aggregation_key == clone.aggregation_key assert query._distinct_hit_count == clone._distinct_hit_count
def test_clone_with_different_class(self): """ Verify that clone does not copy aggregation_key and distinct_hit_count when using different class.""" query = DistinctCountsSearchQuery() query.add_field_facet('pacing_type') query.aggregation_key = 'aggregation_key' query._distinct_hit_count = 123 clone = query._clone(klass=ElasticsearchSearchQuery) assert isinstance(clone, ElasticsearchSearchQuery) assert query.facets == clone.facets assert not hasattr(clone, 'aggregation_key') assert not hasattr(clone, '_distinct_hit_count')
def test_run_executes_the_query_and_caches_the_results(self): """ Verify that run executes the query and caches the results.""" course_1 = CourseFactory() run_1 = CourseRunFactory(title='foo', pacing_type='self_paced', hidden=True, course=course_1) run_2 = CourseRunFactory(title='foo', pacing_type='self_paced', hidden=True, course=course_1) course_2 = CourseFactory() run_3 = CourseRunFactory(title='foo', pacing_type='instructor_paced', hidden=False, course=course_2) CourseRunFactory(title='bar', pacing_type='instructor_paced', hidden=False, course=course_2) query = DistinctCountsSearchQuery() query.aggregation_key = 'aggregation_key' query.add_filter(SQ(title='foo')) query.add_model(CourseRun) query.add_field_facet('pacing_type') query.add_query_facet('hidden', 'hidden:true') assert query._distinct_hit_count is None assert query._hit_count is None assert query._results is None assert query._facet_counts is None query.run() expected_results = sorted([run_1.key, run_2.key, run_3.key]) actual_results = sorted([run.key for run in query._results]) assert query._distinct_hit_count == 2 assert query._hit_count == 3 assert expected_results == actual_results facet_counts = query._facet_counts for field_val, count, distinct_count in facet_counts['fields'][ 'pacing_type']: assert field_val in {'self_paced', 'instructor_paced'} if field_val == 'self_paced': assert count == 2 and distinct_count == 1 elif field_val == 'instructor_paced': assert count == 1 and distinct_count == 1 count, distinct_count = facet_counts['queries']['hidden'] assert count == 2 and distinct_count == 1