@@ -116,16 +116,19 @@ def make_studio_data(self, run, add_pacing=True, add_schedule=True, team=None, a
116116 }
117117 if add_pacing :
118118 data ['pacing_type' ] = run .pacing_type
119- if add_schedule :
120- data ['schedule' ] = {
121- 'start' : serialize_datetime (run .start ),
122- 'end' : serialize_datetime (run .end ),
123- }
124- if add_enrollment_dates :
125- data ['schedule' ] = {
126- 'enrollment_start' : serialize_datetime (run .enrollment_start ),
127- 'enrollment_end' : serialize_datetime (run .enrollment_end ),
128- }
119+
120+ if add_schedule or add_enrollment_dates :
121+ data ['schedule' ] = {}
122+ if add_schedule :
123+ data ['schedule' ].update ({
124+ 'start' : serialize_datetime (run .start ),
125+ 'end' : serialize_datetime (run .end ),
126+ })
127+ if add_enrollment_dates :
128+ data ['schedule' ].update ({
129+ 'enrollment_start' : serialize_datetime (run .enrollment_start ),
130+ 'enrollment_end' : serialize_datetime (run .enrollment_end ),
131+ })
129132 return data
130133
131134 def test_create_rerun (self ):
@@ -194,7 +197,7 @@ def test_generate_data_for_studio_api__external_course_enrollment_dates(self):
194197 exec_ed_type = CourseTypeFactory (slug = CourseType .EXECUTIVE_EDUCATION_2U )
195198 run = CourseRunFactory (course = CourseFactory (type = exec_ed_type ))
196199 with mock .patch ('course_discovery.apps.api.utils.logger' ) as mock_logger :
197- expected_data = self .make_studio_data (run , add_pacing = False , add_schedule = False , add_enrollment_dates = True )
200+ expected_data = self .make_studio_data (run , add_pacing = False , add_schedule = True , add_enrollment_dates = True )
198201 output_data = StudioAPI .generate_data_for_studio_api (run , False )
199202 assert output_data == expected_data
200203 mock_logger .info .assert_called_with (
@@ -208,14 +211,35 @@ def test_generate_data_for_studio_api__external_course_missing_enrollment_dates(
208211 exec_ed_type = CourseTypeFactory (slug = CourseType .EXECUTIVE_EDUCATION_2U )
209212 run = CourseRunFactory (course = CourseFactory (type = exec_ed_type ), enrollment_start = None , enrollment_end = None )
210213 with mock .patch ('course_discovery.apps.api.utils.logger' ) as mock_logger :
211- expected_data = self .make_studio_data (run , add_pacing = False , add_schedule = False )
214+ expected_data = self .make_studio_data (run , add_pacing = False , add_schedule = True , add_enrollment_dates = False )
212215 output_data = StudioAPI .generate_data_for_studio_api (run , False )
213216 assert output_data == expected_data
214217
215218 with self .assertRaises (AssertionError ):
216- mock_logger .info .assert_called_with (
217- f'Enrollment information added to data { output_data } for course run { run .key } '
218- )
219+ mock_logger .info .assert_called ()
220+
221+ def test_generate_data_for_studio_api__non_external_course_no_start_end_on_update (self ):
222+ """Test that start/end are NOT included when updating a non-external course."""
223+ run = CourseRunFactory () # Default: non-external
224+ expected_data = self .make_studio_data (run , add_pacing = False , add_schedule = False , add_enrollment_dates = True )
225+ with mock .patch ('course_discovery.apps.api.utils.logger' ) as mock_logger :
226+ output_data = StudioAPI .generate_data_for_studio_api (run , creating = False )
227+ self .assertEqual (output_data , expected_data )
228+ mock_logger .info .assert_called_with (
229+ f'Enrollment information added to data { output_data } for course run { run .key } '
230+ )
231+
232+ def test_generate_data_for_studio_api__external_course_start_end_on_update (self ):
233+ """Test that start/end are included for external course on update."""
234+ exec_ed_type = CourseTypeFactory (slug = CourseType .EXECUTIVE_EDUCATION_2U )
235+ run = CourseRunFactory (course = CourseFactory (type = exec_ed_type ))
236+ expected_data = self .make_studio_data (run , add_pacing = False , add_schedule = True , add_enrollment_dates = True )
237+ with mock .patch ('course_discovery.apps.api.utils.logger' ) as mock_logger :
238+ output_data = StudioAPI .generate_data_for_studio_api (run , creating = False )
239+ self .assertEqual (output_data , expected_data )
240+ mock_logger .info .assert_called_with (
241+ f'Enrollment information added to data { output_data } for course run { run .key } '
242+ )
219243
220244 def test_calculate_course_run_key_run_value_with_multiple_runs_per_trimester (self ):
221245 start = datetime .datetime (2017 , 2 , 1 )
0 commit comments