Commit e60e33c9 by mikedikan Committed by GitHub

Merge pull request #389 from edx/mdikan/remove-unpublished-programs-course-search-endpoint

Update Search Indexer to exclude unpublished programs
parents db339d3e 03ce0529
......@@ -171,6 +171,37 @@ class CourseRunSearchViewSetTests(DefaultPartnerMixin, SerializationMixin, Login
}
self.assertDictContainsSubset(expected, response_data['queries'])
def test_exclude_deleted_program_types(self):
""" Verify the deleted programs do not show in the program_types representation. """
self._test_exclude_program_types(ProgramStatus.Deleted)
def test_exclude_unpublished_program_types(self):
""" Verify the unpublished programs do not show in the program_types representation. """
self._test_exclude_program_types(ProgramStatus.Unpublished)
def _test_exclude_program_types(self, program_status):
""" Verify that programs with the provided type do not show in the program_types representation. """
course_run = CourseRunFactory(course__partner=self.partner, course__title='Software Testing',
status=CourseRunStatus.Published)
active_program = ProgramFactory(courses=[course_run.course], status=ProgramStatus.Active)
ProgramFactory(courses=[course_run.course], status=program_status)
with self.assertNumQueries(11):
response = self.get_search_response('software', faceted=False)
self.assertEqual(response.status_code, 200)
response_data = json.loads(response.content.decode('utf-8'))
# Validate the search results
expected = {
'count': 1,
'results': [
self.serialize_course_run(course_run)
]
}
self.assertDictContainsSubset(expected, response_data)
self.assertEqual(response_data['results'][0].get('program_types'), [active_program.type.name])
class AggregateSearchViewSet(DefaultPartnerMixin, SerializationMixin, LoginMixin, ElasticsearchTestMixin, APITestCase):
path = reverse('api:v1:search-all-facets')
......
......@@ -366,7 +366,13 @@ class CourseRun(TimeStampedModel):
@property
def program_types(self):
return [program.type.name for program in self.programs.all()]
"""
Exclude unpublished and deleted programs from list
so we don't identify that program type if not available
"""
program_statuses_to_exclude = (ProgramStatus.Unpublished, ProgramStatus.Deleted)
all_programs = [program for program in self.programs.exclude(status__in=program_statuses_to_exclude)]
return [program.type.name for program in all_programs]
@property
def marketing_url(self):
......
......@@ -163,6 +163,19 @@ class CourseRunTests(TestCase):
other_program = factories.ProgramFactory(courses=courses)
self.assertCountEqual(self.course_run.program_types, [program.type.name, other_program.type.name])
def test_unpublished_program_types(self):
""" Verify the property exludes program types that are unpublished. """
courses = [self.course_run.course]
program = factories.ProgramFactory(courses=courses)
factories.ProgramFactory(courses=courses, status=ProgramStatus.Unpublished)
self.assertEqual(self.course_run.program_types, [program.type.name])
def test_exclude_deleted_program_types(self):
""" Verify the program types property exclude programs that are deleted """
active_program = factories.ProgramFactory(courses=[self.course_run.course])
factories.ProgramFactory(courses=[self.course_run.course], status=ProgramStatus.Deleted)
self.assertEqual(self.course_run.program_types, [active_program.type.name])
class OrganizationTests(TestCase):
""" Tests for the `Organization` model. """
......
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