Пример #1
0
    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
Пример #2
0
    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
Пример #3
0
    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')
Пример #4
0
    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
Пример #5
0
    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