Commit 3fa7c15f by Matthew Piatetsky

Deduplicate course runs in typeahead by course

ECOM-7338
parent 506e0d5b
......@@ -473,6 +473,26 @@ class TypeaheadSearchViewTests(DefaultPartnerMixin, TypeaheadSerializationMixin,
self.assertEqual(len(response_data['course_runs']), RESULT_COUNT)
self.assertEqual(len(response_data['programs']), RESULT_COUNT)
def test_typeahead_deduplicate_course_runs(self):
""" Verify the typeahead response will only include the first course run per course. """
RESULT_COUNT = TypeaheadSearchView.RESULT_COUNT
title = "Test"
course1 = CourseFactory(partner=self.partner)
course2 = CourseFactory(partner=self.partner)
for i in range(RESULT_COUNT):
CourseRunFactory(title="{}{}{}".format(title, course1.title, i), course=course1)
for i in range(RESULT_COUNT):
CourseRunFactory(title="{}{}{}".format(title, course2.title, i), course=course2)
response = self.get_response({'q': title})
assert response.status_code == 200
response_data = response.json()
# There are many runs for both courses, but only one from each will be included
course_runs = response_data['course_runs']
assert len(course_runs) == 2
# compare course titles embedded in course run title to ensure that course runs belong to different courses
assert course_runs[0]['title'][4:-1] != course_runs[1]['title'][4:-1]
def test_typeahead_multiple_authoring_organizations(self):
""" Test typeahead response with multiple authoring organizations. """
title = "Design"
......
......@@ -134,7 +134,17 @@ class TypeaheadSearchView(PartnerMixin, APIView):
SQ(authoring_organizations_autocomplete=clean_query)
)
course_runs = course_runs.filter(published=True).exclude(hidden=True).filter(partner=partner.short_code)
course_runs = course_runs[:self.RESULT_COUNT]
# Get first three results after deduplicating by course run
course_runs_list, course_keys_set = [], set()
for course_run in course_runs:
course_key = course_run.course_key
if course_key in course_keys_set:
continue
course_keys_set.add(course_key)
course_runs_list.append(course_run)
if len(course_runs_list) == 3:
break
programs = sqs.models(Program).filter(
SQ(title_autocomplete=clean_query) |
......@@ -143,7 +153,7 @@ class TypeaheadSearchView(PartnerMixin, APIView):
programs = programs.filter(status=ProgramStatus.Active).filter(partner=partner.short_code)
programs = programs[:self.RESULT_COUNT]
return course_runs, programs
return course_runs_list, programs
def get(self, request, *args, **kwargs):
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment