22Test that changes to courses get synced into the new openedx_catalog models.
33"""
44
5+ import pytest
6+
57from openedx_catalog import api as catalog_api
8+ from openedx_catalog .models_api import CatalogCourse , CourseRun
69
710from cms .djangoapps .contentstore .views .course import rerun_course
811from xmodule .modulestore import ModuleStoreEnum
@@ -20,7 +23,7 @@ class CourseOverviewSyncTestCase(ImmediateOnCommitMixin, ModuleStoreTestCase):
2023 """
2124
2225 MODULESTORE = TEST_DATA_ONLY_SPLIT_MODULESTORE_DRAFT_PREFERRED
23- ENABLED_SIGNALS = ["course_published" ]
26+ ENABLED_SIGNALS = ["course_deleted" , " course_published" ]
2427
2528 def test_courserun_creation (self ) -> None :
2629 """
@@ -144,3 +147,42 @@ def test_courserun_of_many_sync(self) -> None:
144147 # Changing the language of the second run doesn't affect the lanugage of the overall catalog course (since the
145148 # first run is still in English)
146149 assert new_run .catalog_course .language_short == "en"
150+
151+ def test_courserun_deletion (self ) -> None :
152+ """
153+ Tests that when a course run is deleted, the corresponding CourseRun is
154+ deleted, and when it's the last run, the CatalogCourse is deleted too.
155+ """
156+ # Create a course with two runs:
157+ course = CourseFactory .create (display_name = "Intro to Testing" , emit_signals = True )
158+ course_id1 = course .location .context_key
159+ run1 = catalog_api .get_course_run (course_id1 )
160+ # re-run the course:
161+ course_id2 = rerun_course (
162+ self .user ,
163+ source_course_key = course_id1 ,
164+ org = course_id1 .org ,
165+ number = course_id1 .course ,
166+ run = "run2" ,
167+ fields = {"display_name" : "ItT run2" },
168+ background = False ,
169+ )
170+ run2 = catalog_api .get_course_run (course_id2 )
171+ catalog_course = run1 .catalog_course
172+ assert catalog_course == run2 .catalog_course # Same for run1 and run2
173+
174+ self .store .delete_course (course_id1 , ModuleStoreEnum .UserID .test )
175+ with pytest .raises (CourseRun .DoesNotExist ):
176+ run1 .refresh_from_db ()
177+
178+ # run2 should still exist:
179+ run2 .refresh_from_db ()
180+ assert run2 .catalog_course .display_name == "Intro to Testing" # The catalog course still exists and works
181+
182+ # delete run 2:
183+ self .store .delete_course (course_id2 , ModuleStoreEnum .UserID .test )
184+ with pytest .raises (CourseRun .DoesNotExist ):
185+ run2 .refresh_from_db ()
186+ # With no runs left, the CatalogCourse also gets auto-deleted:
187+ with pytest .raises (CatalogCourse .DoesNotExist ):
188+ catalog_course .refresh_from_db ()
0 commit comments