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) query = DistinctCountsSearchQuery() query.aggregation_key = 'aggregation_key' query.add_filter(SQ(title='foo')) query.add_model(CourseRun) query.run() all_results = query._results assert len(all_results) == 5 query._reset() query.set_limits(low=1, high=3) query.run() paginated_results = query._results 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
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
def test_get_distinct_count_runs_query_when_cache_empty(self): """ Verify that get_distinct_count runs the query and caches/returns the distinct_count.""" course = CourseFactory() CourseRunFactory(title='foo', course=course) CourseRunFactory(title='foo', course=course) query = DistinctCountsSearchQuery() query.aggregation_key = 'aggregation_key' query.add_filter(SQ(title='foo')) query.add_model(CourseRun) assert query._distinct_hit_count is None assert query.get_distinct_count() == 1 assert query._distinct_hit_count == 1