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) queryset = DistinctCountsSearchQuerySet( index=CourseRunDocument._index._name).filter('term', title='foo') queryset.aggregation_key = 'aggregation_key' 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))) assert queryset._distinct_result_count is None assert not hasattr(self, '_response') queryset.execute() expected_results = sorted([run_1.key, run_2.key, run_3.key]) actual_results = sorted([run.key for run in queryset._response.hits]) assert queryset._distinct_result_count == 2 assert queryset._response.hits.total['value'] == 3 assert expected_results == actual_results facet_counts = queryset._response.facets 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
def test_run_handles_pagination(self): """ Verify that run supports paginated queries. """ course_1 = CourseFactory() for _ in range(5): CourseRunFactory(title='foo', course=course_1) queryset = DistinctCountsSearchQuerySet( index=CourseRunDocument._index._name).filter('term', title='foo') queryset.aggregation_key = 'aggregation_key' queryset.execute() all_results = queryset._response.hits assert len(all_results) == 5 del queryset._response queryset = queryset.query().extra(from_=1, size=2) queryset.execute() paginated_results = queryset._response.hits assert len(paginated_results) == 2 expected = sorted([run.key for run in all_results[1:3]]) actual = sorted([run.key for run in paginated_results]) assert expected == actual