55import pytest
66
77from learning_resources .factories import (
8+ ContentFileFactory ,
89 LearningResourceFactory ,
910 LearningResourceRunFactory ,
1011)
@@ -71,19 +72,23 @@ def test_search_index_plugin_resource_upserted(
7172
7273@pytest .mark .django_db
7374@pytest .mark .parametrize ("resource_type" , [COURSE_TYPE , PROGRAM_TYPE ])
75+ @pytest .mark .parametrize ("has_content_files" , [True , False ])
7476def test_search_index_plugin_resource_unpublished (
75- mocker , mock_search_index_helpers , resource_type
77+ mocker , mock_search_index_helpers , resource_type , has_content_files
7678):
7779 """The plugin function should remove a resource from the search index"""
7880 resource = LearningResourceFactory .create (resource_type = resource_type )
81+ if resource_type == COURSE_TYPE and has_content_files :
82+ for run in resource .runs .all ():
83+ ContentFileFactory .create (run = run )
7984 unpublish_run_mock = mocker .patch (
8085 "learning_resources_search.plugins.tasks.deindex_run_content_files.si"
8186 )
8287 SearchIndexPlugin ().resource_unpublished (resource )
8388 mock_search_index_helpers .mock_remove_learning_resource_immutable_signature .assert_called_once_with (
8489 resource .id , resource .resource_type
8590 )
86- if resource_type == COURSE_TYPE :
91+ if resource_type == COURSE_TYPE and has_content_files :
8792 assert unpublish_run_mock .call_count == resource .runs .count ()
8893 for run in resource .runs .all ():
8994 unpublish_run_mock .assert_any_call (run .id , unpublished_only = False )
@@ -107,26 +112,42 @@ def test_search_index_plugin_resource_before_delete(
107112
108113
109114@pytest .mark .django_db
110- def test_search_index_plugin_resource_run_unpublished (mock_search_index_helpers ):
115+ @pytest .mark .parametrize ("has_content_files" , [True , False ])
116+ def test_search_index_plugin_resource_run_unpublished (
117+ mock_search_index_helpers , has_content_files
118+ ):
111119 """The plugin function should remove a run's contenfiles from the search index"""
112120 run = LearningResourceRunFactory .create ()
121+ if has_content_files :
122+ ContentFileFactory .create (run = run )
113123 SearchIndexPlugin ().resource_run_unpublished (run )
114- mock_search_index_helpers .mock_remove_contentfiles_immutable_signature .assert_called_once_with (
115- run .id ,
116- unpublished_only = False ,
117- )
124+ if has_content_files :
125+ mock_search_index_helpers .mock_remove_contentfiles_immutable_signature .assert_called_once_with (
126+ run .id ,
127+ unpublished_only = False ,
128+ )
129+ else :
130+ mock_search_index_helpers .mock_remove_contentfiles_immutable_signature .assert_not_called ()
118131
119132
120133@pytest .mark .django_db
121- def test_search_index_plugin_resource_run_delete (mock_search_index_helpers ):
134+ @pytest .mark .parametrize ("has_content_files" , [True , False ])
135+ def test_search_index_plugin_resource_run_delete (
136+ mock_search_index_helpers , has_content_files
137+ ):
122138 """The plugin function should remove contenfiles from the index and delete the run"""
123139 run = LearningResourceRunFactory .create ()
140+ if has_content_files :
141+ ContentFileFactory .create (run = run )
124142 run_id = run .id
125143 SearchIndexPlugin ().resource_run_delete (run )
126- mock_search_index_helpers .mock_remove_contentfiles_immutable_signature .assert_called_once_with (
127- run_id ,
128- unpublished_only = False ,
129- )
144+ if has_content_files :
145+ mock_search_index_helpers .mock_remove_contentfiles_immutable_signature .assert_called_once_with (
146+ run_id ,
147+ unpublished_only = False ,
148+ )
149+ else :
150+ mock_search_index_helpers .mock_remove_contentfiles_immutable_signature .assert_not_called ()
130151 assert LearningResourceRun .objects .filter (id = run_id ).exists () is False
131152
132153
0 commit comments