Commit 52587ad7 by Clinton Blackburn Committed by GitHub

Updated Program.start for search (#272)

- Fixed bug that occurs if no associated course runs have a start datetime
- Updated tests
- Exposed value in search index so that Programs are returned when faceting for availability

ECOM-5316
parent 338e785b
......@@ -650,9 +650,15 @@ class Program(TimeStampedModel):
@property
def start(self):
""" Start datetime, calculated by determining the earliest start datetime of all related course runs. """
if len(self.course_runs) == 0:
return None
return min([course_run.start for course_run in self.course_runs])
course_runs = self.course_runs
if course_runs:
start_dates = [course_run.start for course_run in self.course_runs if course_run.start]
if start_dates:
return min(start_dates)
return None
@property
def staff(self):
......
......@@ -150,6 +150,7 @@ class ProgramIndex(BaseIndex, indexes.Indexable, OrganizationsMixin):
card_image_url = indexes.CharField(model_attr='card_image_url', null=True)
status = indexes.CharField(model_attr='status', faceted=True)
partner = indexes.CharField(model_attr='partner__short_code', null=True, faceted=True)
start = indexes.DateTimeField(model_attr='start', null=True, faceted=True)
def prepare_organizations(self, obj):
return self.prepare_authoring_organizations(obj) + self.prepare_credit_backing_organizations(obj)
......
......@@ -11,12 +11,13 @@ from django.test import TestCase
from freezegun import freeze_time
from course_discovery.apps.core.models import Currency
from course_discovery.apps.core.tests.helpers import make_image_file
from course_discovery.apps.core.utils import SearchQuerySetWrapper
from course_discovery.apps.course_metadata.models import (
AbstractNamedModel, AbstractMediaModel, AbstractValueModel, Course, CourseRun, SeatType,
)
from course_discovery.apps.course_metadata.tests import factories
from course_discovery.apps.core.tests.helpers import make_image_file
from course_discovery.apps.course_metadata.tests.factories import CourseRunFactory
from course_discovery.apps.ietf_language_tags.models import LanguageTag
......@@ -310,9 +311,20 @@ class ProgramTests(TestCase):
self.assertEqual(actual_subjects, expected_subjects)
def test_start(self):
""" Verify the property returns the minimum start date for the course runs associated with the
program's courses. """
expected_start = min([course_run.start for course_run in self.course_runs])
self.assertEqual(self.program.start, expected_start)
# Verify start is None for programs with no courses.
self.program.courses.clear()
self.assertIsNone(self.program.start)
# Verify start is None if no course runs have a start date.
course_run = CourseRunFactory(start=None)
self.program.courses.add(course_run.course)
self.assertIsNone(self.program.start)
def test_price_ranges(self):
currency = Currency.objects.get(code='USD')
course_run = factories.CourseRunFactory()
......
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